简体   繁体   English

在运行rake db:migrate VERSION = 0并重新进行迁移之后,我丢失了数据库中的所有内容

[英]After I ran rake db:migrate VERSION=0 and redid the migration, I lost everything in my database

Before I was trying to run rake db:migrate I ran into an error called column duplication so I ran rake db:migrate VERSION=0 instead and ran rake db:migrate again. 在尝试运行rake db:migrate之前,我遇到了一个称为列重复的错误,因此我改为运行rake db:migrate VERSION = 0,然后再次运行rake db:migrate。

Basically I was clearing up my previous migration and by running a new migration from VERSION=0, I was finally able to finish my pending migration and had no issue with column duplication again. 基本上,我正在清理以前的迁移,并通过从VERSION = 0运行新迁移,终于能够完成我的挂起迁移,并且列复制再次没有问题。

But here's the thing, the command I ran actually cleared up all my previous data. 但是,我执行的命令实际上清除了所有先前的数据。 Is there a way to retrieve what I had lost? 有没有办法找回我丢失的东西? And what is the safest way to run db:migrate if there's a column duplication error. 如果存在列重复错误,那么运行db:migrate的最安全方法是什么? I really don't want to go back to VERSION=0 我真的不想回到VERSION = 0

Is there a way to retrieve what I had lost? 有没有办法找回我丢失的东西?

You deleted your database table and re-created it. 您删除了数据库表并重新创建了它。 All data that was stored in that table was also deleted and is lost. 该表中存储的所有数据也被删除并丢失。 The only way to get your data back is to restore from a backup. 取回数据的唯一方法是从备份还原。 Do you have a recent database backup? 您最近有数据库备份吗?

And what is the safest way to run db:migrate if there's a column duplication error. 如果存在列重复错误,那么运行db:migrate的最安全方法是什么?

If you migration has an error, fix that migration and re-run it. 如果您的迁移有错误,请修复该迁移并重新运行。 In you example: If your migration fails with an error telling you that there is a duplicate column, just remove the code that tries to add a duplicate column and re-run the migration. 在您的示例中:如果迁移失败并显示一条错误消息,提示您存在重复的列,则只需删除尝试添加重复列的代码,然后重新运行迁移即可。

rails migration has a strict process to be followed. Rails迁移必须遵循严格的流程。 Suppose you have duplicate column. 假设您有重复的列。 You should write another migration file to remove that duplicate column. 您应该编写另一个迁移文件以删除该重复的列。 When you have ran 当你跑了

rails db:migrate VERSION=0 rails db:migrate VERSION = 0

It is creating your database all again so obvious that data will be lost. 它再次创建数据库非常明显,以至于数据将丢失。

So make sure any correction in Database used in Rails should follow strict process of creating new migration rather than editing the same. 因此,请确保在Rails中使用的数据库中的所有更正应遵循创建新迁移而不是对其进行编辑的严格过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM