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

Add comment and table_comment table schema definition #51661

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions activerecord/CHANGELOG.md
Expand Up @@ -10,4 +10,15 @@

*Eileen M. Uchitelle*

* Add `comment` and `table_comment` for `change_table` definition.

```ruby
change_table :users do |t|
t.comment :login, "Username"
t.table_comment "Usual users"
end
```

*Edem Topuzov*

Please check [7-2-stable](https://github.com/rails/rails/blob/7-2-stable/activerecord/CHANGELOG.md) for previous changes.
Expand Up @@ -701,6 +701,8 @@ def add_column(name, type, **options)
# t.remove_index
# t.remove_check_constraint
# t.remove_timestamps
# t.comment
# t.table_comment
# end
#
class Table
Expand Down Expand Up @@ -941,6 +943,24 @@ def check_constraint_exists?(*args, **options)
@base.check_constraint_exists?(name, *args, **options)
end

# Changes the comment for a column.
#
# t.comment(:state, "State column comment")
#
# See {connection.change_column_comment}[rdoc-ref:SchemaStatements#change_column_comment]
def comment(column_name, comment_or_changes)
@base.change_column_comment(name, column_name, comment_or_changes)
end

# Changes the comment for a table.
#
# t.table_comment("Table comment")
#
# See {connection.change_table_comment}[rdoc-ref:SchemaStatements#change_table_comment]
def table_comment(comment_or_changes)
@base.change_table_comment(name, comment_or_changes)
end

private
def raise_on_if_exist_options(options)
unrecognized_option = options.keys.find do |key|
Expand Down
16 changes: 16 additions & 0 deletions activerecord/test/cases/migration/change_table_test.rb
Expand Up @@ -359,6 +359,22 @@ def test_validate_constraint
end
end

if ActiveRecord::Base.lease_connection.supports_comments?
def test_change_column_comment
with_change_table do |t|
expect :change_column_comment, nil, [:delete_me, :bar, "Edited column comment"]
t.comment :bar, "Edited column comment"
end
end

def test_change_table_comment
with_change_table do |t|
expect :change_table_comment, nil, [:delete_me, "Edited table comment"]
t.table_comment "Edited table comment"
end
end
end

def test_remove_column_with_if_exists_raises_error
assert_raises(ArgumentError) do
with_change_table do |t|
Expand Down