Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if internal metadata is enabled in create_table_and_set_flagsmethod #49551

Merged
merged 1 commit into from Oct 9, 2023

Conversation

haines
Copy link
Contributor

@haines haines commented Oct 9, 2023

Motivation / Background

I maintain a gem (pg-aws_rds_iam) that runs acceptance tests against supported versions of Active Record. When I added 7.1 to the test matrix, my test failed with the following error:

ERROR AcceptanceTest#test_active_record_load_schema (1.77s)
Minitest::UnexpectedError: ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "ar_internal_metadata" does not exist
  LINE 1: SELECT * FROM "ar_internal_metadata" WHERE "ar_internal_meta...
                        ^

  gems/activerecord-7.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:897:in `exec_params'
  gems/activerecord-7.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:897:in `block (2 levels) in exec_no_cache'
  gems/activerecord-7.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:1024:in `block in with_raw_connection'
  gems/activesupport-7.1.0/lib/active_support/concurrency/null_lock.rb:9:in `synchronize'
  gems/activerecord-7.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:996:in `with_raw_connection'
  gems/activerecord-7.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:896:in `block in exec_no_cache'
  gems/activesupport-7.1.0/lib/active_support/notifications/instrumenter.rb:58:in `instrument'
  gems/activerecord-7.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:1134:in `log'
  gems/activerecord-7.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:895:in `exec_no_cache'
  gems/activerecord-7.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:875:in `execute_and_clear'
  gems/activerecord-7.1.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:61:in `internal_exec_query'
  gems/activerecord-7.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:628:in `select'
  gems/activerecord-7.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:71:in `select_all'
  gems/activerecord-7.1.0/lib/active_record/connection_adapters/abstract/query_cache.rb:114:in `select_all'
  gems/activerecord-7.1.0/lib/active_record/internal_metadata.rb:145:in `select_entry'
  gems/activerecord-7.1.0/lib/active_record/internal_metadata.rb:97:in `update_or_create_entry'
  gems/activerecord-7.1.0/lib/active_record/internal_metadata.rb:68:in `create_table_and_set_flags'
  gems/activerecord-7.1.0/lib/active_record/tasks/database_tasks.rb:365:in `load_schema'
  test/acceptance/test.rb:39:in `block in test_active_record_load_schema'

Detail

It seems that in 7.1 there is a new method, ActiveRecord::InternalMetadata#create_table_and_set_flags, which does not respect the use_metadata_table: false setting on the connection.

https://github.com/rails/rails/blob/v7.1.0/activerecord/lib/active_record/internal_metadata.rb#L66-L70

create_table_and_set_flags calls create_table (which is a no-op if the metadata table is disabled) and then update_or_create_entry (which assumes that the metadata table exists).

This PR adds a guard clause to create_table_and_set_flags to match the other methods in InternalMetadata.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature. - I'm not sure where or how to add a test for this fix
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@eileencodes eileencodes merged commit f11c651 into rails:main Oct 9, 2023
4 checks passed
@eileencodes
Copy link
Member

Thanks for the PR @haines!

eileencodes added a commit that referenced this pull request Oct 9, 2023
Check if internal metadata is enabled in `create_table_and_set_flags`method
@eileencodes
Copy link
Member

Backported to 7-1-stable in 36a3c68, it will be included in the next release of 7.1. Please point your gemfile at the 7-1-stable branch if you need this change ASAP.

@haines haines deleted the fix-internal-metadata-disabled branch October 9, 2023 16:11
@haines
Copy link
Contributor Author

haines commented Oct 9, 2023

Awesome, thanks @eileencodes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants