2021-04-30 08:12:30 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module BulkImports
|
|
|
|
class Export < ApplicationRecord
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2021-05-27 14:10:52 -04:00
|
|
|
STARTED = 0
|
|
|
|
FINISHED = 1
|
|
|
|
FAILED = -1
|
|
|
|
|
2021-04-30 08:12:30 -04:00
|
|
|
self.table_name = 'bulk_import_exports'
|
|
|
|
|
|
|
|
belongs_to :project, optional: true
|
|
|
|
belongs_to :group, optional: true
|
|
|
|
|
|
|
|
has_one :upload, class_name: 'BulkImports::ExportUpload'
|
|
|
|
|
|
|
|
validates :project, presence: true, unless: :group
|
|
|
|
validates :group, presence: true, unless: :project
|
|
|
|
validates :relation, :status, presence: true
|
|
|
|
|
2021-05-12 05:10:19 -04:00
|
|
|
validate :portable_relation?
|
2021-04-30 08:12:30 -04:00
|
|
|
|
|
|
|
state_machine :status, initial: :started do
|
2021-05-27 14:10:52 -04:00
|
|
|
state :started, value: STARTED
|
|
|
|
state :finished, value: FINISHED
|
|
|
|
state :failed, value: FAILED
|
2021-04-30 08:12:30 -04:00
|
|
|
|
|
|
|
event :start do
|
|
|
|
transition any => :started
|
|
|
|
end
|
|
|
|
|
|
|
|
event :finish do
|
|
|
|
transition started: :finished
|
|
|
|
transition failed: :failed
|
|
|
|
end
|
|
|
|
|
|
|
|
event :fail_op do
|
|
|
|
transition any => :failed
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-12 05:10:19 -04:00
|
|
|
def portable_relation?
|
|
|
|
return unless portable
|
2021-04-30 08:12:30 -04:00
|
|
|
|
2021-05-12 05:10:19 -04:00
|
|
|
errors.add(:relation, 'Unsupported portable relation') unless config.portable_relations.include?(relation)
|
2021-04-30 08:12:30 -04:00
|
|
|
end
|
|
|
|
|
2021-05-12 05:10:19 -04:00
|
|
|
def portable
|
|
|
|
strong_memoize(:portable) do
|
2021-04-30 08:12:30 -04:00
|
|
|
project || group
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def relation_definition
|
2021-05-12 05:10:19 -04:00
|
|
|
config.portable_tree[:include].find { |include| include[relation.to_sym] }
|
2021-04-30 08:12:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def config
|
|
|
|
strong_memoize(:config) do
|
2021-05-12 05:10:19 -04:00
|
|
|
FileTransfer.config_for(portable)
|
2021-04-30 08:12:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|