-
Notifications
You must be signed in to change notification settings - Fork 21.9k
Add migrations_priority to database.yml #41538
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
Conversation
Thanks for the PR @dsounded! What's a use case where you need to specify the order? What order do they run in now - is it by timestamp or by database order in the yaml configuration? I think that the feature is interesting, but if they already run in order it's defined, why not change the order of the database yaml instead of adding a new option? |
@eileencodes the use case is: sometimes the primary db depends on the stuff from secondary, or, we need to transfer the data from primary to secondary and then remove if from primary. Checked it and it really depends on the order in the config file. Maybe it's only about some readability enhancement, having the option it will be easier to put Or at least it would be good to document |
@@ -158,7 +158,7 @@ def env_with_configs(env = nil) | |||
configurations.select { |db_config| db_config.env_name == env } | |||
else | |||
configurations | |||
end | |||
end.sort_by!(&:migrations_priority) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this mutate the list?
end.sort_by!(&:migrations_priority) | |
end.sort_by(&:migrations_priority) |
Seems odd to me to bake in an order, when it seems like that'll be migration dependent. Versus having a script to do something like: # script/migrate/particular_migration.sh
bin/rails db:migrate:secondary
bin/rails db:migrate:primary # primary depends on new column in secondary. I'd probably also supply particular |
I don't think this is the right API to solve the problem but you are right @dsounded, there is a flaw that I never considered in how the migrations work for multiple databases. At GitHub we don't have this problem because we haven't yet ported our multi-db migration handling over from the legacy db handling to the Rails way. If an application has 2 databases and there's 1 migration to copy data from I think the right way to solve this is not to add a priority that you have to change if the migration priority changes but rather to run all the migrations in order as if they are in the same directory. I think that we should run all migrations in timestamp order regardless of their directory - this would prevent the above issue. I'm not sure how easy this kind of change would be - the way connections work with the rake tasks get a little complex. I'll have to think on this a bit. |
@eileencodes makes sense, I guess this change also won't be hard to implement |
@eileencodes Hey Just wanted to tweak the stuff locally, I created a commit which is 100% not production ready or something but shares the idea that it can be done like you described. https://github.com/dsounded/rails/commit/012779a3ea20a7fb84e466bf18c940304722b09b I am not sure if there is more efficient way since we do need to flip connections time to time (tried to decrease the needed amount of flips). |
The console output of example:
|
Can you replace this PR with that commit or open a new PR? That new approach is going in the right direction and is close to what I was thinking. I do think it can be simplified a bit but will be easier to comment on in a PR 😄 |
@eileencodes yeah, sure, will do |
Previously if there were 2 migrations in one db and 1 migration in the other db all the migrations for db one would run and then all migrations for db two would run. If a migration in one database depended on a migration in another database then it could fail. This is probably pretty rare, however in a multi-db application that's moving tables from one db to another, running them out of order could result in a migration error. In this this change we collect all the versions for each migration and the corresponding db_config so we can run them in the order they are created rather than per-db. Closes rails#41664 Related rails#41538 Co-authored-by: John Crepezzi <john.crepezzi@gmail.com> Co-authored-by: Kiril Dokh <dsounded@gmail.com>
Summary
Basically sometimes we need to specify the order for multiple database migrations, we can do that only with consecutive calls e.g.
rake db:migrate:secondary rake db:migrate:primary ... rake db:migrate:nth
(since positions in theyml
file are ignored I believe)But this is not always handy.
This requests brings new option for our
database.yml
calledmigrations_priority
which will determine which db should migrate in which order.By default all the priority set to 0, so it shouldn't affect previous behavior.