class ActiveRecord::Migrator

Public Class Methods

shard_status(versions) click to toggle source

public list of pending and missing versions per shard

{1 => [1234567]}, {1 => [2345678]}
# File lib/active_record_shards/migration.rb, line 61
def self.shard_status(versions)
  pending = {}
  missing = {}

  collect = lambda do |shard|
    migrated = shards_migration_context.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) }

  [pending, missing]
end
shards_migration_context() click to toggle source
# File lib/active_record_shards/migration.rb, line 5
def self.shards_migration_context
  if ActiveRecord::VERSION::MAJOR >= 6
    ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths, ActiveRecord::SchemaMigration)
  elsif ActiveRecord::VERSION::STRING >= '5.2.0'
    ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths)
  else
    self
  end
end

Public Instance Methods

initialize(*args)
Also aliased as: initialize_without_sharding
initialize_with_sharding(*args) click to toggle source
# File lib/active_record_shards/migration.rb, line 15
def initialize_with_sharding(*args)
  initialize_without_sharding(*args)

  # Rails creates the internal tables on the unsharded DB. We make them
  # manually on the sharded DBs.
  ActiveRecord::Base.on_all_shards do
    ActiveRecord::SchemaMigration.create_table
    if ActiveRecord::VERSION::MAJOR >= 5
      ActiveRecord::InternalMetadata.create_table
    end
  end
end
Also aliased as: initialize
initialize_without_sharding(*args)
Alias for: initialize
migrate()
Also aliased as: migrate_without_sharding
migrate_with_sharding() click to toggle source
# File lib/active_record_shards/migration.rb, line 37
def migrate_with_sharding
  ActiveRecord::Base.on_shard(nil) { migrate_without_sharding }
  ActiveRecord::Base.on_all_shards { migrate_without_sharding }
end
Also aliased as: migrate
migrate_without_sharding()
Alias for: migrate
migrated() click to toggle source
# File lib/active_record_shards/migration.rb, line 46
def migrated
  self.class.shards_migration_context.get_all_versions
end
pending_migrations() click to toggle source
# File lib/active_record_shards/migration.rb, line 52
def pending_migrations
  pending, _missing = self.class.shard_status(migrations.map(&:version))
  pending = pending.values.flatten
  migrations.select { |m| pending.include?(m.version) }
end
run()
Also aliased as: run_without_sharding
Alias for: run_with_sharding
run_with_sharding() click to toggle source
# File lib/active_record_shards/migration.rb, line 30
def run_with_sharding
  ActiveRecord::Base.on_shard(nil) { run_without_sharding }
  ActiveRecord::Base.on_all_shards { run_without_sharding }
end
Also aliased as: run
run_without_sharding()
Alias for: run