Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid schema dump when primary key column has a comment #36384

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
* Fix invalid schema when primary key column has a comment

Fixes #29966

*Guilherme Goettems Schneider*

* Fix table comment also being applied to the primary key column

*Guilherme Goettems Schneider*
Expand Down
Expand Up @@ -15,7 +15,7 @@ def column_spec(column)
def column_spec_for_primary_key(column)
return {} if default_primary_key?(column)
spec = { id: schema_type(column).inspect }
spec.merge!(prepare_column_options(column).except!(:null))
spec.merge!(prepare_column_options(column).except!(:null, :comment))
spec[:default] ||= "nil" if explicit_primary_key_default?(column)
spec
end
Expand Down
22 changes: 21 additions & 1 deletion activerecord/test/cases/comment_test.rb
Expand Up @@ -14,6 +14,9 @@ class Commented < ActiveRecord::Base
class BlankComment < ActiveRecord::Base
end

class PkCommented < ActiveRecord::Base
end

setup do
@connection = ActiveRecord::Base.connection

Expand All @@ -35,16 +38,21 @@ class BlankComment < ActiveRecord::Base
t.index :absent_comment
end

@connection.create_table("pk_commenteds", comment: "Table comment", id: false, force: true) do |t|
t.integer :id, comment: "Primary key comment", primary_key: true
end

Commented.reset_column_information
BlankComment.reset_column_information
PkCommented.reset_column_information
end

teardown do
@connection.drop_table "commenteds", if_exists: true
@connection.drop_table "blank_comments", if_exists: true
end

def test_primary_key_comment
def test_default_primary_key_comment
column = Commented.columns_hash["id"]
assert_nil column.comment
end
Expand Down Expand Up @@ -169,5 +177,17 @@ def test_change_column_comment_to_nil
column = Commented.columns_hash["name"]
assert_nil column.comment
end

def test_comment_on_primary_key
column = PkCommented.columns_hash["id"]
assert_equal "Primary key comment", column.comment
assert_equal "Table comment", @connection.table_comment("pk_commenteds")
end

def test_schema_dump_with_primary_key_comment
output = dump_table_schema "pk_commenteds"
assert_match %r[create_table "pk_commenteds",.*\s+comment: "Table comment"], output
assert_no_match %r[create_table "pk_commenteds",.*\s+comment: "Primary key comment"], output
end
end
end