Is it possible to output the SQL change scripts that 'rake db:migrate' produces?

后端 未结 5 1279
失恋的感觉
失恋的感觉 2020-12-04 19:00

Is it possible to output the SQL change scripts that \'rake db:migrate\' produces?

5条回答
  •  眼角桃花
    2020-12-04 19:47

    You can run preview SQL in rails console like this:

    ActiveRecord::Base.connection.change_table(:events) do |t|
      t.integer :submission_id, default: 5, null: false
    end
    #=> ALTER TABLE `events` ADD `submission_id` int DEFAULT 5 NOT NULL
    

    So, just prepend your ordinary migration methods with ActiveRecord::Base.connection. and you are good to go.

    There is also rails console --sandbox mode which rollbacks changes after you close the console. Though check if it works for your project, somehow for our project with rails 5 + MySQL it doesn't roll back DDL changes.


    If you have migration files ready, you can also run it directly:

    ActiveRecord::MigrationContext.new("db/migrate").migrate
    ActiveRecord::MigrationContext.new("db/migrate").rollback
    

提交回复
热议问题