More ActiveRecord Pilfering
The idea behind reversible migrations is that in many cases, the library can know exactly how to reverse the migration you are using. For example, if you are using create_table, it can be reversed by using drop_table. And if you are using create_index followed add_column, it can reverse it by calling drop_column and then drop_index. Basically, all reversible migrations can be reversed by applying the reverse of each action in reverse order.
Sequel.migration do change do create_table(:artists) do primary_key :id String :name, :null=>false end end end
This will automatically create the equivalent of:
Sequel.migration do up do create_table(:artists) do primary_key :id String :name, :null=>false end end down do drop_table(:artists) end end
There is no support for reversible migrations using the historical (but still supported) usage of creating a subclass of Sequel::Migration.
The following Database methods are handled and can be reversed successfully:
- +alter_table+ (supporting the following methods in the +alter_table+ block):
- +add_foreign_key+ (with a symbol, not an array)
- +add_primary_key+ (with a symbol, not an array)
Usage of any other method in the change block will result in a down block created that raises a Sequel::Error (basically making the migration irreversible).