Skip to content

Comments

Load schema when running db:migrate on fresh dbs#53320

Merged
jeremy merged 3 commits intorails:mainfrom
mkasztelnik:load-schema-on-migrating-multiple-new-databases
Oct 28, 2024
Merged

Load schema when running db:migrate on fresh dbs#53320
jeremy merged 3 commits intorails:mainfrom
mkasztelnik:load-schema-on-migrating-multiple-new-databases

Conversation

@mkasztelnik
Copy link
Contributor

Motivation / Background

Loading database schemas before db:migrate for a single database was fixed in #52830. However, this approach fails when multiple databases are defined. This fix addresses the issue by moving schema loading to occur before initiating database migrations.

Detail

db:migrate has different logic when single or multiple databases are defined (https://github.com/rails/rails/blob/main/activerecord/lib/active_record/railties/databases.rake#L92-L102). When multiple DBs are defined, migration method is invoked only for the DBs with pending migrations. The fix moves DBs schema load for new DBs before DBs migrations are invoked.

Additional information

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes 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.
  • 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.

@mkasztelnik mkasztelnik force-pushed the load-schema-on-migrating-multiple-new-databases branch 2 times, most recently from 2eab749 to e12a3b9 Compare October 16, 2024 07:06
@excid3
Copy link
Contributor

excid3 commented Oct 16, 2024

Ran into this today and this seemed to fix it for me. 👍

Loading database schemas before db:migrate for a single database was
fixed in rails#52830. However, this approach fails when multiple databases
are defined. This fix addresses the issue by moving schema loading to
occur before initiating database migrations.

Fixes rails#52829
jeremy added a commit that referenced this pull request Oct 28, 2024
Backport #53320 to 8-0-stable.

Loading database schemas before db:migrate for a single database was
fixed in #52830. However, this approach fails when multiple databases
are defined. This fix addresses the issue by moving schema loading to
occur before initiating database migrations.

Fixes #52829

Co-authored-by: Marek Kasztelnik <[email protected]>
@jeremy jeremy merged commit ecbdfbc into rails:main Oct 28, 2024
@mkasztelnik mkasztelnik deleted the load-schema-on-migrating-multiple-new-databases branch October 29, 2024 08:07
mnordin added a commit to mnordin/webhook-deliverer that referenced this pull request Nov 4, 2024
I discovered that rake db:drop will cause issues when re-creating the
databases on a multi database config, causing the queue schema not to
load its migrations and we end up with an empty database.

This is not affected by SolidQueues migrations being faulty, it seems to
be related to the multi database setups only running the migrations for
the primary database. I discovered this while playing around with
SolidCable and decided to backtrack and fix this issue in main branch.

This is due to a bug in Rails: rails/rails#52829
It was fixed by rails/rails#53320 but is part of
Rails main and not 8.0.0.rc2.

Revert this commit when a new release candidate is released including
the rails/rails@ecbdfbc
commit.
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.

4 participants