Skip to content

Commit

Permalink
Add support for primary key column comment with primary_key_comment
Browse files Browse the repository at this point in the history
… option to `create_table`

Before this schema dump/load was not able to keep a comment that was added to a primary key column.
  • Loading branch information
guigs committed Jun 3, 2019
1 parent c926ca4 commit f634630
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Add support for primary key column comment with `primary_key_comment` option to `create_table`

*Guilherme Goettems Schneider*

* Fix invalid schema when primary key column has a comment

Fixes #29966
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ def column_spec(column)
end

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, :comment))
spec = {}
spec[:id] = schema_type(column).inspect unless default_primary_key?(column)
spec.merge!(prepare_primary_key_options(column))
spec[:default] ||= "nil" if explicit_primary_key_default?(column)
spec
end
Expand All @@ -33,6 +33,13 @@ def prepare_column_options(column)
spec
end

def prepare_primary_key_options(column)
options = prepare_column_options(column).except!(:null)
comment = options.delete(:comment)
options[:primary_key_comment] = comment if comment
options
end

def default_primary_key?(column)
schema_type(column) == :bigint
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def primary_key(table_name)
# SELECT * FROM orders INNER JOIN line_items ON order_id=orders.id
#
# See also TableDefinition#column for details on how to create columns.
def create_table(table_name, id: :primary_key, primary_key: nil, force: nil, **options)
def create_table(table_name, id: :primary_key, primary_key: nil, force: nil, primary_key_comment: nil, **options)
td = create_table_definition(
table_name, options.extract!(:temporary, :if_not_exists, :options, :as, :comment)
)
Expand All @@ -302,6 +302,7 @@ def create_table(table_name, id: :primary_key, primary_key: nil, force: nil, **o
if pk.is_a?(Array)
td.primary_keys pk
else
options[:comment] = primary_key_comment if primary_key_comment
td.primary_key pk, id, options
end
end
Expand Down
6 changes: 2 additions & 4 deletions activerecord/test/cases/comment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ class PkCommented < 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
@connection.create_table("pk_commenteds", comment: "Table comment", primary_key_comment: "Primary key comment", force: true)

Commented.reset_column_information
BlankComment.reset_column_information
Expand Down Expand Up @@ -187,7 +185,7 @@ def test_comment_on_primary_key
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
assert_match %r[create_table "pk_commenteds",.*\s+primary_key_comment: "Primary key comment"], output
end
end
end

0 comments on commit f634630

Please sign in to comment.