Skip to content

Comments

Load schema when running db:migrate on fresh db#52830

Merged
rafaelfranca merged 1 commit intorails:mainfrom
Shopify:load-schema-on-migrating-new-database
Sep 11, 2024
Merged

Load schema when running db:migrate on fresh db#52830
rafaelfranca merged 1 commit intorails:mainfrom
Shopify:load-schema-on-migrating-new-database

Conversation

@andrewn617
Copy link
Member

@andrewn617 andrewn617 commented Sep 7, 2024

Motivation / Background

Fixes #52829

When we have a schema file, if we run db:migrate before setting up the db, we do not load the schema and then we blow away the contents of the file when we dump the schema after migrations.

You can repro by creating a fresh app and running the following commands:

bin/rails g model Post
bin/rails db:migrate
rm -rf db/migrate
bin/rails db:drop
bin/rails db:migrate

After following these steps, the schema file will be empty and the posts table will not exist.

Detail

We should make sure the db is setup before we run migrations. This is what we do in the db:prepare task. So, I extracted the code from that task and reused it in the migration task.

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.

@dhh dhh requested a review from rafaelfranca September 7, 2024 23:04
@andrewn617 andrewn617 force-pushed the load-schema-on-migrating-new-database branch from bbbe7c0 to 1c28120 Compare September 7, 2024 23:04
@@ -179,20 +179,9 @@ def prepare_all

each_current_configuration(env) do |db_config|
with_temporary_pool(db_config) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice initialize_database already has with_temporary_pool, so it looks like we're nesting temporary pools. Is it safe to remove it here?

@rafaelfranca rafaelfranca force-pushed the load-schema-on-migrating-new-database branch from 1c28120 to b696583 Compare September 11, 2024 16:06
… before running migrations.

If we have an existing schema file and we run db:migrate before setting up the database, we will not load the schema before running migrations, so when we dump the schema will overwrite the existing schema file and lose the tables define there. Instead, we should check if the db is setup and if it is not lload the schema, before running migrations.
@rafaelfranca rafaelfranca force-pushed the load-schema-on-migrating-new-database branch from b696583 to 66aacb6 Compare September 11, 2024 16:07
@rafaelfranca rafaelfranca merged commit 95e3901 into rails:main Sep 11, 2024
@rafaelfranca rafaelfranca deleted the load-schema-on-migrating-new-database branch September 11, 2024 17:48
mkasztelnik added a commit to mkasztelnik/rails that referenced this pull request Oct 15, 2024
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.
mkasztelnik added a commit to mkasztelnik/rails that referenced this pull request Oct 15, 2024
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.
mkasztelnik added a commit to mkasztelnik/rails that referenced this pull request Oct 15, 2024
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
mkasztelnik added a commit to mkasztelnik/rails that referenced this pull request Oct 15, 2024
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
mkasztelnik added a commit to mkasztelnik/rails that referenced this pull request Oct 16, 2024
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
mkasztelnik added a commit to mkasztelnik/rails that referenced this pull request Oct 16, 2024
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
mkasztelnik added a commit to mkasztelnik/rails that referenced this pull request Oct 16, 2024
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
mkasztelnik added a commit to mkasztelnik/rails that referenced this pull request Oct 18, 2024
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 pushed a commit that referenced this pull request Oct 28, 2024
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
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 pushed a commit that referenced this pull request Oct 28, 2024
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
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.

Second-db schema is overwritten erroneously when running db:migrate

3 participants