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
rake db:migrate:status emits an exit status of 2 when migrations are required #46434
base: main
Are you sure you want to change the base?
Conversation
Hi @gee-forr thanks for the PR and welcome to Rail!
I'd like to better understand why you need this. Why aren't you able to run |
Hey @eileencodes, long time listener, first time caller :)
This may be more specific to the way Ansible works, but I can see a use case for knowing whether or not to run a migrate, vs. blindly running it every deploy. When an Ansible playbook runs, it provides a summary of changed elements upon completion. We currently do a regex on If we blindly ran a migrate every time, we also wouldn't be able to detect if the migration ran anything because it also doesn't provide an exit status, putting us back at square 1. We're also moving over to K8S, and having This is pretty much the same energy as what Dave Copeland is doing here with Also just ran some benchmarks... for a project with a fair number (200+) migrations, running a noop
|
…ected to raise non-zero exit code
Hey @eileencodes - I know you're hella busy, but I was wondering if you've had some time to go over my justification for having |
Hi @eileencodes - happy new year... I hope you had a relaxing festive season? Please let me know if there's anything I can do to get this PR over the finish line? |
# Conflicts: # activerecord/lib/active_record/tasks/database_tasks.rb
To be honest, I don't like the implementation but I don't have another suggestion at the moment. So I'm not sure what to tell you to work on - it just feels too complex for a simple status check. It also changes the behavior of applications and that might be surprising / undesirable. |
Hey @eileencodes - thanks for your feedback :) I totally see your point... as an alternative, would you be amenable to the following:
This will enable the following workflow: rails db:migrate:check || rails db:migrate This ☝🏻 approach has utility outside of just my Ansible use case above. Container entrypoints that include a migrate prior to boot would save time starting up, and k8s migration Jobs would also execute faster, as migrations - in my experience at least - happen way less per deploy compared to vanilla code changes. Let me know :) And once again - thanks for the feedback, I appreciate you taking time out to respond to me. |
Motivation / Background
This Pull Request has been created because previously during deployments I would need to write a hairy grep on the output of
rails db:migrate:status
to determine if I needed to run a migration. I've taken inspiration frombundle check
which outputs a status code of1
if abundle install
is required.Now, when running a
rails db:migrate:status
, people can check the exit status, and make a decision to migrate or not based on that value.Detail
This Pull Request changes the rake task
db:migrate:status
found inactiverecord/lib/active_record/tasks/database_tasks.rb
Now, as part of rendering the list of migrations, it checks if any migrations are needed. If so, it will exit with a status code of
2
. No migrations required will exit as normal with a status code of0
. There is an abort check before the task runs which will exit with a status code of1
, which is why I elected to use2
.Additional information
Here's how I was checking for outstanding migrations previously with Ansible:
This can now be simplified to:
Checklist
I could not find the documentation for this, and I'm not sure if this requires a CHANGELOG entry. Please let me know and I'll update.
Please also let me know if I must backport these changes to 6.1.x and 6.0.x?
[Fix #issue-number]