Repository navigation
Allow db:prepare to load schema if database already exists but is empty; also dumps schema after migrations - #45464
Conversation
eileencodes
left a comment
There was a problem hiding this comment.
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.
14eab93 to
45abf03
Compare
45abf03 to
c1d76d3
Compare
db:prepare to load schema if database already exists but is emptydb:prepare to load schema if database already exists but is empty; also dumps schema after migrations
5da85e6 to
367e665
Compare
There was a problem hiding this comment.
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."
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
My intention was only to test if the file content is preserved.
367e665 to
e5c3e97
Compare
…ty; also dumps schema after migrations
e5c3e97 to
660fee0
Compare
|
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? |
|
@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). |
Summary
Rails 6.0 introduced a
db:preparerake 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:preparewill 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:preparewill 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:preparein 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:preparetask, 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