-
-
Save iamlos/1ddaf44fa5fee42f5afff04659fd41a4 to your computer and use it in GitHub Desktop.
Get SQL output from a Rails migration
This file contains hidden or 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
| #!/usr/bin/env ruby | |
| require_relative 'config/environment' | |
| path = ARGV.shift || raise("specify migration as first argument") | |
| require_relative path | |
| filename = File.basename(path, ".rb") | |
| timestamp, name = filename.split("_", 2) | |
| name = name.camelcase | |
| ActiveRecord::Migration.verbose = false | |
| module Wrapper | |
| def execute(sql, *args) | |
| if sql =~ /^ALTER\ TABLE|((CREATE|DROP) (TABLE|VIEW))|INSERT/i | |
| puts sql.gsub(/`/, '') + ";" | |
| else | |
| super | |
| end | |
| end | |
| end | |
| ActiveRecord::Base.connection # Needed to load Mysql2Adapter | |
| ActiveRecord::ConnectionAdapters::Mysql2Adapter.prepend(Wrapper) | |
| migration = name.constantize | |
| migration.migrate(:up) | |
| ActiveRecord::SchemaMigration.create!(:version => timestamp.to_s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment