summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/background_migration/legacy_uploads_migrator_spec.rb
blob: ed8cbfeb11f20e4af99080af00a1628c37fe020e (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
# frozen_string_literal: true
require 'spec_helper'

# rubocop: disable RSpec/FactoriesInMigrationSpecs
describe Gitlab::BackgroundMigration::LegacyUploadsMigrator do
  let(:test_dir) { FileUploader.options['storage_path'] }

  let!(:hashed_project) { create(:project) }
  let!(:legacy_project) { create(:project, :legacy_storage) }
  let!(:issue) { create(:issue, project: hashed_project) }
  let!(:issue_legacy) { create(:issue, project: legacy_project) }

  let!(:note1) { create(:note, project: hashed_project, noteable: issue) }
  let!(:note2) { create(:note, project: hashed_project, noteable: issue) }
  let!(:note_legacy) { create(:note, project: legacy_project, noteable: issue_legacy) }

  def create_upload(model, with_file = true)
    filename = 'image.png'
    params = {
      path: "uploads/-/system/note/attachment/#{model.id}/#{filename}",
      model: model,
      store: ObjectStorage::Store::LOCAL
    }

    if with_file
      upload = create(:upload, :with_file, :attachment_upload, params)
      model.update(attachment: upload.build_uploader)
      model.attachment.upload
    else
      create(:upload, :attachment_upload, params)
    end
  end

  let!(:legacy_upload) { create_upload(note1) }
  let!(:legacy_upload_no_file) { create_upload(note2, false) }
  let!(:legacy_upload_legacy_project) { create_upload(note_legacy) }

  let(:start_id) { 1 }
  let(:end_id) { 10000 }

  subject { described_class.new.perform(start_id, end_id) }

  it 'removes all legacy files' do
    expect(File.exist?(legacy_upload.absolute_path)).to be_truthy
    expect(File.exist?(legacy_upload_no_file.absolute_path)).to be_falsey
    expect(File.exist?(legacy_upload_legacy_project.absolute_path)).to be_truthy

    subject

    expect(File.exist?(legacy_upload.absolute_path)).to be_falsey
    expect(File.exist?(legacy_upload_no_file.absolute_path)).to be_falsey
    expect(File.exist?(legacy_upload_legacy_project.absolute_path)).to be_falsey
  end

  it 'removes all AttachmentUploader records' do
    expect { subject }.to change { Upload.where(uploader: 'AttachmentUploader').count }.from(3).to(0)
  end

  it 'creates new uploads for successfully migrated records' do
    expect { subject }.to change { Upload.where(uploader: 'FileUploader').count }.from(0).to(2)
  end
end
# rubocop: enable RSpec/FactoriesInMigrationSpecs