2017-11-06 16:44:30 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
require Rails.root.join('db', 'post_migrate', '20171103140253_track_untracked_uploads')
|
|
|
|
|
2017-11-15 05:36:25 -05:00
|
|
|
describe TrackUntrackedUploads, :migration, :sidekiq, :temp_table_may_drop do
|
2017-11-08 18:05:08 -05:00
|
|
|
include TrackUntrackedUploadsHelpers
|
|
|
|
|
2017-11-15 02:15:30 -05:00
|
|
|
let(:untracked_files_for_uploads) { table(:untracked_files_for_uploads) }
|
2017-11-07 22:08:02 -05:00
|
|
|
|
2017-11-06 16:44:30 -05:00
|
|
|
matcher :be_scheduled_migration do
|
|
|
|
match do |migration|
|
|
|
|
BackgroundMigrationWorker.jobs.any? do |job|
|
|
|
|
job['args'] == [migration]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
failure_message do |migration|
|
|
|
|
"Migration `#{migration}` with args `#{expected.inspect}` not scheduled!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-15 05:36:25 -05:00
|
|
|
context 'before running the background migration' do
|
|
|
|
around do |example|
|
|
|
|
# Especially important so the follow-up migration does not get run
|
|
|
|
Sidekiq::Testing.fake! do
|
|
|
|
example.run
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'correctly schedules the follow-up background migration' do
|
2017-11-06 16:44:30 -05:00
|
|
|
migrate!
|
|
|
|
|
|
|
|
expect(described_class::MIGRATION).to be_scheduled_migration
|
|
|
|
expect(BackgroundMigrationWorker.jobs.size).to eq(1)
|
|
|
|
end
|
|
|
|
|
2017-11-15 05:36:25 -05:00
|
|
|
it 'ensures the untracked_files_for_uploads table exists' do
|
|
|
|
expect do
|
|
|
|
migrate!
|
|
|
|
end.to change { table_exists?(:untracked_files_for_uploads) }.from(false).to(true)
|
|
|
|
end
|
2017-11-06 16:44:30 -05:00
|
|
|
|
2017-11-15 05:36:25 -05:00
|
|
|
it 'has a path field long enough for really long paths' do
|
|
|
|
migrate!
|
2017-11-06 16:44:30 -05:00
|
|
|
|
2017-11-15 06:01:35 -05:00
|
|
|
component = 'a' * 255
|
2017-11-08 18:05:08 -05:00
|
|
|
|
2017-11-15 05:36:25 -05:00
|
|
|
long_path = [
|
|
|
|
'uploads',
|
|
|
|
component, # project.full_path
|
|
|
|
component # filename
|
|
|
|
].flatten.join('/')
|
2017-11-08 18:05:08 -05:00
|
|
|
|
2017-11-15 05:36:25 -05:00
|
|
|
record = untracked_files_for_uploads.create!(path: long_path)
|
|
|
|
expect(record.reload.path.size).to eq(519)
|
|
|
|
end
|
2017-11-06 16:44:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with tracked and untracked uploads' do
|
2017-11-15 02:15:30 -05:00
|
|
|
let!(:appearance) { create(:appearance, logo: uploaded_file, header_logo: uploaded_file) }
|
|
|
|
let!(:user1) { create(:user, :with_avatar) }
|
|
|
|
let!(:user2) { create(:user, :with_avatar) }
|
|
|
|
let!(:project1) { create(:project, :with_avatar) }
|
|
|
|
let!(:project2) { create(:project, :with_avatar) }
|
2017-11-07 22:54:28 -05:00
|
|
|
let(:uploads) { table(:uploads) }
|
2017-11-06 16:44:30 -05:00
|
|
|
|
|
|
|
before do
|
2017-11-15 02:15:30 -05:00
|
|
|
UploadService.new(project1, uploaded_file, FileUploader).execute # Markdown upload
|
|
|
|
UploadService.new(project2, uploaded_file, FileUploader).execute # Markdown upload
|
|
|
|
|
|
|
|
# Save expected Upload attributes
|
|
|
|
@appearance_logo_attributes = appearance.uploads.where("path like '%/logo/%'").first.attributes.slice('path', 'uploader', 'size', 'checksum')
|
|
|
|
@appearance_header_logo_attributes = appearance.uploads.where("path like '%/header_logo/%'").first.attributes.slice('path', 'uploader', 'size', 'checksum')
|
|
|
|
@user1_avatar_attributes = user1.uploads.first.attributes.slice('path', 'uploader', 'size', 'checksum')
|
|
|
|
@user2_avatar_attributes = user2.uploads.first.attributes.slice('path', 'uploader', 'size', 'checksum')
|
|
|
|
@project1_avatar_attributes = project1.uploads.first.attributes.slice('path', 'uploader', 'size', 'checksum')
|
|
|
|
@project2_avatar_attributes = project2.uploads.first.attributes.slice('path', 'uploader', 'size', 'checksum')
|
|
|
|
@project1_markdown_attributes = project1.uploads.last.attributes.slice('path', 'uploader', 'size', 'checksum')
|
|
|
|
@project2_markdown_attributes = project2.uploads.last.attributes.slice('path', 'uploader', 'size', 'checksum')
|
|
|
|
|
|
|
|
# Untrack 4 files
|
2017-11-06 16:44:30 -05:00
|
|
|
user2.uploads.delete_all
|
2017-11-15 02:15:30 -05:00
|
|
|
project2.uploads.delete_all # 2 files: avatar and a Markdown upload
|
|
|
|
appearance.uploads.where("path like '%header_logo%'").delete_all
|
2017-11-06 16:44:30 -05:00
|
|
|
end
|
|
|
|
|
2017-11-07 22:54:28 -05:00
|
|
|
it 'tracks untracked uploads' do
|
2017-11-15 05:36:25 -05:00
|
|
|
expect do
|
|
|
|
migrate!
|
|
|
|
end.to change { uploads.count }.from(4).to(8)
|
|
|
|
|
|
|
|
expect(appearance.reload.uploads.where("path like '%/header_logo/%'").first.attributes).to include(@appearance_header_logo_attributes)
|
|
|
|
expect(user2.reload.uploads.first.attributes).to include(@user2_avatar_attributes)
|
|
|
|
expect(project2.reload.uploads.first.attributes).to include(@project2_avatar_attributes)
|
|
|
|
expect(project2.uploads.last.attributes).to include(@project2_markdown_attributes)
|
2017-11-07 22:54:28 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'ignores already-tracked uploads' do
|
2017-11-15 05:36:25 -05:00
|
|
|
migrate!
|
2017-11-07 22:54:28 -05:00
|
|
|
|
2017-11-15 05:36:25 -05:00
|
|
|
expect(appearance.reload.uploads.where("path like '%/logo/%'").first.attributes).to include(@appearance_logo_attributes)
|
|
|
|
expect(user1.reload.uploads.first.attributes).to include(@user1_avatar_attributes)
|
|
|
|
expect(project1.reload.uploads.first.attributes).to include(@project1_avatar_attributes)
|
|
|
|
expect(project1.uploads.last.attributes).to include(@project1_markdown_attributes)
|
2017-11-07 22:08:02 -05:00
|
|
|
end
|
|
|
|
|
2017-11-15 05:36:25 -05:00
|
|
|
it 'the temporary table untracked_files_for_uploads no longer exists' do
|
|
|
|
migrate!
|
2017-11-07 22:08:02 -05:00
|
|
|
|
2017-11-15 05:36:25 -05:00
|
|
|
expect(table_exists?(:untracked_files_for_uploads)).to be_falsey
|
2017-11-06 16:44:30 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|