Class: ActiveRecord::Migrator
- Inherits:
-
Object
- Object
- ActiveRecord::Migrator
- Defined in:
- lib/active_record_shards/migration.rb
Class Method Summary collapse
-
.shard_status(versions) ⇒ Object
public list of pending and missing versions per shard [=> [1234567], => [2345678]].
- .shards_migration_context ⇒ Object
Instance Method Summary collapse
- #initialize_with_sharding(*args) ⇒ Object (also: #initialize)
- #migrate_with_sharding ⇒ Object (also: #migrate)
- #migrated ⇒ Object
- #pending_migrations ⇒ Object
- #run_with_sharding ⇒ Object (also: #run)
Class Method Details
.shard_status(versions) ⇒ Object
public list of pending and missing versions per shard
- => [1234567], => [2345678]
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# 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 ⇒ Object
5 6 7 8 9 10 11 12 13 |
# 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 |
Instance Method Details
#initialize_with_sharding(*args) ⇒ Object Also known as: initialize
15 16 17 18 19 20 21 22 23 24 25 26 |
# 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 |
#migrate_with_sharding ⇒ Object Also known as: migrate
37 38 39 40 |
# 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 |
#migrated ⇒ Object
46 47 48 |
# File 'lib/active_record_shards/migration.rb', line 46 def migrated self.class.shards_migration_context.get_all_versions end |
#pending_migrations ⇒ Object
52 53 54 55 56 |
# 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_with_sharding ⇒ Object Also known as: run
30 31 32 33 |
# 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 |