Skip to content

Instantly share code, notes, and snippets.

@marcospereira
Created October 15, 2009 17:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcospereira/211135 to your computer and use it in GitHub Desktop.
Save marcospereira/211135 to your computer and use it in GitHub Desktop.
Logging SQL for Rails Migrations
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