Skip to content

Commit

Permalink
Extract migration_path method
Browse files Browse the repository at this point in the history
Changed the behavior as require_migration expected to be called from
the spec, but this method could be called from the helper or elsewhere.
Therefore, we detect the first caller location with the spec instead of assuming
it's the first one.
  • Loading branch information
jrafanie committed Jan 3, 2025
1 parent 7af2420 commit 0f2cd76
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

allow(ActiveRecord::Base.connection_db_config).to receive(:configuration_hash).and_wrap_original do |original_method, *args, &block|
# set a value for any calls originating from the migration file, not from rails itself
original_method.call(*args, &block).dup.tap {|i| i[:password] ||= "abc" if caller_locations.any? {|loc| loc.path.include?("db/migrate/20241017013023_reencrypt_password_scramsha.rb")} }
original_method.call(*args, &block).dup.tap { |i| i[:password] ||= "abc" if caller_locations.any? {|loc| loc.path.include?(migration_path)} }
end
expect(ActiveRecord::Base.connection).to receive(:execute).with(a_string_matching(/ALTER ROLE #{username} WITH PASSWORD \'SCRAM-SHA-256.*\'\;/))

Expand All @@ -25,7 +25,7 @@

allow(ActiveRecord::Base.connection_db_config).to receive(:configuration_hash).and_wrap_original do |original_method, *args, &block|
# set a value for any calls originating from the migration file, not from rails itself
original_method.call(*args, &block).dup.tap { |i| i.delete(:password) if caller_locations.any? {|loc| loc.path.include?("db/migrate/20241017013023_reencrypt_password_scramsha.rb")} }
original_method.call(*args, &block).dup.tap { |i| i.delete(:password) if caller_locations.any? {|loc| loc.path.include?(migration_path)} }
end
expect(ActiveRecord::Base.connection).not_to receive(:execute).with(a_string_matching(/ALTER ROLE.*\'\;/))

Expand Down
8 changes: 5 additions & 3 deletions spec/support/migration_name_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
def require_migration
spec_name = caller_locations.first.path
migration_name = spec_name.sub("spec/migrations", "db/migrate").sub("_spec.rb", ".rb")
require migration_path
end

require migration_name
def migration_path
spec_name = caller_locations.detect {|loc| loc.path.end_with?("_spec.rb")}.path
spec_name.sub("spec/migrations", "db/migrate").sub("_spec.rb", ".rb")
end

0 comments on commit 0f2cd76

Please sign in to comment.