-
Notifications
You must be signed in to change notification settings - Fork 7
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
rollback migration cause data loss #15
Comments
@OuYangJinTing thanks for adding your issue. Can you please specify your real scenario? That would allow me to think of a concrete fix rather than implementing an abstract solution. Thank you! |
@ka8725 The actual scenario is as follows: git checkout -b feat main
rails generate model Feat name:string
rails db:migrate # create feats table
rails runner 'Feat.create(name: :test)'
git add -A && git commit -m 'stash'
git checkout -b other main
rails generate model Other name:string
rails db:migrate # create others table and drop feats table
git add -A && git commit -m 'stash'
git checkout feat
rails db:migrate # create feats table and drop others table
rails runner 'puts Feat.count' # ==> 0 |
@OuYangJinTing if the original migration had the seeding data populated automatically would it work for you? There is a solution for seeding data within migrations - https://github.com/ka8725/migration_data This is what you could do after installing this gem:
It might sound too annoying. But, I don't think it's possible to implement something that also would have the data not touched. Even if implement automatic dumping data on rolling migration back and later reinstating this data, there is no guarantee this data will be valid. There are many reasons why it could be invalid:
So, my recommendation is seeding data inside migrations for dev purposes. Or handle these cases manually. If you have a better idea, I'm open for discussion. Thank you! |
@ka8725 Thank for you reply. the migration_data is good. |
@OuYangJinTing WDYT about this approach: when it rolls back the first migration automatically, it makes the DB dump behind the scene. So that on each run of
I thought about DB dump diff, that would probably work in case of dropping/restoring the table and its data. But when it comes to dropping/restoring columns - again there is a problem. It can't generate column data for new rows automatically. This way, IMO the issue is reduced to proper implementation of DB dump diff. I'm not sure if there is any, I'm researching the topic. |
@ka8725 Sorry. I just saw this comment today. I'm a little confused that users table has always existed, why the data is lost? |
@OuYangJinTing because the solution implied to have full DB dump of rollback. If there is a solution that can generate a DB data diff, it would solve the problem. |
@ka8725 I think the solution may be too complex, maybe it's better to simply give a warm prompt and ask whether to continue rollback. What do you think? |
@OuYangJinTing is a good suggestion, I like it. Also, it actually could be configurable so that someone can set it up to rollback always without confirmation. Also, it could check if a deleting table/column really has data in there. |
As the title, it destroy data when happen migration rollback.
Can the data be automatically exported before rollback, and then imported back when migrating again?
PS: English is not my native language; please excuse typing errors.
The text was updated successfully, but these errors were encountered: