mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #31814 from fatkodima/index-nulls-order
Dump correctly index nulls order for PostgreSQL
This commit is contained in:
commit
4734812ec5
3 changed files with 40 additions and 2 deletions
|
@ -124,11 +124,15 @@ module ActiveRecord
|
|||
|
||||
# add info on sort order (only desc order is explicitly specified, asc is the default)
|
||||
# and non-default opclasses
|
||||
expressions.scan(/(\w+)(?: (?!DESC)(\w+))?(?: (DESC))?/).each do |column, opclass, desc|
|
||||
expressions.scan(/(?<column>\w+)\s?(?<opclass>\w+_ops)?\s?(?<desc>DESC)?\s?(?<nulls>NULLS (?:FIRST|LAST))?/).each do |column, opclass, desc, nulls|
|
||||
opclasses[column] = opclass.to_sym if opclass
|
||||
if nulls
|
||||
orders[column] = [desc, nulls].compact.join(" ")
|
||||
else
|
||||
orders[column] = :desc if desc
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
IndexDefinition.new(
|
||||
table_name,
|
||||
|
|
|
@ -62,6 +62,12 @@ class PostgresqlActiveSchemaTest < ActiveRecord::PostgreSQLTestCase
|
|||
expected = %(CREATE INDEX "index_people_on_last_name" ON "people" USING gist ("last_name" bpchar_pattern_ops))
|
||||
assert_equal expected, add_index(:people, :last_name, using: :gist, opclass: { last_name: :bpchar_pattern_ops })
|
||||
|
||||
expected = %(CREATE INDEX "index_people_on_last_name_and_first_name" ON "people" ("last_name" DESC NULLS LAST, "first_name" ASC))
|
||||
assert_equal expected, add_index(:people, [:last_name, :first_name], order: { last_name: "DESC NULLS LAST", first_name: :asc })
|
||||
|
||||
expected = %(CREATE INDEX "index_people_on_last_name" ON "people" ("last_name" NULLS FIRST))
|
||||
assert_equal expected, add_index(:people, :last_name, order: "NULLS FIRST")
|
||||
|
||||
assert_raise ArgumentError do
|
||||
add_index(:people, :last_name, algorithm: :copy)
|
||||
end
|
||||
|
|
|
@ -532,6 +532,34 @@ class SchemaIndexOpclassTest < ActiveRecord::PostgreSQLTestCase
|
|||
end
|
||||
end
|
||||
|
||||
class SchemaIndexNullsOrderTest < ActiveRecord::PostgreSQLTestCase
|
||||
include SchemaDumpingHelper
|
||||
|
||||
setup do
|
||||
@connection = ActiveRecord::Base.connection
|
||||
@connection.create_table "trains" do |t|
|
||||
t.string :name
|
||||
t.text :description
|
||||
end
|
||||
end
|
||||
|
||||
teardown do
|
||||
@connection.drop_table "trains", if_exists: true
|
||||
end
|
||||
|
||||
def test_nulls_order_is_dumped
|
||||
@connection.execute "CREATE INDEX trains_name_and_description ON trains USING btree(name NULLS FIRST, description)"
|
||||
output = dump_table_schema "trains"
|
||||
assert_match(/order: \{ name: "NULLS FIRST" \}/, output)
|
||||
end
|
||||
|
||||
def test_non_default_order_with_nulls_is_dumped
|
||||
@connection.execute "CREATE INDEX trains_name_and_desc ON trains USING btree(name DESC NULLS LAST, description)"
|
||||
output = dump_table_schema "trains"
|
||||
assert_match(/order: \{ name: "DESC NULLS LAST" \}/, output)
|
||||
end
|
||||
end
|
||||
|
||||
class DefaultsUsingMultipleSchemasAndDomainTest < ActiveRecord::PostgreSQLTestCase
|
||||
setup do
|
||||
@connection = ActiveRecord::Base.connection
|
||||
|
|
Loading…
Reference in a new issue