Skip to content

Commit

Permalink
Dropped support for Ruby < 2.6 and Active Record < 5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Jan 17, 2022
1 parent 2c494f2 commit 8a025da
Show file tree
Hide file tree
Showing 11 changed files with 18 additions and 101 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/build.yml
Expand Up @@ -19,12 +19,6 @@ jobs:
- ruby: 2.6
gemfile: gemfiles/activerecord52.gemfile
postgres: true
- ruby: 2.5
gemfile: gemfiles/activerecord51.gemfile
postgres: true
- ruby: 2.4
gemfile: gemfiles/activerecord50.gemfile
postgres: true
- ruby: 3.1
gemfile: Gemfile
adapter: mysql2
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## 0.8.0 (unreleased)

- Dropped support for Ruby < 2.6 and Active Record < 5.2

## 0.7.9 (2021-12-15)

- Fixed error with multiple databases with Active Record 7
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.txt
@@ -1,4 +1,4 @@
Copyright (c) 2013 Bob Remeika and David Waller, 2015-2021 Andrew Kane
Copyright (c) 2013 Bob Remeika and David Waller, 2015-2022 Andrew Kane

MIT License

Expand Down
30 changes: 2 additions & 28 deletions README.md
Expand Up @@ -540,9 +540,7 @@ end

#### Good

Add the foreign key without validating existing rows, then validate them in a separate migration.

For Rails 5.2+, use:
Add the foreign key without validating existing rows:

```ruby
class AddForeignKeyOnUsers < ActiveRecord::Migration[7.0]
Expand All @@ -552,7 +550,7 @@ class AddForeignKeyOnUsers < ActiveRecord::Migration[7.0]
end
```

Then:
Then validate them in a separate migration.

```ruby
class ValidateForeignKeyOnUsers < ActiveRecord::Migration[7.0]
Expand All @@ -562,30 +560,6 @@ class ValidateForeignKeyOnUsers < ActiveRecord::Migration[7.0]
end
```

For Rails < 5.2, use:

```ruby
class AddForeignKeyOnUsers < ActiveRecord::Migration[5.1]
def change
safety_assured do
execute 'ALTER TABLE "users" ADD CONSTRAINT "fk_rails_c1e9b98e31" FOREIGN KEY ("order_id") REFERENCES "orders" ("id") NOT VALID'
end
end
end
```

Then:

```ruby
class ValidateForeignKeyOnUsers < ActiveRecord::Migration[5.1]
def change
safety_assured do
execute 'ALTER TABLE "users" VALIDATE CONSTRAINT "fk_rails_c1e9b98e31"'
end
end
end
```

### Adding a json column

#### Bad
Expand Down
9 changes: 0 additions & 9 deletions gemfiles/activerecord50.gemfile

This file was deleted.

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

This file was deleted.

28 changes: 5 additions & 23 deletions lib/strong_migrations/checker.rb
Expand Up @@ -275,32 +275,14 @@ def perform(method, *args)
from_table, to_table, options = args
options ||= {}

# always validated before 5.2
validate = options.fetch(:validate, true) || ar_version < 5.2
validate = options.fetch(:validate, true)

if postgresql? && validate
if ar_version < 5.2
# fk name logic from rails
primary_key = options[:primary_key] || "id"
column = options[:column] || "#{to_table.to_s.singularize}_id"
hashed_identifier = Digest::SHA256.hexdigest("#{from_table}_#{column}_fk").first(10)
fk_name = options[:name] || "fk_rails_#{hashed_identifier}"
return safe_add_foreign_key(from_table, to_table, options) if StrongMigrations.safe_by_default

add_code = constraint_str("ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s) NOT VALID", [from_table, fk_name, column, to_table, primary_key])
validate_code = constraint_str("ALTER TABLE %s VALIDATE CONSTRAINT %s", [from_table, fk_name])

return safe_add_foreign_key_code(from_table, to_table, add_code, validate_code) if StrongMigrations.safe_by_default

raise_error :add_foreign_key,
add_foreign_key_code: safety_assured_str(add_code),
validate_foreign_key_code: safety_assured_str(validate_code)
else
return safe_add_foreign_key(from_table, to_table, options) if StrongMigrations.safe_by_default

raise_error :add_foreign_key,
add_foreign_key_code: command_str("add_foreign_key", [from_table, to_table, options.merge(validate: false)]),
validate_foreign_key_code: command_str("validate_foreign_key", [from_table, to_table])
end
raise_error :add_foreign_key,
add_foreign_key_code: command_str("add_foreign_key", [from_table, to_table, options.merge(validate: false)]),
validate_foreign_key_code: command_str("validate_foreign_key", [from_table, to_table])
end
when :validate_foreign_key
if postgresql? && writes_blocked?
Expand Down
15 changes: 0 additions & 15 deletions lib/strong_migrations/safe_methods.rb
Expand Up @@ -56,21 +56,6 @@ def safe_add_foreign_key(from_table, to_table, options)
end
end

def safe_add_foreign_key_code(from_table, to_table, add_code, validate_code)
@migration.reversible do |dir|
dir.up do
@migration.safety_assured do
@migration.execute(add_code)
disable_transaction
@migration.execute(validate_code)
end
end
dir.down do
@migration.remove_foreign_key(from_table, to_table)
end
end
end

def safe_add_check_constraint(table, expression, add_options, validate_options)
@migration.reversible do |dir|
dir.up do
Expand Down
4 changes: 2 additions & 2 deletions strong_migrations.gemspec
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.4"
spec.required_ruby_version = ">= 2.6"

spec.add_dependency "activerecord", ">= 5"
spec.add_dependency "activerecord", ">= 5.2"
end
10 changes: 3 additions & 7 deletions test/add_foreign_key_test.rb
Expand Up @@ -12,25 +12,21 @@ def test_add_foreign_key
def test_add_foreign_key_safe
skip "Active Record 6.0.3 bug" if (mysql? || mariadb?) && ActiveRecord::VERSION::STRING.start_with?("6.0.3")

if postgresql? && ActiveRecord::VERSION::STRING <= "5.2"
assert_unsafe AddForeignKeySafe
else
assert_safe AddForeignKeySafe
end
assert_safe AddForeignKeySafe
end

def test_add_foreign_key_validate_same_transaction
skip "Active Record 6.0.3 bug" if ActiveRecord::VERSION::STRING.start_with?("6.0.3")

skip unless postgresql? && ActiveRecord::VERSION::STRING >= "5.2"
skip unless postgresql?

assert_unsafe AddForeignKeyValidateSameTransaction
end

def test_add_foreign_key_validate_no_transaction
skip "Active Record 6.0.3 bug" if ActiveRecord::VERSION::STRING.start_with?("6.0.3")

skip unless postgresql? && ActiveRecord::VERSION::STRING >= "5.2"
skip unless postgresql?

assert_safe AddForeignKeyValidateNoTransaction
end
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Expand Up @@ -100,7 +100,7 @@ def with_target_version(version)
end

def check_constraints?
ActiveRecord::VERSION::STRING >= "6.1"
ActiveRecord::VERSION::STRING.to_f >= 6.1
end
end

Expand Down

0 comments on commit 8a025da

Please sign in to comment.