When dropping a database table in a migration all data contained within the dropped table will be lost as well. This is to be expected since the table schema no longer exists. However, it is possible to make such a migration reversible so that at least the database structure is preserved.
The expectation here is that we can run
rails db:migrate followed by
rails db:rollback and recover the previous table’s structure.
I'm using Rails 5 in the above example which makes
rakecommands consistent with
railscommands. This means that any
rakecommand such as
rake db:migratehas a counterpart of
A naive first approach might look something like the following:
Unfortunately, the above results in the following errors after attempting to rollback the drop migration.
As in the error message says above, drop_table can be reversible if you specify options.
Let’s specify the specific columns that were on the original table as part of the drop table block.
With the addition of the table columns we can now rollback the database structure and retain the its original schema. Now we can migrate and rollback at will without worrying about losing the widgets table.
Join the conversation