

The migration that cannot be undone: Irreversible Migration
source link: https://www.devroom.io/2008/05/06/the-migration-that-cannot-be-undone-irreversible-migration/?utm_campaign=Feed%3A+ariejan+%28ariejan%7Cdevroom.io%29
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

The migration that cannot be undone: Irreversible Migration
Posted:
2008-05-06
- Last updated:
2019-06-05
Tagged general
Migrations have up and down methods, as we all know. But in some cases, your up method does things you can’t undo in your down method.
For example:
def self.up # Change the zipcode from the current :integer to a :string type. change_column :address, :zipcode, :string end
Now, converting integers to strings will always work. But, you feel it coming, converting a string into an integer will not always be possible. In other words, we can’t reverse this migration.
That’s why we should raise ActiveRecord::IrreversibleMigration in the down method.
def self.down raise ActiveRecord::IrreversibleMigration end
Now, if you run your migration (upwards), you’ll see it being applied like it shoud. However, if you try to go back, you’ll see rake aborting with ActiveRecord::IrreversibleMigration.
$ rake db:migrate VERSION=4 -- Database is migrated $ rake db:migrate VERSION=3 -- Rake aborted! -- ActiveRecord::IrreversibleMigration
So, if you have things you can’t undo, raise ActiveRecord::IrreversibleMigration in your migration’s down method.
Recommend
-
84
现象最近工作中给公司的系统中增加了一些网络有关的功能,它是基于GatewayWorker的,启动后报告类似如下的错误信息:Cannotfindmodule(IP-MIB):Atline0in(none)Cannotfindmodule(IF-MIB):Atline0in(none)Cannotfindmodule(TCP-MIB):Atline0in(none)Cannotfindmodul...
-
41
I like big butts and I cannot lie
-
33
Just the other day I came across an interesting error in React, despite my code working as it should. Here’s the error: “Cannot update during an existing state transition” Not super confusing and ce...
-
72
Why the Kotlin/Native memory model cannot hold.
-
29
In this post: extending pebble games to include measurement based uncomputation. A pebble game is a simplified abstraction of a reversible computation. A p...
-
9
Ether price flirts with $2,400 ATH as irreversible hard fork looms New all-time highs are being witnessed all over the cryptocurrency market, and Ether is no exception. 25062 Total views 45...
-
7
Clean EnergyNuclear power is on the brink of a $1 trillion resurgence, but one acc...
-
8
Feds Have 48 Hrs to Fix 'Soon-to-Be-Irreversible Mistake' US Markets Loading... In the news...
-
16
How Biden’s Anti-Hacking ‘Dream Team’ Was...
-
5
Gamers at risk of irreversible hearing loss and tinnitus, study suggestsPublished4 days agocomments
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK