Created
June 3, 2012 01:18
-
-
Save alainravet/2860829 to your computer and use it in GitHub Desktop.
have `rake db:migrate` export the matching DDL to a file in db/migrate
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
if $0.end_with?('/rake') && ARGV[0].start_with?('db:migrate') && Rails.env.development? | |
#require 'active_record/migration' | |
klass = | |
case (adapter_class = ActiveRecord::Base.connection.class.to_s) | |
when 'ActiveRecord::ConnectionAdapters::SQLite3Adapter' | |
require 'active_record/connection_adapters/sqlite_adapter' | |
ActiveRecord::ConnectionAdapters::SQLiteAdapter | |
when 'ActiveRecord::ConnectionAdapters::Mysql2Adapter' | |
require 'active_record/connection_adapters/mysql2_adapter' | |
ActiveRecord::ConnectionAdapters::Mysql2Adapter | |
when 'ActiveRecord::ConnectionAdapters::MysqlAdapter' | |
require 'active_record/connection_adapters/mysql_adapter' | |
ActiveRecord::ConnectionAdapters::MysqlAdapter | |
else | |
raise "Unsupported adapter : #{adapter_class}" | |
end | |
klass.class_eval(' | |
alias_method :original_execute, :execute | |
def execute(sql, name = nil) #:nodoc: | |
DDLMigrationsExporter.record_ddl(sql) | |
original_execute(sql, name = nil) | |
end | |
') | |
class ActiveRecord::Migrator#:nodoc: | |
class << self | |
alias_method :original_migrate, :migrate | |
def migrate(migrations_paths, target_version = nil, &block) | |
migrations_summaries = original_migrate(migrations_paths, target_version, &block) | |
DDLMigrationsExporter.export_ddl migrations_summaries | |
migrations_summaries | |
end | |
end | |
end | |
class DDLMigrationsExporter | |
class << self | |
attr_accessor :migrations_as_ddl | |
def record_ddl(sql) | |
return if sql.include? '`schema_migrations`' | |
return if sql.start_with?('SHOW') \ | |
|| sql.start_with?('SELECT') \ | |
|| sql.start_with?('SET SQL_AUTO_IS_NULL=0') | |
(self.migrations_as_ddl ||= []) << sql | |
end | |
def export_ddl(migrations_summaries) | |
if migrations_summaries.empty? | |
puts "\033[31mMIGRATIONS ARE UP-TO-DATE -> NO DDL\033[0m" | |
return | |
end | |
lines = [] | |
migrations_ids = migrations_summaries.collect(&:version) | |
ddl_filename = 'db/migrate/DDL_for_migrations_' \ | |
+ migrations_ids.join('_') \ | |
+ '.sql' | |
lines << "-- DDL for the migrations :" | |
lines += migrations_summaries.collect { |m| | |
m.filename =~ /([^\/]+)$/ | |
"-- #{$1}" | |
} | |
lines << '' | |
lines << "-- date : #{Time.now}" | |
lines << '' | |
lines << '-- DDL :' | |
lines += migrations_as_ddl | |
lines << '-- : LDD' | |
puts "\033[32mExporting the DDL to : #{ddl_filename}\033[0m..." | |
open(ddl_filename, 'w') do |f| lines.each do |ddl| f.puts ddl end end | |
puts "\033[32mReading the DDL file\033[0m :" | |
File.open(ddl_filename).readlines.collect(&:chomp).each do |line| | |
puts "\033[33m#{line}\033[0m".chomp | |
end and puts | |
end | |
end | |
end | |
end # if $0.end_with?('/rake') && ARGV[0].start_with?('db:migrate') && Rails.env.development? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment