1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
# frozen_string_literal: true
module BulkImports
class Export < ApplicationRecord
include Gitlab::Utils::StrongMemoize
STARTED = 0
FINISHED = 1
FAILED = -1
self.table_name = 'bulk_import_exports'
belongs_to :project, optional: true
belongs_to :group, optional: true
belongs_to :user, optional: true
has_one :upload, class_name: 'BulkImports::ExportUpload'
has_many :batches, class_name: 'BulkImports::ExportBatch'
validates :project, presence: true, unless: :group
validates :group, presence: true, unless: :project
validates :relation, :status, presence: true
validate :portable_relation?
scope :for_status, ->(status) { where(status: status) }
scope :for_user, ->(user) { where(user: user) }
scope :for_user_and_relation, ->(user, relation) { where(user: user, relation: relation) }
state_machine :status, initial: :started do
state :started, value: STARTED
state :finished, value: FINISHED
state :failed, value: FAILED
event :start do
transition any => :started
end
event :finish do
transition any => :finished
end
event :fail_op do
transition any => :failed
end
after_transition any => :finished do |export|
if export.config.user_contributions_relation?(export.relation)
UserContributionsExportMapper.new(export.portable).clear_cache
end
end
end
def portable_relation?
return unless portable
errors.add(:relation, 'Unsupported portable relation') unless config.portable_relations.include?(relation)
end
def portable
strong_memoize(:portable) do
project || group
end
end
def relation_definition
config.relation_definition_for(relation)
end
def config
strong_memoize(:config) do
FileTransfer.config_for(portable)
end
end
def remove_existing_upload!
return unless upload&.export_file&.file
upload.remove_export_file!
upload.save!
end
def relation_has_user_contributions?
config.relation_has_user_contributions?(relation)
end
end
end
|