Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 465
Blankman Avatar asked Nov 04 '10 19:11

Blankman


People also ask

What is a migration script SQL?

A SQL migration script is similar to a SQL build script, except that it changes a database from one version to another, rather than builds it from scratch. Although they're simple in essence, it is worth knowing how to use them effectively for stress-free database updates and deployments.

What does db Migrate do?

A migration means that you move from the current version to a newer version (as is said in the first answer). Using rake db:migrate you can apply any new changes to your schema. But if you want to rollback to a previous migration you can use rake db:rollback to nullify your new changes if they are incorrectly defined.

How rails db Migrate works?

When you run db:migrate, rails will check a special table in the database which contains the timestamp of the last migration applied to the database. It will then apply all of the migrations with timestamps after that date and update the database table with the timestamp of the last migration.


2 Answers

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 
like image 116
Josh Avatar answered Oct 23 '22 23:10

Josh


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.

like image 37
qarol Avatar answered Oct 24 '22 00:10

qarol