Created
November 3, 2015 14:34
-
-
Save caius/415d9ee1f4864c282c1d to your computer and use it in GitHub Desktop.
`bundle exec rake db:migrate > up.sql; migration_sql.rb < up.sql`
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
#!/usr/bin/env ruby | |
inside_migration = false | |
previous_line = "" | |
ARGF.each_line do |line| | |
# Remove ANSI colours to make shit easier to reason with | |
line.gsub!(/\e\[(\d+)(;\d+)*m/, "") | |
if line =~ /Migrating to/ | |
inside_migration = true | |
next | |
end | |
if query = line[%r@(?:^|-- :(?:\s+SQL)?)\s*\([\d.]+ms\)\s*(.*?)(?: /\*.+\*/)?$@, 1] | |
puts query << ";" unless %w(BEGIN COMMIT).include?(query) | |
end | |
if inside_migration && line =~ /(?:INSERT INTO|DELETE FROM) `schema_migrations`/ | |
inside_migration = false | |
end | |
if previous_line =~ /COMMIT|DELETE FROM `schema_migrations`/ && line =~ /ActiveRecord::SchemaMigration Load/ | |
# We're just dumping the schema now, so don't bother parsing anything | |
break | |
end | |
previous_line = line | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment