Skip to content

Allow db:prepare to load schema if database already exists but is empty; also dumps schema after migrations - #45464

Merged
eileencodes merged 1 commit into
rails:mainfrom
bensheldon:db_prepare_for_paas
Jun 28, 2022
Merged

eileencodes merged 1 commit into
rails:mainfrom
bensheldon:db_prepare_for_paas

Conversation

@bensheldon

@bensheldon bensheldon commented Jun 26, 2022 •

Copy link
Copy Markdown
Contributor

Summary

Rails 6.0 introduced a db:prepare rake task that idempotently sets up a database (creating the database, loading schema, and/or migrations).

Existing Behavior: If the database exists, but has not been populated with tables, db:prepare will run all migrations to bring the database up to state.

Proposed Behavior in this PR: if the database exists, but has not been populated with tables, db:prepare will load the schema, then run any remaining migrations, to bring the database up to state.

Why?

Platforms as a Service, like Heroku, Render, DOAP, Fly, etc. provision an empty database for the application on initial setup, which can take place regularly with "1-click deploy"-features, Heroku Review Apps, or Render Preview Environments, for example. Running the existing db:prepare in this situation will cause all of the migrations to run. Loading the schema instead is the more correct and efficient action. Also, for large/legacy applications that prune migration files, running all migrations may not produce the desired end state.

I believe that this PR does not deviate from the original purpose of the db:prepare task, but simply covers a not-uncommon scenario.

How?

This PR uses the value of ActiveRecord::SchemaMigration.table_exists? to determine if the database has been populated with tables.

Outstanding work

  • Tests passing
  • Add test for new behavior
  • Add changelog entry

@eileencodes eileencodes left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the PR!

There is a Railties test failing related to this change https://buildkite.com/rails/rails/builds/87628#0181a0c1-e8e0-4289-8012-45d0e90f83d0/1215-1223. Can you take a look?

In the same file that has the failing test, we should add a new test for the behavior added here. I also think this should have a changelog entry in active record's CHANGELOG.

@bensheldon
bensheldon force-pushed the db_prepare_for_paas branch from 14eab93 to 45abf03 Compare June 27, 2022 18:25
@rails-bot rails-bot Bot added the railties label Jun 27, 2022
@bensheldon
bensheldon force-pushed the db_prepare_for_paas branch from 45abf03 to c1d76d3 Compare June 27, 2022 20:54
@bensheldon bensheldon changed the title Allow db:prepare to load schema if database already exists but is empty Allow db:prepare to load schema if database already exists but is empty; also dumps schema after migrations Jun 27, 2022
@bensheldon
bensheldon force-pushed the db_prepare_for_paas branch 2 times, most recently from 5da85e6 to 367e665 Compare June 27, 2022 21:45

@ghiculescu ghiculescu Jun 27, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the syntax error here was intentional. The test should raise if the file is ever required, loaded, or eval'd. See #36744, "this is causing issues in environments with limited file permissions that are preparing database."

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@morgoth correct me if I'm wrong!

@bensheldon bensheldon Jun 27, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think touch is a bit ambiguous, but my reading of #36744 is to avoid writing ("dumping") to the schema.rb file, not to avoid reading from it.

Even in that #36744, the behavior would still load the schema file, but only if the database didn't exist. Which was not a scenario covered in the test cases.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

My intention was only to test if the file content is preserved.

@bensheldon
bensheldon force-pushed the db_prepare_for_paas branch from 367e665 to e5c3e97 Compare June 27, 2022 21:54
@bensheldon
bensheldon force-pushed the db_prepare_for_paas branch from e5c3e97 to 660fee0 Compare June 28, 2022 13:36
@eileencodes
eileencodes merged commit 7ebda0a into rails:main Jun 28, 2022
@bensheldon
bensheldon deleted the db_prepare_for_paas branch June 28, 2022 22:11
@natematykiewicz

natematykiewicz commented Dec 8, 2022 •

Copy link
Copy Markdown
Contributor

This is awesome! I was wishing I had this today on Rails 7.0.4, when using Render's Preview Environments (an environment per PR), for the exact reasons mentioned. I assume this will land in 7.1?

@bensheldon

Copy link
Copy Markdown
Contributor Author

@natematykiewicz yep, this should be part of the next release, 7.1. Or you can run off of Rails trunk, which is stable and increasingly common (and trying to make it more common and thus even more stable).

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.

5 participants