1
0
Fork 0
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:
Rafael França 2018-01-29 18:48:35 -05:00 committed by GitHub
commit 4734812ec5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 2 deletions

View file

@ -124,11 +124,15 @@ module ActiveRecord
# add info on sort order (only desc order is explicitly specified, asc is the default) # add info on sort order (only desc order is explicitly specified, asc is the default)
# and non-default opclasses # 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 opclasses[column] = opclass.to_sym if opclass
if nulls
orders[column] = [desc, nulls].compact.join(" ")
else
orders[column] = :desc if desc orders[column] = :desc if desc
end end
end end
end
IndexDefinition.new( IndexDefinition.new(
table_name, table_name,

View file

@ -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)) 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 }) 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 assert_raise ArgumentError do
add_index(:people, :last_name, algorithm: :copy) add_index(:people, :last_name, algorithm: :copy)
end end

View file

@ -532,6 +532,34 @@ class SchemaIndexOpclassTest < ActiveRecord::PostgreSQLTestCase
end end
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 class DefaultsUsingMultipleSchemasAndDomainTest < ActiveRecord::PostgreSQLTestCase
setup do setup do
@connection = ActiveRecord::Base.connection @connection = ActiveRecord::Base.connection