2020-07-10 17:09:29 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Database
|
|
|
|
class BackgroundMigrationJob < ActiveRecord::Base # rubocop:disable Rails/ApplicationRecord
|
2020-09-03 20:09:08 -04:00
|
|
|
include EachBatch
|
|
|
|
|
2020-07-10 17:09:29 -04:00
|
|
|
self.table_name = :background_migration_jobs
|
|
|
|
|
2020-07-16 08:09:22 -04:00
|
|
|
scope :for_migration_class, -> (class_name) { where(class_name: normalize_class_name(class_name)) }
|
2020-07-10 17:09:29 -04:00
|
|
|
scope :for_migration_execution, -> (class_name, arguments) do
|
2020-07-16 08:09:22 -04:00
|
|
|
for_migration_class(class_name).where('arguments = ?', arguments.to_json)
|
|
|
|
end
|
|
|
|
|
|
|
|
scope :for_partitioning_migration, -> (class_name, table_name) do
|
|
|
|
for_migration_class(class_name).where('arguments ->> 2 = ?', table_name)
|
2020-07-10 17:09:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
enum status: {
|
|
|
|
pending: 0,
|
|
|
|
succeeded: 1
|
|
|
|
}
|
|
|
|
|
|
|
|
def self.mark_all_as_succeeded(class_name, arguments)
|
|
|
|
self.pending.for_migration_execution(class_name, arguments)
|
|
|
|
.update_all("status = #{statuses[:succeeded]}, updated_at = NOW()")
|
|
|
|
end
|
2020-07-14 20:09:23 -04:00
|
|
|
|
|
|
|
def self.normalize_class_name(class_name)
|
|
|
|
return class_name unless class_name.present? && class_name.start_with?('::')
|
|
|
|
|
|
|
|
class_name[2..]
|
|
|
|
end
|
|
|
|
|
|
|
|
def class_name=(value)
|
|
|
|
write_attribute(:class_name, self.class.normalize_class_name(value))
|
|
|
|
end
|
2020-07-10 17:09:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|