mirror of
https://github.com/github/rails.git
synced 2026-01-10 07:07:54 -05:00
Allow to run migrations from more than one directory
This commit is contained in:
@@ -442,12 +442,14 @@ module ActiveRecord
|
||||
end
|
||||
end
|
||||
|
||||
def assume_migrated_upto_version(version, migrations_path = ActiveRecord::Migrator.migrations_path)
|
||||
def assume_migrated_upto_version(version, migrations_paths = ActiveRecord::Migrator.migrations_paths)
|
||||
migrations_paths = [migrations_paths] unless migrations_paths.kind_of?(Array)
|
||||
version = version.to_i
|
||||
sm_table = quote_table_name(ActiveRecord::Migrator.schema_migrations_table_name)
|
||||
|
||||
migrated = select_values("SELECT version FROM #{sm_table}").map { |v| v.to_i }
|
||||
versions = Dir["#{migrations_path}/[0-9]*_*.rb"].map do |filename|
|
||||
paths = migrations_paths.map {|p| "#{p}/[0-9]*_*.rb" }
|
||||
versions = Dir[*paths].map do |filename|
|
||||
filename.split('/').last.split('_').first.to_i
|
||||
end
|
||||
|
||||
|
||||
@@ -511,39 +511,40 @@ module ActiveRecord
|
||||
|
||||
class Migrator#:nodoc:
|
||||
class << self
|
||||
attr_writer :migrations_path
|
||||
attr_writer :migrations_paths
|
||||
alias :migrations_path= :migrations_paths=
|
||||
|
||||
def migrate(migrations_path, target_version = nil)
|
||||
def migrate(migrations_paths, target_version = nil)
|
||||
case
|
||||
when target_version.nil?
|
||||
up(migrations_path, target_version)
|
||||
up(migrations_paths, target_version)
|
||||
when current_version == 0 && target_version == 0
|
||||
[]
|
||||
when current_version > target_version
|
||||
down(migrations_path, target_version)
|
||||
down(migrations_paths, target_version)
|
||||
else
|
||||
up(migrations_path, target_version)
|
||||
up(migrations_paths, target_version)
|
||||
end
|
||||
end
|
||||
|
||||
def rollback(migrations_path, steps=1)
|
||||
move(:down, migrations_path, steps)
|
||||
def rollback(migrations_paths, steps=1)
|
||||
move(:down, migrations_paths, steps)
|
||||
end
|
||||
|
||||
def forward(migrations_path, steps=1)
|
||||
move(:up, migrations_path, steps)
|
||||
def forward(migrations_paths, steps=1)
|
||||
move(:up, migrations_paths, steps)
|
||||
end
|
||||
|
||||
def up(migrations_path, target_version = nil)
|
||||
self.new(:up, migrations_path, target_version).migrate
|
||||
def up(migrations_paths, target_version = nil)
|
||||
self.new(:up, migrations_paths, target_version).migrate
|
||||
end
|
||||
|
||||
def down(migrations_path, target_version = nil)
|
||||
self.new(:down, migrations_path, target_version).migrate
|
||||
def down(migrations_paths, target_version = nil)
|
||||
self.new(:down, migrations_paths, target_version).migrate
|
||||
end
|
||||
|
||||
def run(direction, migrations_path, target_version)
|
||||
self.new(direction, migrations_path, target_version).run
|
||||
def run(direction, migrations_paths, target_version)
|
||||
self.new(direction, migrations_paths, target_version).run
|
||||
end
|
||||
|
||||
def schema_migrations_table_name
|
||||
@@ -569,12 +570,20 @@ module ActiveRecord
|
||||
name.table_name rescue "#{ActiveRecord::Base.table_name_prefix}#{name}#{ActiveRecord::Base.table_name_suffix}"
|
||||
end
|
||||
|
||||
def migrations_path
|
||||
@migrations_path ||= 'db/migrate'
|
||||
def migrations_paths
|
||||
@migrations_paths ||= ['db/migrate']
|
||||
# just to not break things if someone uses: migration_path = some_string
|
||||
@migrations_paths.kind_of?(Array) ? @migrations_paths : [@migrations_paths]
|
||||
end
|
||||
|
||||
def migrations(path)
|
||||
files = Dir["#{path}/[0-9]*_*.rb"]
|
||||
def migrations_path
|
||||
migrations_paths.first
|
||||
end
|
||||
|
||||
def migrations(paths)
|
||||
paths = [paths] unless paths.kind_of?(Array)
|
||||
|
||||
files = Dir[*paths.map { |p| "#{p}/[0-9]*_*.rb" }]
|
||||
|
||||
seen = Hash.new false
|
||||
|
||||
@@ -598,22 +607,22 @@ module ActiveRecord
|
||||
|
||||
private
|
||||
|
||||
def move(direction, migrations_path, steps)
|
||||
migrator = self.new(direction, migrations_path)
|
||||
def move(direction, migrations_paths, steps)
|
||||
migrator = self.new(direction, migrations_paths)
|
||||
start_index = migrator.migrations.index(migrator.current_migration)
|
||||
|
||||
if start_index
|
||||
finish = migrator.migrations[start_index + steps]
|
||||
version = finish ? finish.version : 0
|
||||
send(direction, migrations_path, version)
|
||||
send(direction, migrations_paths, version)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(direction, migrations_path, target_version = nil)
|
||||
def initialize(direction, migrations_paths, target_version = nil)
|
||||
raise StandardError.new("This database does not yet support migrations") unless Base.connection.supports_migrations?
|
||||
Base.connection.initialize_schema_migrations_table
|
||||
@direction, @migrations_path, @target_version = direction, migrations_path, target_version
|
||||
@direction, @migrations_paths, @target_version = direction, migrations_paths, target_version
|
||||
end
|
||||
|
||||
def current_version
|
||||
@@ -679,7 +688,7 @@ module ActiveRecord
|
||||
|
||||
def migrations
|
||||
@migrations ||= begin
|
||||
migrations = self.class.migrations(@migrations_path)
|
||||
migrations = self.class.migrations(@migrations_paths)
|
||||
down? ? migrations.reverse : migrations
|
||||
end
|
||||
end
|
||||
|
||||
@@ -30,8 +30,8 @@ module ActiveRecord
|
||||
# ActiveRecord::Schema is only supported by database adapters that also
|
||||
# support migrations, the two features being very similar.
|
||||
class Schema < Migration
|
||||
def migrations_path
|
||||
ActiveRecord::Migrator.migrations_path
|
||||
def migrations_paths
|
||||
ActiveRecord::Migrator.migrations_paths
|
||||
end
|
||||
|
||||
# Eval the given block. All methods available to the current connection
|
||||
@@ -51,7 +51,7 @@ module ActiveRecord
|
||||
|
||||
unless info[:version].blank?
|
||||
initialize_schema_migrations_table
|
||||
assume_migrated_upto_version(info[:version], schema.migrations_path)
|
||||
assume_migrated_upto_version(info[:version], schema.migrations_paths)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1275,6 +1275,18 @@ if ActiveRecord::Base.connection.supports_migrations?
|
||||
end
|
||||
end
|
||||
|
||||
def test_finds_migrations_from_two_directories
|
||||
directories = [MIGRATIONS_ROOT + '/valid_with_timestamps', MIGRATIONS_ROOT + '/to_copy_with_timestamps']
|
||||
migrations = ActiveRecord::Migrator.new(:up, directories).migrations
|
||||
|
||||
[[20090101010101, "PeopleHaveHobbies"], [20090101010202, "PeopleHaveDescriptions"],
|
||||
[20100101010101, "PeopleHaveLastNames"], [20100201010101, "WeNeedReminders"],
|
||||
[20100301010101, "InnocentJointable"]].each_with_index do |pair, i|
|
||||
assert_equal migrations[i].version, pair.first
|
||||
assert_equal migrations[i].name, pair.last
|
||||
end
|
||||
end
|
||||
|
||||
def test_finds_pending_migrations
|
||||
ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/interleaved/pass_2", 1)
|
||||
migrations = ActiveRecord::Migrator.new(:up, MIGRATIONS_ROOT + "/interleaved/pass_2").pending_migrations
|
||||
|
||||
Reference in New Issue
Block a user