Skip to content

Instantly share code, notes, and snippets.

@josegomezr
Last active April 30, 2020 10:57
Show Gist options
  • Save josegomezr/a229072d24f07b6fc30a750d49096181 to your computer and use it in GitHub Desktop.
Save josegomezr/a229072d24f07b6fc30a750d49096181 to your computer and use it in GitHub Desktop.
Ruby script to see the DDL ran by rails in a migration

Place this in the rails root of your project. And then call

ruby rails_migration_checker.rb <path/to/migration 1> [<path/to/migration 2>...]
#!/usr/bin/env ruby
module Wrapper
def execute(sql, *args)
puts " > " + sql.gsub(/`/, '') + ";"
super
end
end
require_relative 'config/environment'
ActiveRecord::Base.connection
ActiveRecord::Migration.verbose = false
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(Wrapper)
queue = []
while path = ARGV.shift
require_relative path
filename = File.basename(path, ".rb")
timestamp, name = filename.split("_", 2)
name = name.camelcase
migration = name.constantize
schema_change = ActiveRecord::SchemaMigration.new(:version => timestamp.to_s)
queue.append([
migration,
name,
timestamp,
schema_change,
])
end
puts
puts "====== UP ======="
queue.each do |(migration, name, timestamp, schema_change)|
puts "^ #{timestamp}_#{name}"
migration.migrate(:up)
schema_change.save
end
puts
puts "====== DOWN ======="
queue.reverse_each do |(migration, name, timestamp, schema_change)|
puts "v #{timestamp}_#{name}"
migration.migrate(:down)
schema_change.destroy
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment