v7.0.0 -
Show latest stable
-
0 notes
- Superclass:
ActiveRecord::ActiveRecordError
- 1.0.0 (0)
- 1.1.6 (0)
- 1.2.6 (0)
- 2.0.3 (0)
- 2.1.0 (0)
- 2.2.1 (0)
- 2.3.8 (0)
- 3.0.0 (2)
- 3.0.9 (0)
- 3.1.0 (0)
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (0)
- 4.1.8 (0)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (38)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- 7.2.3 (0)
- 8.0.0 (0)
- 8.1.1 (0)
- What's this?
Exception that can be raised to stop migrations from being rolled back. For example the following migration is not reversible. Rolling back this migration will raise an ActiveRecord::IrreversibleMigration error.
class IrreversibleMigrationExample < ActiveRecord::Migration[7.0] def change create_table :distributors do |t| t.string :zipcode end execute <<~SQL ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5) NO INHERIT; SQL end end
There are two ways to mitigate this problem.
-
Define #up and #down methods instead of #change:
class ReversibleMigrationExample < ActiveRecord::Migration[7.0] def up create_table :distributors do |t| t.string :zipcode end execute <<~SQL ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5) NO INHERIT; SQL end def down execute <<~SQL ALTER TABLE distributors DROP CONSTRAINT zipchk SQL drop_table :distributors end end
-
Use the #reversible method in #change method:
class ReversibleMigrationExample < ActiveRecord::Migration[7.0] def change create_table :distributors do |t| t.string :zipcode end reversible do |dir| dir.up do execute <<~SQL ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5) NO INHERIT; SQL end dir.down do execute <<~SQL ALTER TABLE distributors DROP CONSTRAINT zipchk SQL end end end end

