BigW Consortium Gitlab

Commit 5199dcd7 by Michael Kozono

Fix orphan temp table untracked_files_for_uploads

parent 04721479
...@@ -39,7 +39,11 @@ module Gitlab ...@@ -39,7 +39,11 @@ module Gitlab
store_untracked_file_paths store_untracked_file_paths
schedule_populate_untracked_uploads_jobs if UntrackedFile.all.empty?
drop_temp_table
else
schedule_populate_untracked_uploads_jobs
end
end end
private private
...@@ -158,6 +162,11 @@ module Gitlab ...@@ -158,6 +162,11 @@ module Gitlab
bulk_queue_background_migration_jobs_by_range( bulk_queue_background_migration_jobs_by_range(
UntrackedFile, FOLLOW_UP_MIGRATION) UntrackedFile, FOLLOW_UP_MIGRATION)
end end
def drop_temp_table
UntrackedFile.connection.drop_table(:untracked_files_for_uploads,
if_exists: true)
end
end end
end end
end end
...@@ -8,8 +8,6 @@ describe Gitlab::BackgroundMigration::PrepareUntrackedUploads, :sidekiq do ...@@ -8,8 +8,6 @@ describe Gitlab::BackgroundMigration::PrepareUntrackedUploads, :sidekiq do
before do before do
DatabaseCleaner.clean DatabaseCleaner.clean
drop_temp_table_if_exists
end end
after do after do
...@@ -23,25 +21,25 @@ describe Gitlab::BackgroundMigration::PrepareUntrackedUploads, :sidekiq do ...@@ -23,25 +21,25 @@ describe Gitlab::BackgroundMigration::PrepareUntrackedUploads, :sidekiq do
end end
end end
it 'ensures the untracked_files_for_uploads table exists' do # E.g. The installation is in use at the time of migration, and someone has
expect do # just uploaded a file
described_class.new.perform shared_examples 'does not add files in /uploads/tmp' do
end.to change { ActiveRecord::Base.connection.table_exists?(:untracked_files_for_uploads) }.from(false).to(true) let(:tmp_file) { Rails.root.join(described_class::ABSOLUTE_UPLOAD_DIR, 'tmp', 'some_file.jpg') }
end
it 'has a path field long enough for really long paths' do before do
described_class.new.perform FileUtils.mkdir(File.dirname(tmp_file))
FileUtils.touch(tmp_file)
end
component = 'a' * 255 after do
FileUtils.rm(tmp_file)
end
long_path = [ it 'does not add files from /uploads/tmp' do
'uploads', described_class.new.perform
component, # project.full_path
component # filename
].flatten.join('/')
record = untracked_files_for_uploads.create!(path: long_path) expect(untracked_files_for_uploads.count).to eq(5)
expect(record.reload.path.size).to eq(519) end
end end
context "test bulk insert with ON CONFLICT DO NOTHING or IGNORE" do context "test bulk insert with ON CONFLICT DO NOTHING or IGNORE" do
...@@ -69,6 +67,21 @@ describe Gitlab::BackgroundMigration::PrepareUntrackedUploads, :sidekiq do ...@@ -69,6 +67,21 @@ describe Gitlab::BackgroundMigration::PrepareUntrackedUploads, :sidekiq do
UploadService.new(project2, uploaded_file, FileUploader).execute UploadService.new(project2, uploaded_file, FileUploader).execute
end end
it 'has a path field long enough for really long paths' do
described_class.new.perform
component = 'a' * 255
long_path = [
'uploads',
component, # project.full_path
component # filename
].flatten.join('/')
record = untracked_files_for_uploads.create!(path: long_path)
expect(record.reload.path.size).to eq(519)
end
it 'adds unhashed files to the untracked_files_for_uploads table' do it 'adds unhashed files to the untracked_files_for_uploads table' do
described_class.new.perform described_class.new.perform
...@@ -109,24 +122,8 @@ describe Gitlab::BackgroundMigration::PrepareUntrackedUploads, :sidekiq do ...@@ -109,24 +122,8 @@ describe Gitlab::BackgroundMigration::PrepareUntrackedUploads, :sidekiq do
end end
end end
# E.g. The installation is in use at the time of migration, and someone has
# just uploaded a file
context 'when there are files in /uploads/tmp' do context 'when there are files in /uploads/tmp' do
let(:tmp_file) { Rails.root.join(described_class::ABSOLUTE_UPLOAD_DIR, 'tmp', 'some_file.jpg') } it_behaves_like 'does not add files in /uploads/tmp'
before do
FileUtils.touch(tmp_file)
end
after do
FileUtils.rm(tmp_file)
end
it 'does not add files from /uploads/tmp' do
described_class.new.perform
expect(untracked_files_for_uploads.count).to eq(5)
end
end end
context 'when the last batch size exactly matches the max batch size' do context 'when the last batch size exactly matches the max batch size' do
...@@ -209,24 +206,8 @@ describe Gitlab::BackgroundMigration::PrepareUntrackedUploads, :sidekiq do ...@@ -209,24 +206,8 @@ describe Gitlab::BackgroundMigration::PrepareUntrackedUploads, :sidekiq do
end end
end end
# E.g. The installation is in use at the time of migration, and someone has
# just uploaded a file
context 'when there are files in /uploads/tmp' do context 'when there are files in /uploads/tmp' do
let(:tmp_file) { Rails.root.join(described_class::ABSOLUTE_UPLOAD_DIR, 'tmp', 'some_file.jpg') } it_behaves_like 'does not add files in /uploads/tmp'
before do
FileUtils.touch(tmp_file)
end
after do
FileUtils.rm(tmp_file)
end
it 'does not add files from /uploads/tmp' do
described_class.new.perform
expect(untracked_files_for_uploads.count).to eq(5)
end
end end
context 'when the last batch size exactly matches the max batch size' do context 'when the last batch size exactly matches the max batch size' do
...@@ -246,10 +227,10 @@ describe Gitlab::BackgroundMigration::PrepareUntrackedUploads, :sidekiq do ...@@ -246,10 +227,10 @@ describe Gitlab::BackgroundMigration::PrepareUntrackedUploads, :sidekiq do
# Very new or lightly-used installations that are running this migration # Very new or lightly-used installations that are running this migration
# may not have an upload directory because they have no uploads. # may not have an upload directory because they have no uploads.
context 'when no files were ever uploaded' do context 'when no files were ever uploaded' do
it 'does not add to the untracked_files_for_uploads table (and does not raise error)' do it 'deletes the `untracked_files_for_uploads` table (and does not raise error)' do
described_class.new.perform described_class.new.perform
expect(untracked_files_for_uploads.count).to eq(0) expect(untracked_files_for_uploads.connection.table_exists?(:untracked_files_for_uploads)).to be_falsey
end end
end end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment