Is it possible to output the SQL change scripts that 'rake db:migrate' produces?
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With