diff --git a/CHANGELOG.md b/CHANGELOG.md index 982ef253..f4698480 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,6 @@ - Added `skip_databases` option - Added warning for unsupported adapters -- Improved instructions for removing a column with `NOT NULL` and no default value - Improved output for `db:forward`, `db:rollback`, `db:migrate:up`, and `db:migrate:down` ## 2.0.2 (2024-10-30) diff --git a/README.md b/README.md index 67bab5b6..017e4d69 100644 --- a/README.md +++ b/README.md @@ -129,16 +129,6 @@ end 4. Deploy and run the migration 5. Remove the line added in step 1 -Note: For columns with `NOT NULL` and no default value, remove `NOT NULL` before any other steps. - -```ruby -class RemoveNotNull < ActiveRecord::Migration[7.2] - def change - change_column_null :users, :some_column, true - end -end -``` - ### Changing the type of a column #### Bad diff --git a/lib/strong_migrations/checks.rb b/lib/strong_migrations/checks.rb index 78871941..7c863673 100644 --- a/lib/strong_migrations/checks.rb +++ b/lib/strong_migrations/checks.rb @@ -319,19 +319,11 @@ def check_remove_column(method, *args) code = "self.ignored_columns += #{columns.map(&:to_s).inspect}" - table_columns = connection.columns(args[0]).index_by(&:name) rescue {} - null_columns = columns.select { |c| table_columns[c.to_s] && !table_columns[c.to_s].null && table_columns[c.to_s].default.nil? } - if null_columns.any? - commands = null_columns.map { |c| command_str(:change_column_null, [args[0], c, true]) } - null_code = "First, remove NOT NULL:\n\nclass RemoveNotNull < ActiveRecord::Migration#{migration_suffix}\n def change\n #{commands.join("\n ")}\n end\nend\n\nDeploy and run the migration. " - end - raise_error :remove_column, model: model_name(args[0]), code: code, command: command_str(method, args), - column_suffix: columns.size > 1 ? "s" : "", - null_code: null_code + column_suffix: columns.size > 1 ? "s" : "" end def check_remove_index(*args) diff --git a/lib/strong_migrations/error_messages.rb b/lib/strong_migrations/error_messages.rb index 9d5d3512..8ae60868 100644 --- a/lib/strong_migrations/error_messages.rb +++ b/lib/strong_migrations/error_messages.rb @@ -61,7 +61,7 @@ def change "Changing the type is safe, but setting NOT NULL is not.", remove_column: "Active Record caches attributes, which causes problems -when removing columns. %{null_code}Ignore the column%{column_suffix}: +when removing columns. Be sure to ignore the column%{column_suffix}: class %{model} < %{base_model} %{code} diff --git a/test/migrations/remove_column.rb b/test/migrations/remove_column.rb index 27530645..6e370bd9 100644 --- a/test/migrations/remove_column.rb +++ b/test/migrations/remove_column.rb @@ -39,15 +39,3 @@ def change remove_belongs_to :users, :device end end - -class RemoveColumnNull < TestMigration - def change - remove_column :devices, :name, :string - end -end - -class RemoveColumnsNull < TestMigration - def change - remove_columns :devices, :name, :city, :country - end -end diff --git a/test/remove_column_test.rb b/test/remove_column_test.rb index ca35f8b7..1f950d7d 100644 --- a/test/remove_column_test.rb +++ b/test/remove_column_test.rb @@ -28,12 +28,4 @@ def test_remove_reference_polymorphic def test_remove_belongs_to assert_unsafe RemoveBelongsTo end - - def test_remove_column_null - assert_unsafe RemoveColumnNull - end - - def test_remove_columns_null - assert_unsafe RemoveColumnsNull - end end diff --git a/test/test_helper.rb b/test/test_helper.rb index ce1d3ced..132c8aec 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -93,9 +93,6 @@ def connection_class end create_table :devices do |t| - t.string :name, null: false - t.string :city, null: false - t.string :country, null: false, default: "" end end