I have the following SQL which I need to do
CREATE TABLE cars_users2 AS SELECT DISTINCT * FROM cars_users;
DROP TABLE cars_users;
ALTER TABLE cars_users2 RENAME TO cars_users;
since I cannot use heroku dataclips to drop a table, I cannot use dataclips.
So I guess I need to do this in a migration.
How do I write this sql as a migration?
I prefer here doc:
notice: This only works for PostgreSQL, if you are using MySQL you should set CLIENT_MULTI_STATEMENTS for the adapter.
In case you need to use
change
instead ofup
anddown
you can usereversible
. It works on Rails 4 or above.Sources: http://edgeguides.rubyonrails.org/active_record_migrations.html#using-reversible
https://apidock.com/rails/ActiveRecord/Migration/reversible
For your up migration:
and for down:
Full migration:
You could try to use the
execute
method.Something like this (it's untested, some kind of brainchild)
As there is no equivalent
down
method, anActiveRecord::IrreversibleMigration
should be raised when trying to migrate down.