Skip to content

Commit

Permalink
Add comment and table_comment for change_table definition
Browse files Browse the repository at this point in the history
  • Loading branch information
mechnicov committed May 22, 2024
1 parent b504498 commit a878018
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
11 changes: 11 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
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.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit a878018

Please sign in to comment.