2017-05-26 04:36:01 -04:00
|
|
|
module MigrationsHelpers
|
|
|
|
def table(name)
|
|
|
|
Class.new(ActiveRecord::Base) { self.table_name = name }
|
|
|
|
end
|
|
|
|
|
|
|
|
def migrations_paths
|
|
|
|
ActiveRecord::Migrator.migrations_paths
|
|
|
|
end
|
|
|
|
|
2017-05-26 08:46:45 -04:00
|
|
|
def table_exists?(name)
|
|
|
|
ActiveRecord::Base.connection.table_exists?(name)
|
|
|
|
end
|
|
|
|
|
2017-06-05 05:07:10 -04:00
|
|
|
def migrations
|
|
|
|
ActiveRecord::Migrator.migrations(migrations_paths)
|
|
|
|
end
|
|
|
|
|
2017-08-07 05:11:11 -04:00
|
|
|
def reset_column_in_migration_models
|
2017-08-28 06:49:43 -04:00
|
|
|
ActiveRecord::Base.clear_cache!
|
|
|
|
|
2017-08-07 05:11:11 -04:00
|
|
|
described_class.constants.sort.each do |name|
|
|
|
|
const = described_class.const_get(name)
|
|
|
|
|
|
|
|
if const.is_a?(Class) && const < ActiveRecord::Base
|
|
|
|
const.reset_column_information
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-05 05:07:10 -04:00
|
|
|
def previous_migration
|
|
|
|
migrations.each_cons(2) do |previous, migration|
|
|
|
|
break previous if migration.name == described_class.name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-16 07:05:18 -04:00
|
|
|
def migration_schema_version
|
|
|
|
self.class.metadata[:schema] || previous_migration.version
|
|
|
|
end
|
|
|
|
|
|
|
|
def schema_migrate_down!
|
2017-08-17 06:34:31 -04:00
|
|
|
disable_migrations_output do
|
|
|
|
ActiveRecord::Migrator.migrate(migrations_paths,
|
|
|
|
migration_schema_version)
|
|
|
|
end
|
|
|
|
|
2017-08-16 07:05:18 -04:00
|
|
|
reset_column_in_migration_models
|
|
|
|
end
|
|
|
|
|
|
|
|
def schema_migrate_up!
|
2017-08-17 06:34:31 -04:00
|
|
|
disable_migrations_output do
|
|
|
|
ActiveRecord::Migrator.migrate(migrations_paths)
|
|
|
|
end
|
|
|
|
|
2017-08-16 07:05:18 -04:00
|
|
|
reset_column_in_migration_models
|
|
|
|
end
|
|
|
|
|
2017-08-17 06:34:31 -04:00
|
|
|
def disable_migrations_output
|
|
|
|
ActiveRecord::Migration.verbose = false
|
|
|
|
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Migration.verbose = true
|
|
|
|
end
|
|
|
|
|
2017-05-26 04:36:01 -04:00
|
|
|
def migrate!
|
|
|
|
ActiveRecord::Migrator.up(migrations_paths) do |migration|
|
|
|
|
migration.name == described_class.name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|