Class: ActiveRecord::Migrator

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record_shards/migration.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bootstrap_migrations_from_nil_shard(migrations_path, this_migration = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/active_record_shards/migration.rb', line 17

def bootstrap_migrations_from_nil_shard(migrations_path, this_migration=nil)
  migrations = nil
  ActiveRecord::Base.on_shard(nil) do
    migrations = ActiveRecord::Migrator.new(:up, migrations_path).migrated
  end

  puts "inserting #{migrations.size} migrations on all shards..."
  ActiveRecord::Base.on_all_shards do
    migrator = ActiveRecord::Migrator.new(:up, migrations_path)
    migrations.each do |m|
      migrator.__send__(:record_version_state_after_migrating, m)
    end
    if this_migration
      migrator.__send__(:record_version_state_after_migrating, this_migration)
    end
  end
end

.shard_status(versions) ⇒ Object

public list of pending and missing versions per shard

=> [1234567], => [2345678]


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/active_record_shards/migration.rb', line 51

def self.shard_status(versions)
  pending = {}
  missing = {}

  collect = lambda do |shard|
    migrated = get_all_versions

    p = versions - migrated
    pending[shard] = p if p.any?

    m = migrated - versions
    missing[shard] = m if m.any?
  end

  ActiveRecord::Base.on_shard(nil) { collect.call(nil) }
  ActiveRecord::Base.on_all_shards { |shard| collect.call(shard) }

  return pending, missing
end

Instance Method Details

#migratedObject

don’t allow Migrator class to cache versions



37
38
39
# File 'lib/active_record_shards/migration.rb', line 37

def migrated
  self.class.get_all_versions
end

#pending_migrationsObject

list of pending migrations is any migrations that haven’t run on all shards.



42
43
44
45
46
# File 'lib/active_record_shards/migration.rb', line 42

def pending_migrations
  pending, missing = self.class.shard_status(migrations.map(&:version))
  pending = pending.values.flatten
  migrations.select { |m| pending.include?(m.version) }
end