PostgreSQL
 sql >> Database >  >> RDS >> PostgreSQL

Come scrivere SQL in una migrazione in Rails

Per la tua migrazione verso l'alto:

execute "CREATE TABLE cars_users2 AS SELECT DISTINCT * FROM cars_users;" 
drop_table :car_users  
rename_table :car_users2, :car_users  

e per il piumino:

raise ActiveRecord::IrreversibleMigration

Migrazione completa:

class TheMigration < ActiveRecord::Migration
    def up
        execute "CREATE TABLE cars_users2 AS SELECT DISTINCT * from cars_users;" 
        drop_table :car_users  
        rename_table :car_users2, :car_users  
    end

    def down
        raise ActiveRecord::IrreversibleMigration
    end
end