require 'active_record' require 'active_support' require 'stringio' require 'uri' require 'taps/adapter_hacks' module Taps module Schema extend self def create_config(url) uri = URI.parse(url) adapter = uri.scheme adapter = 'postgresql' if adapter == 'postgres' adapter = 'sqlite3' if adapter == 'sqlite' config = { 'adapter' => adapter, 'database' => uri.path.blank? ? uri.host : uri.path.split('/')[1], 'username' => uri.user, 'password' => uri.password, 'host' => uri.host, } config = sqlite_config(url) if config['adapter'] == 'sqlite3' config end def sqlite_config(url) m = %r{(sqlite3?)://(.+)}.match(url) database = m[2] database, q = database.split('?') { 'adapter' => 'sqlite3', 'database' => database } end def connection(database_url) config = create_config(database_url) c = ActiveRecord::Base.establish_connection(config) Taps::AdapterHacks.load(config['adapter']) c end def dump(database_url) connection(database_url) stream = StringIO.new ActiveRecord::SchemaDumper.ignore_tables = [] ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) stream.string end def dump_without_indexes(database_url) schema = dump(database_url) schema.split("\n").collect do |line| if line =~ /^\s+add_index/ line = "##{line}" end line end.join("\n") end def indexes(database_url) schema = dump(database_url) schema.split("\n").collect do |line| line if line =~ /^\s+add_index/ end.uniq.join("\n") end def load(database_url, schema) connection(database_url) eval(schema) end def load_indexes(database_url, indexes) connection(database_url) schema =<