简体   繁体   English

我的Rails模式更改会清除掉数据吗?

[英]Will my Rails schema changes wipe out data?

Sort of weird question, I suspect. 我怀疑这是一个奇怪的问题。 I'll just explain the problem immediately below, then give the background that explains how I got here, for those interested 我将在下面立即说明问题,然后为感兴趣的人提供解释我如何到达这里的背景

PROBLEM: 问题:

My schema is in such a state as a result of migrations that it marks me as having both removed and added the same columns. 由于迁移,我的架构处于这样的状态,它将我标记为已删除并添加了相同的列。 In other words, I get this when I diff schema.rb: 换句话说,我在比较schema.rb时得到了这个:

-    t.integer  "cut_id"
-    t.datetime "created_at",  :null => false
-    t.datetime "updated_at",  :null => false
-    t.integer  "animal_id"
+    t.datetime "created_at",                     :null => false
+    t.datetime "updated_at",                     :null => false
+    t.integer  "animal_id"
+    t.integer  "cut_id"

The concern is that while this doesn't net add/remove columns, if I push this code to production and run the migrations, it will essentially wipe out existing data, even if it doesn't remove any columns in net. 令人担心的是,尽管这不会净增/删除列,但如果我将此代码推送到生产环境并运行迁移,则即使它没有删除净额中的任何列,它也将清除现有数据。

Is this the case? 是这样吗 Does anyone know whether the DB will literally remove and then add the four columns, or realize that there are no changes? 有谁知道数据库是从字面上删除然后添加四列,还是意识到没有更改? If it will remove them, can anyone think of a way to fix this and save the data to re-insert BEFORE it's deleted? 如果它将删除它们,谁能想到解决此问题的方法,并保存数据以在删除之前重新插入? I don't mind a lag time between the erasure of the data and its replacement. 我不介意在删除数据与替换数据之间存在时间间隔。

HOW I GOT HERE: 我如何在这里:

Basically, I made a whole bunch of changes on local, involving a number of migrations, before realizing that I'd erased some columns I needed to migrate all the data I wanted to new tables, because I'm hasty, and an idiot. 基本上,我在本地进行了很多更改,涉及许多迁移,然后才意识到我已经擦除了一些列,因为我很匆忙,而且是个白痴,因此我需要将所有想要迁移的数据迁移到新表中。

Luckily, this was just on local. 幸运的是,这只是本地的。 Unluckily, I just did a git reset HEAD^ to toss out my work, but even though the migrations had disappeared, the schema remained doggedly in the post-bad-migration state, and couldn't be reset or anything. 不幸的是,我只是做了一个git reset HEAD ^来扔掉我的工作,但是即使迁移消失了,该架构仍处于顽固的迁移后状态,无法重置或进行任何其他操作。 So I went through this whole process of trying to recover the old migrations and roll them back, half-successfully, and then write other migrations to completely roll back the changes I semi-rolled-back. 因此,我经历了整个过程,尝试恢复旧的迁移并成功地将其回滚一半,然后编写其他迁移以完全回滚我半回滚的更改。

Huge mess, with the result of just this one problem. 巨大的混乱,仅仅是这个问题的结果。 Any chance it isn't a problem after all, and I can deploy/sleep easy? 毕竟这不是问题,我可以轻松部署/睡眠吗?

So there are two different issues here - the migrations and the schema. 因此,这里有两个不同的问题-迁移和架构。 If you run migrations that remove columns, then the columns will be removed, as will any data that they contain. 如果运行删除列的迁移,那么列以及包含的所有数据也会被删除。 So you probably want to edit your migrations and remove or edit any migrations that remove columns you want to keep. 因此,您可能想要编辑迁移并删除或编辑任何删除要保留的列的迁移。 Be very, very careful that any migrations do not remove data you want to keep. 请务必非常小心,任何迁移都不会删除要保留的数据。

The schema.rb is essentially irrelevant to your production database unless you explicitly load the schema with a rake task (rake db:schema:load). schema.rb本质上与生产数据库无关,除非您使用rake任务(rake db:schema:load)显式加载架构。 That would wipe out your data, but it's not something you'd do to your production system as a general rule. 那会抹掉您的数据,但通常来说,这不是您要对生产系统执行的操作。

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

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