summaryrefslogtreecommitdiff
path: root/lib/gitlab/database/background_migration/batched_migration.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gitlab/database/background_migration/batched_migration.rb')
-rw-r--r--lib/gitlab/database/background_migration/batched_migration.rb56
1 files changed, 56 insertions, 0 deletions
diff --git a/lib/gitlab/database/background_migration/batched_migration.rb b/lib/gitlab/database/background_migration/batched_migration.rb
new file mode 100644
index 00000000000..0c9add9b355
--- /dev/null
+++ b/lib/gitlab/database/background_migration/batched_migration.rb
@@ -0,0 +1,56 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module Database
+ module BackgroundMigration
+ class BatchedMigration < ActiveRecord::Base # rubocop:disable Rails/ApplicationRecord
+ JOB_CLASS_MODULE = 'Gitlab::BackgroundMigration'
+ BATCH_CLASS_MODULE = "#{JOB_CLASS_MODULE}::BatchingStrategies".freeze
+
+ self.table_name = :batched_background_migrations
+
+ has_many :batched_jobs, foreign_key: :batched_background_migration_id
+ has_one :last_job, -> { order(id: :desc) },
+ class_name: 'Gitlab::Database::BackgroundMigration::BatchedJob',
+ foreign_key: :batched_background_migration_id
+
+ scope :queue_order, -> { order(id: :asc) }
+
+ enum status: {
+ paused: 0,
+ active: 1,
+ aborted: 2,
+ finished: 3
+ }
+
+ def interval_elapsed?
+ last_job.nil? || last_job.created_at <= Time.current - interval
+ end
+
+ def create_batched_job!(min, max)
+ batched_jobs.create!(min_value: min, max_value: max, batch_size: batch_size, sub_batch_size: sub_batch_size)
+ end
+
+ def next_min_value
+ last_job&.max_value&.next || min_value
+ end
+
+ def job_class
+ "#{JOB_CLASS_MODULE}::#{job_class_name}".constantize
+ end
+
+ def batch_class
+ "#{BATCH_CLASS_MODULE}::#{batch_class_name}".constantize
+ end
+
+ def job_class_name=(class_name)
+ write_attribute(:job_class_name, class_name.demodulize)
+ end
+
+ def batch_class_name=(class_name)
+ write_attribute(:batch_class_name, class_name.demodulize)
+ end
+ end
+ end
+ end
+end