class ActiveRecord::IrreversibleMigration
end
end
end
end
SQL
DROP CONSTRAINT zipchk
ALTER TABLE distributors
execute <<~SQL
dir.down do
end
SQL
CHECK (char_length(zipcode) = 5) NO INHERIT;
ADD CONSTRAINT zipchk
ALTER TABLE distributors
execute <<~SQL
dir.up do
reversible do |dir|
end
t.string :zipcode
create_table :distributors do |t|
def change
class ReversibleMigrationExample < ActiveRecord::Migration[7.0]
2. Use the #reversible method in #change
method:
end
end
drop_table :distributors
SQL
DROP CONSTRAINT zipchk
ALTER TABLE distributors
execute <<~SQL
def down
end
SQL
CHECK (char_length(zipcode) = 5) NO INHERIT;
ADD CONSTRAINT zipchk
ALTER TABLE distributors
execute <<~SQL
end
t.string :zipcode
create_table :distributors do |t|
def up
class ReversibleMigrationExample < ActiveRecord::Migration[7.0]
1. Define #up
and #down
methods instead of #change
:
There are two ways to mitigate this problem.
end
end
SQL
CHECK (char_length(zipcode) = 5) NO INHERIT;
ADD CONSTRAINT zipchk
ALTER TABLE distributors
execute <<~SQL
end
t.string :zipcode
create_table :distributors do |t|
def change
class IrreversibleMigrationExample < ActiveRecord::Migration[7.0]
Rolling back this migration will raise an ActiveRecord::IrreversibleMigration error.
For example the following migration is not reversible.
Exception that can be raised to stop migrations from being rolled back.