From 5523449278c7758a34a76da0fa9203f92e0c8c05 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Mon, 17 Jun 2024 11:03:17 -0700 Subject: [PATCH] Dropped support for Ruby < 3 and Active Record < 6.1 --- .github/workflows/build.yml | 4 --- CHANGELOG.md | 4 +++ gemfiles/activerecord52.gemfile | 9 ------ gemfiles/activerecord60.gemfile | 9 ------ .../strong_migrations/install_generator.rb | 6 +--- .../adapters/abstract_adapter.rb | 8 ----- lib/strong_migrations/checks.rb | 29 ++----------------- lib/strong_migrations/safe_methods.rb | 12 ++------ strong_migrations.gemspec | 4 +-- test/add_check_constraint_test.rb | 5 ---- test/add_foreign_key_test.rb | 14 +-------- test/add_index_test.rb | 4 +-- test/change_column_null_test.rb | 2 +- test/multiple_databases_test.rb | 19 +----------- test/remove_index_test.rb | 10 ++----- test/safe_by_default_test.rb | 22 +++----------- test/test_helper.rb | 14 +-------- 17 files changed, 23 insertions(+), 152 deletions(-) delete mode 100644 gemfiles/activerecord52.gemfile delete mode 100644 gemfiles/activerecord60.gemfile diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index fb378cc2..a0e05e19 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -15,10 +15,6 @@ jobs: gemfile: gemfiles/activerecord70.gemfile - ruby: "3.0" gemfile: gemfiles/activerecord61.gemfile - - ruby: 2.7 - gemfile: gemfiles/activerecord60.gemfile - - ruby: 2.6 - gemfile: gemfiles/activerecord52.gemfile env: BUNDLE_GEMFILE: ${{ matrix.gemfile }} steps: diff --git a/CHANGELOG.md b/CHANGELOG.md index abdd2e15..6dc8b3f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.0.0 (unreleased) + +- Dropped support for Ruby < 3 and Active Record < 6.1 + ## 1.8.0 (2024-03-11) - Added check for `add_column` with auto-incrementing columns diff --git a/gemfiles/activerecord52.gemfile b/gemfiles/activerecord52.gemfile deleted file mode 100644 index caa76ad3..00000000 --- a/gemfiles/activerecord52.gemfile +++ /dev/null @@ -1,9 +0,0 @@ -source "https://rubygems.org" - -gemspec path: ".." - -gem "rake" -gem "minitest", ">= 5" -gem "activerecord", "~> 5.2.0" -gem "pg" -gem "mysql2" diff --git a/gemfiles/activerecord60.gemfile b/gemfiles/activerecord60.gemfile deleted file mode 100644 index 2ec8538d..00000000 --- a/gemfiles/activerecord60.gemfile +++ /dev/null @@ -1,9 +0,0 @@ -source "https://rubygems.org" - -gemspec path: ".." - -gem "rake" -gem "minitest", ">= 5" -gem "activerecord", "~> 6.0.0" -gem "pg" -gem "mysql2" diff --git a/lib/generators/strong_migrations/install_generator.rb b/lib/generators/strong_migrations/install_generator.rb index 26e73b8a..33ac1907 100644 --- a/lib/generators/strong_migrations/install_generator.rb +++ b/lib/generators/strong_migrations/install_generator.rb @@ -31,11 +31,7 @@ def target_version end def adapter - if ActiveRecord::VERSION::STRING.to_f >= 6.1 - ActiveRecord::Base.connection_db_config.adapter.to_s - else - ActiveRecord::Base.connection_config[:adapter].to_s - end + ActiveRecord::Base.connection_db_config.adapter.to_s end def postgresql? diff --git a/lib/strong_migrations/adapters/abstract_adapter.rb b/lib/strong_migrations/adapters/abstract_adapter.rb index 704fd7c2..7c4186d9 100644 --- a/lib/strong_migrations/adapters/abstract_adapter.rb +++ b/lib/strong_migrations/adapters/abstract_adapter.rb @@ -55,14 +55,6 @@ def target_version(target_version) version = if target_version && StrongMigrations.developer_env? if target_version.is_a?(Hash) - # Active Record 6.0 supports multiple databases - # but connection.pool.spec.name always returns "primary" - # in migrations with rails db:migrate - if ActiveRecord::VERSION::STRING.to_f < 6.1 - # error class is not shown in db:migrate output so ensure message is descriptive - raise StrongMigrations::Error, "StrongMigrations.target_version does not support multiple databases for Active Record < 6.1" - end - db_config_name = connection.pool.db_config.name target_version.stringify_keys.fetch(db_config_name) do # error class is not shown in db:migrate output so ensure message is descriptive diff --git a/lib/strong_migrations/checks.rb b/lib/strong_migrations/checks.rb index b0d3ee3d..7acb1974 100644 --- a/lib/strong_migrations/checks.rb +++ b/lib/strong_migrations/checks.rb @@ -249,25 +249,13 @@ def check_change_column_null(*args) validate_code = constraint_str("ALTER TABLE %s VALIDATE CONSTRAINT %s", [table, constraint_name]) remove_code = constraint_str("ALTER TABLE %s DROP CONSTRAINT %s", [table, constraint_name]) - constraint_methods = ar_version >= 6.1 - - validate_constraint_code = - if constraint_methods - String.new(command_str(:validate_check_constraint, [table, {name: constraint_name}])) - else - String.new(safety_assured_str(validate_code)) - end + validate_constraint_code = String.new(command_str(:validate_check_constraint, [table, {name: constraint_name}])) if safe_with_check_constraint change_args = [table, column, null] validate_constraint_code << "\n #{command_str(:change_column_null, change_args)}" - - if constraint_methods - validate_constraint_code << "\n #{command_str(:remove_check_constraint, [table, {name: constraint_name}])}" - else - validate_constraint_code << "\n #{safety_assured_str(remove_code)}" - end + validate_constraint_code << "\n #{command_str(:remove_check_constraint, [table, {name: constraint_name}])}" end if StrongMigrations.safe_by_default @@ -275,12 +263,7 @@ def check_change_column_null(*args) throw :safe end - add_constraint_code = - if constraint_methods - command_str(:add_check_constraint, [table, "#{quote_column_if_needed(column)} IS NOT NULL", {name: constraint_name, validate: false}]) - else - safety_assured_str(add_code) - end + add_constraint_code = command_str(:add_check_constraint, [table, "#{quote_column_if_needed(column)} IS NOT NULL", {name: constraint_name, validate: false}]) validate_constraint_code = if safe_with_check_constraint @@ -373,12 +356,6 @@ def check_remove_index(*args) # avoid suggesting extra (invalid) args args = args[0..1] unless StrongMigrations.safe_by_default - # Active Record < 6.1 only supports two arguments (including options) - if args.size == 2 && ar_version < 6.1 - # arg takes precedence over option - options[:column] = args.pop - end - if StrongMigrations.safe_by_default safe_remove_index(*args, **options) throw :safe diff --git a/lib/strong_migrations/safe_methods.rb b/lib/strong_migrations/safe_methods.rb index 85ce0d90..0bfffed7 100644 --- a/lib/strong_migrations/safe_methods.rb +++ b/lib/strong_migrations/safe_methods.rb @@ -50,19 +50,11 @@ def safe_add_foreign_key(from_table, to_table, *args, **options) @migration.add_foreign_key(from_table, to_table, *args, **options.merge(validate: false)) disable_transaction validate_options = options.slice(:column, :name) - if ActiveRecord::VERSION::MAJOR >= 6 - @migration.validate_foreign_key(from_table, to_table, **validate_options) - else - @migration.validate_foreign_key(from_table, validate_options.any? ? validate_options : to_table) - end + @migration.validate_foreign_key(from_table, to_table, **validate_options) end dir.down do remove_options = options.slice(:column, :name) - if ActiveRecord::VERSION::MAJOR >= 6 - @migration.remove_foreign_key(from_table, to_table, **remove_options) - else - @migration.remove_foreign_key(from_table, remove_options.any? ? remove_options : to_table) - end + @migration.remove_foreign_key(from_table, to_table, **remove_options) end end end diff --git a/strong_migrations.gemspec b/strong_migrations.gemspec index b5ac6589..3c5d36dd 100644 --- a/strong_migrations.gemspec +++ b/strong_migrations.gemspec @@ -13,7 +13,7 @@ Gem::Specification.new do |spec| spec.files = Dir["*.{md,txt}", "{lib}/**/*"] spec.require_path = "lib" - spec.required_ruby_version = ">= 2.6" + spec.required_ruby_version = ">= 3" - spec.add_dependency "activerecord", ">= 5.2" + spec.add_dependency "activerecord", ">= 6.1" end diff --git a/test/add_check_constraint_test.rb b/test/add_check_constraint_test.rb index 85dfe0a2..abba9031 100644 --- a/test/add_check_constraint_test.rb +++ b/test/add_check_constraint_test.rb @@ -1,11 +1,6 @@ require_relative "test_helper" class AddCheckConstraintTest < Minitest::Test - def setup - skip unless check_constraints? - super - end - def test_basic assert_unsafe AddCheckConstraint end diff --git a/test/add_foreign_key_test.rb b/test/add_foreign_key_test.rb index d1c68e52..116e772c 100644 --- a/test/add_foreign_key_test.rb +++ b/test/add_foreign_key_test.rb @@ -10,22 +10,16 @@ def test_basic end def test_safe - skip "Active Record 6.0.3 bug" if ar_bug? && (mysql? || mariadb?) - assert_safe AddForeignKeySafe end def test_validate_same_transaction - skip "Active Record 6.0.3 bug" if ar_bug? - skip unless postgresql? assert_unsafe AddForeignKeyValidateSameTransaction end def test_validate_no_transaction - skip "Active Record 6.0.3 bug" if ar_bug? - skip unless postgresql? assert_safe AddForeignKeyValidateNoTransaction @@ -34,14 +28,8 @@ def test_validate_no_transaction def test_extra_arguments if postgresql? assert_unsafe AddForeignKeyExtraArguments - elsif ActiveRecord::VERSION::MAJOR >= 6 - assert_argument_error AddForeignKeyExtraArguments else - assert_type_error AddForeignKeyExtraArguments + assert_argument_error AddForeignKeyExtraArguments end end - - def ar_bug? - ActiveRecord::VERSION::STRING.start_with?("6.0.3") - end end diff --git a/test/add_index_test.rb b/test/add_index_test.rb index af5e99d9..90cdddaf 100644 --- a/test/add_index_test.rb +++ b/test/add_index_test.rb @@ -73,10 +73,8 @@ def test_auto_analyze def test_extra_arguments if postgresql? assert_unsafe AddIndexExtraArguments - elsif ActiveRecord::VERSION::STRING.to_f != 6.0 - assert_argument_error AddIndexExtraArguments else - assert_type_error AddIndexExtraArguments + assert_argument_error AddIndexExtraArguments end end diff --git a/test/change_column_null_test.rb b/test/change_column_null_test.rb index e37f9e18..8934f501 100644 --- a/test/change_column_null_test.rb +++ b/test/change_column_null_test.rb @@ -46,7 +46,7 @@ def test_default end def test_constraint_methods - skip unless postgresql? && check_constraints? + skip unless postgresql? with_target_version(12) do assert_safe ChangeColumnNullConstraintMethods diff --git a/test/multiple_databases_test.rb b/test/multiple_databases_test.rb index 6753722e..93ff2126 100644 --- a/test/multiple_databases_test.rb +++ b/test/multiple_databases_test.rb @@ -2,7 +2,7 @@ class MultipleDatabasesTest < Minitest::Test def test_target_version - skip unless multiple_dbs? && postgresql? + skip unless postgresql? with_target_version({primary: 10, animals: 11}) do with_database(:primary) do @@ -15,8 +15,6 @@ def test_target_version end def test_target_version_unconfigured - skip unless multiple_dbs? - error = assert_raises(StrongMigrations::Error) do with_target_version({primary: 10}) do with_database(:animals) do @@ -27,17 +25,6 @@ def test_target_version_unconfigured assert_equal "StrongMigrations.target_version is not configured for :animals database", error.message end - def test_target_version_unsupported - skip if multiple_dbs? - - with_target_version({primary: 10, animals: 15}) do - error = assert_raises(StrongMigrations::Error) do - assert_safe AddColumnDefault - end - assert_equal "StrongMigrations.target_version does not support multiple databases for Active Record < 6.1", error.message - end - end - private def with_database(database, &block) @@ -56,8 +43,4 @@ def with_database(database, &block) ActiveRecord::Base.configurations = previous_configurations if previous_configurations ActiveRecord::Base.establish_connection(previous_db_config) if previous_db_config end - - def multiple_dbs? - ActiveRecord::VERSION::STRING.to_f >= 6.1 - end end diff --git a/test/remove_index_test.rb b/test/remove_index_test.rb index 1bb35948..01effaed 100644 --- a/test/remove_index_test.rb +++ b/test/remove_index_test.rb @@ -7,14 +7,8 @@ def test_concurrently begin StrongMigrations.enable_check(:remove_index) - if ActiveRecord::VERSION::STRING.to_f >= 6.1 - assert_unsafe RemoveIndex, "remove_index :users, :name, algorithm: :concurrently" - assert_unsafe RemoveIndexExtraArguments, "remove_index :users, :name, algorithm: :concurrently" - else - # cannot pass column argument and options together in Active Record < 6.1 - assert_unsafe RemoveIndex, "remove_index :users, column: :name, algorithm: :concurrently" - assert_unsafe RemoveIndexExtraArguments, "remove_index :users, column: :name, algorithm: :concurrently" - end + assert_unsafe RemoveIndex, "remove_index :users, :name, algorithm: :concurrently" + assert_unsafe RemoveIndexExtraArguments, "remove_index :users, :name, algorithm: :concurrently" assert_unsafe RemoveIndexColumn assert_unsafe RemoveIndexName migrate RemoveIndexConcurrently diff --git a/test/safe_by_default_test.rb b/test/safe_by_default_test.rb index de6e1e8a..a6830738 100644 --- a/test/safe_by_default_test.rb +++ b/test/safe_by_default_test.rb @@ -14,11 +14,7 @@ def test_add_index end def test_add_index_extra_arguments - if postgresql? || ActiveRecord::VERSION::STRING.to_f != 6.0 - assert_argument_error AddIndexExtraArguments - else - assert_type_error AddIndexExtraArguments - end + assert_argument_error AddIndexExtraArguments end def test_add_index_corruption @@ -45,8 +41,6 @@ def test_remove_index_name end def test_remove_index_options - skip if ActiveRecord::VERSION::STRING.to_f < 6.1 - migrate RemoveIndexOptions end @@ -79,16 +73,10 @@ def test_add_foreign_key end def test_add_foreign_key_extra_arguments - if postgresql? || ActiveRecord::VERSION::MAJOR >= 6 - assert_argument_error AddForeignKeyExtraArguments - else - assert_type_error AddForeignKeyExtraArguments - end + assert_argument_error AddForeignKeyExtraArguments end def test_add_foreign_key_name - skip if ActiveRecord::VERSION::MAJOR < 6 - migrate AddForeignKeyName foreign_keys = ActiveRecord::Schema.foreign_keys(:users) assert_equal 2, foreign_keys.size @@ -101,8 +89,6 @@ def test_add_foreign_key_name end def test_add_foreign_key_column - skip if ActiveRecord::VERSION::MAJOR < 6 - migrate AddForeignKeyColumn foreign_keys = ActiveRecord::Schema.foreign_keys(:users) assert_equal 2, foreign_keys.size @@ -115,13 +101,13 @@ def test_add_foreign_key_column end def test_add_check_constraint - skip unless check_constraints? && postgresql? + skip unless postgresql? assert_safe AddCheckConstraint end def test_add_check_constraint_extra_arguments - skip unless check_constraints? && postgresql? + skip unless postgresql? assert_argument_error AddCheckConstraintExtraArguments end diff --git a/test/test_helper.rb b/test/test_helper.rb index c7c8cea3..83ee270a 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -134,10 +134,8 @@ def migrate(migration, direction: :up, version: 123) args = if ActiveRecord::VERSION::STRING.to_f >= 7.1 [schema_migration, connection_class.internal_metadata] - elsif ActiveRecord::VERSION::MAJOR >= 6 - [schema_migration] else - [] + [schema_migration] end ActiveRecord::Migrator.new(direction, [migration], *args).migrate true @@ -169,12 +167,6 @@ def assert_argument_error(migration) end end - def assert_type_error(migration) - assert_raises(TypeError) do - migrate(migration) - end - end - def with_target_version(version) StrongMigrations.target_version = version yield @@ -193,10 +185,6 @@ def outside_developer_env yield end end - - def check_constraints? - ActiveRecord::VERSION::STRING.to_f >= 6.1 - end end StrongMigrations.add_check do |method, args|