Skip to content

Commit

Permalink
Fixed safety_assured with revert
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Sep 8, 2023
1 parent fa43564 commit fec19f3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.6.2 (unreleased)

- Fixed `safety_assured` with `revert`

## 1.6.1 (2023-08-09)

- Fixed `safety_assured` for custom checks with `safe_by_default`
Expand Down
15 changes: 9 additions & 6 deletions lib/strong_migrations/checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,25 @@ class Checker

attr_accessor :direction, :transaction_disabled, :timeouts_set

class << self
attr_accessor :safe
end

def initialize(migration)
@migration = migration
@new_tables = []
@new_columns = []
@safe = false
@timeouts_set = false
@committed = false
end

def safety_assured
previous_value = @safe
def self.safety_assured
previous_value = safe
begin
@safe = true
self.safe = true
yield
ensure
@safe = previous_value
self.safe = previous_value
end
end

Expand Down Expand Up @@ -160,7 +163,7 @@ def check_lock_timeout
end

def safe?
@safe || ENV["SAFETY_ASSURED"] || (direction == :down && !StrongMigrations.check_down) || version_safe?
self.class.safe || ENV["SAFETY_ASSURED"] || (direction == :down && !StrongMigrations.check_down) || version_safe?
end

def version_safe?
Expand Down
2 changes: 1 addition & 1 deletion lib/strong_migrations/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def method_missing(method, *args)
ruby2_keywords(:method_missing) if respond_to?(:ruby2_keywords, true)

def safety_assured
strong_migrations_checker.safety_assured do
strong_migrations_checker.class.safety_assured do
yield
end
end
Expand Down
6 changes: 6 additions & 0 deletions test/migrations/misc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ def change
end
end

class RevertAddReference < TestMigration
def change
safety_assured { revert AddReferenceNoIndex }
end
end

class Custom < TestMigration
def change
add_column :users, :forbidden, :string
Expand Down
5 changes: 5 additions & 0 deletions test/misc_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ def test_create_join_table_force
assert_unsafe CreateJoinTableForce
end

def test_revert
migrate AddReferenceNoIndex
migrate RevertAddReference
end

def test_custom
assert_unsafe Custom, "Cannot add forbidden column"
end
Expand Down

0 comments on commit fec19f3

Please sign in to comment.