require 'test/unit' require "rubygems" require "active_support" require "active_record" require File.join(File.dirname(__FILE__), "..", "init") class Test::Unit::TestCase def run_migration(version = nil, path = File.join(File.dirname(__FILE__), "migrations", "")) ActiveRecord::Migrator.migrate(path, version) end def run_migration_without_duplicates(version = nil, path = File.join(File.dirname(__FILE__), "migrations", "")) ActiveRecord::Migrator.migrate_without_duplicates(path, version) end def convert_from_non_duplicates(path = File.join(File.dirname(__FILE__), "migrations_no_dup", "")) ActiveRecord::Migrator.convert_from_non_duplicates(path) end def convert_from_duplicates(path = File.join(File.dirname(__FILE__), "migrations_no_dup", "")) ActiveRecord::Migrator.convert_from_duplicates(path) end def ensure_database! ActiveRecord::Base.logger = Logger.new(File.open("/tmp/duplicate_migrations_test.log", "w")) conn_spec = YAML.load(File.read(File.join(File.dirname(__FILE__), "database.yml"))) ActiveRecord::Base.establish_connection(conn_spec) assert ActiveRecord::Base.connection ActiveRecord::Base.connection.recreate_database(conn_spec["database"]) ActiveRecord::Base.establish_connection(conn_spec) assert ActiveRecord::Base.connection end def tables_exist?(*names) tables = ActiveRecord::Base.connection.select_all("SHOW TABLES") names = names.map(&:to_s) incl = true tables = tables.collect(&:values).flatten names.each do |name| incl = false if !tables.include?(name.to_s) end incl end def tables_dont_exist?(*names) tables = ActiveRecord::Base.connection.select_all("SHOW TABLES") names = names.map(&:to_s) incl = true tables = tables.collect(&:values).flatten names.each do |name| incl = false if tables.include?(name.to_s) end incl end def schemas_exist?(*names) tables = ActiveRecord::Base.connection.select_all("SELECT * FROM #{ActiveRecord::Migrator.schema_info_table_name}s") names = names.map(&:to_s) tables = tables.collect{|row| row["migration"] } incl = true names.each do |name| incl = false if !tables.include?(name.to_s) end incl end def schemas_dont_exist?(*names) tables = ActiveRecord::Base.connection.select_all("SELECT * FROM #{ActiveRecord::Migrator.schema_info_table_name}s") names = names.map(&:to_s) tables.collect{|row| row["migration"] } incl = true names.each do |name| incl = false if tables.include?(name.to_s) end incl end def no_tables_exist? tables = ActiveRecord::Base.connection.select_all("SHOW TABLES") tables.collect(&:values).flatten.empty? end def no_schemas_exist? tables = ActiveRecord::Base.connection.select_all("SHOW TABLES") return true if !tables.collect(&:values).flatten.include?("#{ActiveRecord::Migrator.schema_info_table_name}s") tables = ActiveRecord::Base.connection.select_all("SELECT * FROM #{ActiveRecord::Migrator.schema_info_table_name}s") tables.empty? end end