Skip to content

Commit

Permalink
Merge pull request #776 from jrafanie/fix-rails-7-1-plus-test-failure
Browse files Browse the repository at this point in the history
Allow the rails method to be called and change the result only when called from the migration
  • Loading branch information
Fryguy authored Jan 3, 2025
2 parents d08bbf3 + 0f2cd76 commit a40802b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ jobs:
run: bin/setup
- name: Run tests
run: bundle exec rake
continue-on-error: ${{ matrix.rails-version == '7.2' || matrix.rails-version == '7.1'}}
- name: Report code coverage
if: ${{ github.ref == 'refs/heads/master' && matrix.ruby-version == '3.3' && matrix.rails-version == '7.0' }}
continue-on-error: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

username = ActiveRecord::Base.connection_db_config.configuration_hash[:username]

expect(ActiveRecord::Base.connection_db_config).to receive(:configuration_hash).exactly(10).times.and_call_original
expect(ActiveRecord::Base.connection_db_config).to receive(:configuration_hash).and_wrap_original do |original_method, *args, &block|
original_method.call(*args, &block).dup.tap { |i| i[:password] ||= "abc" }
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?(migration_path)} }
end
expect(ActiveRecord::Base.connection).to receive(:execute).with(a_string_matching(/ALTER ROLE #{username} WITH PASSWORD \'SCRAM-SHA-256.*\'\;/))

Expand All @@ -23,9 +23,9 @@

username = ActiveRecord::Base.connection_db_config.configuration_hash[:username]

expect(ActiveRecord::Base.connection_db_config).to receive(:configuration_hash).exactly(10).times.and_call_original
expect(ActiveRecord::Base.connection_db_config).to receive(:configuration_hash).and_wrap_original do |original_method, *args, &block|
original_method.call(*args, &block).dup.tap { |i| i.delete(:password) }
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?(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 a40802b

Please sign in to comment.