Skip to content

Commit

Permalink
Dropped support for Ruby < 3 and Active Record < 6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Jun 17, 2024
1 parent b42ddba commit 5523449
Show file tree
Hide file tree
Showing 17 changed files with 23 additions and 152 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 0 additions & 9 deletions gemfiles/activerecord52.gemfile

This file was deleted.

9 changes: 0 additions & 9 deletions gemfiles/activerecord60.gemfile

This file was deleted.

6 changes: 1 addition & 5 deletions lib/generators/strong_migrations/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
8 changes: 0 additions & 8 deletions lib/strong_migrations/adapters/abstract_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 3 additions & 26 deletions lib/strong_migrations/checks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,38 +249,21 @@ 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
safe_change_column_null(add_code, validate_code, change_args, remove_code, default)
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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 2 additions & 10 deletions lib/strong_migrations/safe_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions strong_migrations.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 0 additions & 5 deletions test/add_check_constraint_test.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 1 addition & 13 deletions test/add_foreign_key_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
4 changes: 1 addition & 3 deletions test/add_index_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/change_column_null_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 1 addition & 18 deletions test/multiple_databases_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
10 changes: 2 additions & 8 deletions test/remove_index_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 4 additions & 18 deletions test/safe_by_default_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
14 changes: 1 addition & 13 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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|
Expand Down

0 comments on commit 5523449

Please sign in to comment.