diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index cfc5647969d756b9ba208017c1602304756528e6..8bdcdbda9bdcbfa98a16a06943e1dc983bcd0853 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,15 @@ +* Added `index` option for `change_table` migration helpers. + With this change you can create indexes while adding new + columns into the existing tables. + + Example: + + change_table(:languages) do |t| + t.string :country_code, index: true + end + + *Mehmet Emin İNAÇ* + * Don't update counter cache unless the record is actually saved. Fixes #31493, #33113, #33117. diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb index 582ac516c71b038e92057765c60e81665b32254c..015204c0563aa2458f8ce41a2d0ed677292d25b4 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb @@ -527,7 +527,9 @@ def initialize(table_name, base) # # See TableDefinition#column for details of the options you can use. def column(column_name, type, options = {}) + index_options = options.delete(:index) @base.add_column(name, column_name, type, options) + index(column_name, index_options.is_a?(Hash) ? index_options : {}) if index_options end # Checks to see if a column exists. diff --git a/activerecord/test/cases/migration/change_table_test.rb b/activerecord/test/cases/migration/change_table_test.rb index 034bf3216506a5f3be07589c30ab97fda330aab9..c108d372d15e480626757f228801994f86cf778e 100644 --- a/activerecord/test/cases/migration/change_table_test.rb +++ b/activerecord/test/cases/migration/change_table_test.rb @@ -164,6 +164,14 @@ def test_column_creates_column_with_options end end + def test_column_creates_column_with_index + with_change_table do |t| + @connection.expect :add_column, nil, [:delete_me, :bar, :integer, {}] + @connection.expect :add_index, nil, [:delete_me, :bar, {}] + t.column :bar, :integer, index: true + end + end + def test_index_creates_index with_change_table do |t| @connection.expect :add_index, nil, [:delete_me, :bar, {}]