Module: Gitlab::Database::DynamicModelHelpers
- Included in:
- BackgroundMigration::AlterWebhookDeletedAuditEvent, BackgroundMigration::BackfillIssueSearchData, BackgroundMigration::BackfillMemberNamespaceForGroupMembers, BackgroundMigration::BackfillNamespaceIdForProjectRoute, BackgroundMigration::BackfillProjectSettings, BackgroundMigration::BatchedMigrationJob, BackgroundMigration::BatchingStrategies::LooseIndexScanBatchingStrategy, BackgroundMigration::BatchingStrategies::PrimaryKeyBatchingStrategy, BackgroundMigration::CleanupOrphanedRoutes, BackgroundMigration::MarkDuplicateIssuableResourceLinks, MigrationHelpers, Migrations::TestBatchedBackgroundRunner, PartitioningMigrationHelpers::BackfillPartitionedTable
- Defined in:
- lib/gitlab/database/dynamic_model_helpers.rb
Constant Summary collapse
- BATCH_SIZE =
1_000
Instance Method Summary collapse
- #define_batchable_model(table_name, connection:, primary_key: nil, base_class: ActiveRecord::Base) ⇒ Object
- #each_batch(table_name, connection:, scope: ->(table) { table.all }, of: BATCH_SIZE, **opts) ⇒ Object
- #each_batch_range(table_name, connection:, scope: ->(table) { table.all }, of: BATCH_SIZE, **opts) ⇒ Object
Instance Method Details
#define_batchable_model(table_name, connection:, primary_key: nil, base_class: ActiveRecord::Base) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/gitlab/database/dynamic_model_helpers.rb', line 8 def define_batchable_model(table_name, connection:, primary_key: nil, base_class: ActiveRecord::Base) klass = Class.new(base_class) do include EachBatch self.table_name = table_name self.inheritance_column = :_type_disabled end klass.primary_key = primary_key if connection.primary_keys(table_name).length > 1 klass.connection = connection klass end |
#each_batch(table_name, connection:, scope: ->(table) { table.all }, of: BATCH_SIZE, **opts) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/gitlab/database/dynamic_model_helpers.rb', line 21 def each_batch(table_name, connection:, scope: ->(table) { table.all }, of: BATCH_SIZE, **opts) if transaction_open? raise <<~MSG.squish each_batch should not run inside a transaction, you can disable transactions by calling disable_ddl_transaction! in the body of your migration class MSG end opts.select! { |k, _| [:column].include? k } batchable_model = define_batchable_model(table_name, connection: connection) scope.call(batchable_model) .each_batch(of: of, **opts) { |batch| yield batch, batchable_model } end |
#each_batch_range(table_name, connection:, scope: ->(table) { table.all }, of: BATCH_SIZE, **opts) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/gitlab/database/dynamic_model_helpers.rb', line 38 def each_batch_range(table_name, connection:, scope: ->(table) { table.all }, of: BATCH_SIZE, **opts) opts.select! { |k, _| [:column].include? k } each_batch(table_name, connection: connection, scope: scope, of: of, **opts) do |batch, batchable_model| column = opts.fetch(:column, batchable_model.primary_key) yield batch.pick("MIN(#{column}), MAX(#{column})") end end |