class ActiveRecord::MigrationContext
per primary database.
sufficient. Multiple database applications need a SchemaMigration
class can be provided. For most applications, SchemaMigration
is
in the migrations_paths
parameter. Optionally a schema_migration
A migration context requires the path to the migrations is set
MigrationContext sets the context in which a migration is run.
def current_environment # :nodoc:
def current_environment # :nodoc: ActiveRecord::ConnectionHandling::DEFAULT_ENV.call end
def current_version # :nodoc:
def current_version # :nodoc: get_all_versions.max || 0 rescue ActiveRecord::NoDatabaseError end
def down(target_version = nil, &block) # :nodoc:
def down(target_version = nil, &block) # :nodoc: selected_migrations = if block_given? migrations.select(&block) else migrations end Migrator.new(:down, selected_migrations, schema_migration, target_version).migrate end
def forward(steps = 1) # :nodoc:
def forward(steps = 1) # :nodoc: move(:up, steps) end
def get_all_versions # :nodoc:
def get_all_versions # :nodoc: if schema_migration.table_exists? schema_migration.all_versions.map(&:to_i) else [] end end
def initialize(migrations_paths, schema_migration = SchemaMigration)
def initialize(migrations_paths, schema_migration = SchemaMigration) @migrations_paths = migrations_paths @schema_migration = schema_migration end
def last_stored_environment # :nodoc:
def last_stored_environment # :nodoc: return nil unless ActiveRecord::InternalMetadata.enabled? return nil if current_version == 0 raise NoEnvironmentInSchemaError unless ActiveRecord::InternalMetadata.table_exists? environment = ActiveRecord::InternalMetadata[:environment] raise NoEnvironmentInSchemaError unless environment environment end
def migrate(target_version = nil, &block)
If none of the conditions are met, +up+ will be run with
the +target_version+, then +down+ will be run.
If the +current_version+ in the schema is greater than
will be run.
0 then an empty array will be returned and no migrations
If the +current_version+ and +target_version+ are both
If +target_version+ is +nil+, +migrate+ will run +up+.
Runs the migrations in the +migrations_path+.
def migrate(target_version = nil, &block) case when target_version.nil? up(target_version, &block) when current_version == 0 && target_version == 0 [] when current_version > target_version down(target_version, &block) else up(target_version, &block) end end
def migration_files
def migration_files paths = Array(migrations_paths) Dir[*paths.flat_map { |path| "#{path}/**/[0-9]*_*.rb" }] end
def migrations # :nodoc:
def migrations # :nodoc: migrations = migration_files.map do |file| version, name, scope = parse_migration_filename(file) raise IllegalMigrationNameError.new(file) unless version version = version.to_i name = name.camelize MigrationProxy.new(name, version, file, scope) end migrations.sort_by(&:version) end
def migrations_status # :nodoc:
def migrations_status # :nodoc: db_list = schema_migration.normalized_versions file_list = migration_files.filter_map do |file| version, name, scope = parse_migration_filename(file) raise IllegalMigrationNameError.new(file) unless version version = schema_migration.normalize_migration_number(version) status = db_list.delete(version) ? "up" : "down" [status, version, (name + scope).humanize] end db_list.map! do |version| ["up", version, "********** NO FILE **********"] end (db_list + file_list).sort_by { |_, version, _| version.to_i } end
def move(direction, steps)
def move(direction, steps) migrator = Migrator.new(direction, migrations, schema_migration) if current_version != 0 && !migrator.current_migration raise UnknownMigrationVersionError.new(current_version) end start_index = if current_version == 0 0 else migrator.migrations.index(migrator.current_migration) end finish = migrator.migrations[start_index + steps] version = finish ? finish.version : 0 public_send(direction, version) end
def needs_migration? # :nodoc:
def needs_migration? # :nodoc: pending_migration_versions.size > 0 end
def open # :nodoc:
def open # :nodoc: Migrator.new(:up, migrations, schema_migration) end
def parse_migration_filename(filename)
def parse_migration_filename(filename) File.basename(filename).scan(Migration::MigrationFilenameRegexp).first end
def pending_migration_versions # :nodoc:
def pending_migration_versions # :nodoc: migrations.collect(&:version) - get_all_versions end
def protected_environment? # :nodoc:
def protected_environment? # :nodoc: ActiveRecord::Base.protected_environments.include?(last_stored_environment) if last_stored_environment end
def rollback(steps = 1) # :nodoc:
def rollback(steps = 1) # :nodoc: move(:down, steps) end
def run(direction, target_version) # :nodoc:
def run(direction, target_version) # :nodoc: Migrator.new(direction, migrations, schema_migration, target_version).run end
def up(target_version = nil, &block) # :nodoc:
def up(target_version = nil, &block) # :nodoc: selected_migrations = if block_given? migrations.select(&block) else migrations end Migrator.new(:up, selected_migrations, schema_migration, target_version).migrate end