简体   繁体   中英

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:

-    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. 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). That would wipe out your data, but it's not something you'd do to your production system as a general rule.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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