Created
October 15, 2009 17:50
-
-
Save marcospereira/211135 to your computer and use it in GitHub Desktop.
Logging SQL for Rails Migrations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace "db" do | |
namespace "migrate" do | |
desc "migrations logging sql" | |
task "logging" => :environment do | |
connection = ActiveRecord::Base.connection | |
class << connection | |
alias :original_exec :execute | |
def execute(sql, *name) | |
puts "\t #{sql}" | |
result = original_exec(sql, *name) | |
puts "\t #{database_message}\n" | |
return result | |
end | |
def database_message | |
if RAILS_ENV == 'development' or RAILS_ENV == 'test' | |
rows_affected = instance_variable_get(:@connection).changes | |
warning_count = 0 | |
else | |
rows_affected = instance_variable_get(:@connection).affected_rows | |
warning_count = instance_variable_get(:@connection).warning_count | |
end | |
"#{rows_affected} rows affected, #{warning_count} warnings" | |
end | |
ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true | |
ActiveRecord::Migrator.migrate("db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment