-
Notifications
You must be signed in to change notification settings - Fork 21.8k
Add a #populate method to migrations #31082
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
Add a #populate method to migrations #31082
Conversation
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @sgrif (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review. Please see the contribution instructions for more information. |
Looks like the MySQL iterations of the test are failing for some reason I can't quite discern. I suspect it's an error in how I've coded the test, rather than an error in the functionality. |
Thank you for the pull request. Although the code is good I'm not too much inclined to advertising doing data migrations in the same migrations as the structure migration. Data migrations are problematic and I'd not do them in the same time I'd doing a structure change. If anything does wrong for example, when using PostgreSQL, your entire migration will be reverted. Other problem is if only part of the records are updated, the migration will not be finished and you will have to edit an existing migration to make it work again. |
@tenderlove @matthewd @jeremy thoughts? |
@rafaelfranca That's an interesting point, although of course in my common use case it's fine because this is primarily used for populating a column that has been created in the same migration (so a rollback will just erase the whole column). Is there a more conventional way to do data migrations? As far as I knew, there's only one kind of migration available in Active Record so I use it for both schema and data. |
Yeah, right now active record don't have any other way to do it. My problem is not with doing it in the same migration as the structure change is being made. At Shopify for example we wrote a new framework to do data migrations to avoid this kind of problem that I mentioned but before this new framework we avoided to do data migrations in the same migration as a structure migration. |
IMO the |
I'm certainly happy to call it |
I fully support doing data migrations in migrations -- if you're rearranging tables & columns that currently have data in them, it is The Right Thing for you to carry their existing data with them as needed, and to correspondingly back-fill new fields, such that they reflect the most reasonable approximation of the state the DB would end up in if the user performed those same operations against the post-migration version of the app. (Or, semi-equivalently, such that they behave in the way most consistent with their pre-migration form -- as in the To my view, the fact that your entire migration will be reverted if something goes wrong is a feature not a problem: unless you have an inordinate amount of data [in which case you probably have a more complex migration story anyway], for the average app, it's ideal that the schema and data are never out of sync. For me the danger is in any encouragement for people to use migrations to seed, which is very different from back-filling a column, but also sounds a lot like "populate". (The distinction is in whether they need to be performed on an empty database, as you'll get from I do agree that the full I like @jeremy's suggestion of an |
@matthewd Thanks for saying this! I was going to say the same thing - I want the whole thing to roll back if it fails, so putting the schema and data in the same migration is essential for me. It seems like people would prefer this was called I don't think we can use the name |
# def change | ||
# add_column :posts, :published, :boolean, default: false | ||
# populate do | ||
# Post.update_all(published: true) |
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.
I think using model directly in migration is fragile since future modification on validation or callback may break this.
If a succeeding migration do drop_table :posts
and app/models/post.rb
is deleted, Post
even does not exist.
How about defining model in place?
class AddPublishedToPosts < ActiveRecord::Migration[5.3]
class Post < ActiveRecord::Base; end
def change
....
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.
That's a good point. Maybe the example and test should use execute
instead? That could be easily done with:
execute "update posts set published = 'true'"
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.
👍
Thanks for all your comments so far! Following the feedback above, I've renamed the method to |
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.
Can you add a CHANGELOG entry?
@rafaelfranca Sure! Done. |
@rafaelfranca Why don't we have |
@bogdanvlviv I see it now, but yesterday my look up was now showing it there. Thanks. |
@@ -1,3 +1,8 @@ | |||
* Add `#only_up` to database migrations for code that is only relevant when |
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.
docs are wrong. function is named up_only
not only_up
cc @rafaelfranca
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.
The method was briefly called #only_up. This line has already been corrected in the latest https://github.com/rails/rails/blob/5-2-stable/activerecord/CHANGELOG.md
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.
@pedantic-git ah, thanks. I guess the release notes are out of date is all
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.
@bf4 The release notes you link to also call it up_only
, as far as I can tell?
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.
¯\_(ツ)_/¯
👿 thanks
This is a feature suggestion, and my first time contributing to the core, so I'm very open to comments about whether this is a good idea, etc.
I often find myself writing migrations that look something like this:
That is, I'm using one half of a
#reversible
block to prepopulate the existing records with appropriate values for the new column. (In the example above, we assume all existing posts are already published, but new posts are unpublished by default.)It doesn't seem very Railsy because it's not really a reversible operation - it's an operation that only happens on the way up and is irrelevant on the way down because the column ceases to exist.
So this PR adds a new
#populate
method for this use case which simplifies the above migration slightly to:Thoughts?