Welcome to mirror list, hosted at ThFree Co, Russian Federation.

export.rb « bulk_imports « models « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5c3f8e4b8d45c5c7bf0aa47661e46842e9b9a4ff (plain)
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
# 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

    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?

    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
    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
  end
end