57

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

2
  • 3
    You should also take a look at this blog post Commented Feb 24, 2014 at 19:20
  • 2
    rails console --sandbox and then run migration from console. The SQL will be printed while it is being executed and everything should rollback when you exit the console Commented Aug 22, 2019 at 13:03

5 Answers 5

100

Building on @qarol but even cooler, add this Rake task to one of your Rake files:

task :log => :environment do
  ActiveRecord::Base.logger = Logger.new(STDOUT)
end

Then you can call ANY Rake task and have the output logged:

rake log db:migrate
Sign up to request clarification or add additional context in comments.

4 Comments

You can use this: ActionMailer::Base.logger = ActiveRecord::Base.logger = Logger.new(STDOUT) to get mailer output as well.
This worked great and is much more generic. It should be the accepted answer.
Put this script in a new file called log_migration.rake (or whatever) and drop it in the lib/tasks directory
This ActiveRecord::Base.logger... statement can also be placed in the default Rakefile to unconditionally output all migration statements.
46

You can create a Rake task in lib/tasks/:

namespace :db do
  desc 'Make migration with output'
  task(:migrate_with_sql => :environment) do
    ActiveRecord::Base.logger = Logger.new(STDOUT)
    Rake::Task['db:migrate'].invoke
  end
end

Then call rake db:migrate_with_sql to log the migration.

3 Comments

ActiveRecord::Base.logger = Logger.new(STDOUT) is the magic, getting a particular migration to run just involves setting the logger prior :)
This is brilliance
This is fine but stackoverflow.com/a/8335695/972128 feels more elegant.
17

The SQL output is captured in your environment log file e.g. development.log

1 Comment

There are too many tasks and requests going on that I cannot check it out in development.log
8

I put together the capture_migration_sql gem for this purpose. It will dump your migration SQL to files in db/migration_sql.

It's overkill if you just need to find a single SQL statement, but is great to have if your production process requires the raw SQL statements. It can also make reviewing complex database changes in code reviews a bit easier since there's no ruby -> SQL mental math required.

2 Comments

This needs to be upvoted more, this gem is super useful and more than answers the question
Thank you for this super useful gem! Helped me a lot today.
6

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.