Skip to content

Instantly share code, notes, and snippets.

@caius
Created November 3, 2015 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save caius/415d9ee1f4864c282c1d to your computer and use it in GitHub Desktop.
Save caius/415d9ee1f4864c282c1d to your computer and use it in GitHub Desktop.
`bundle exec rake db:migrate > up.sql; migration_sql.rb < up.sql`
#!/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