Commit c0dc8634 authored by Mike Kozono's avatar Mike Kozono

Ensure filled file_store on packages_package_files

parent 5f5c0dae
# frozen_string_literal: true
class EnsureFilledFileStoreOnPackageFiles < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
BACKGROUND_MIGRATION_CLASS = 'SetNullPackageFilesFileStoreToLocalValue'
BATCH_SIZE = 5_000
LOCAL_STORE = 1 # equal to ObjectStorage::Store::LOCAL
DOWNTIME = false
disable_ddl_transaction!
module Packages
class PackageFile < ActiveRecord::Base
self.table_name = 'packages_package_files'
include ::EachBatch
end
end
def up
Gitlab::BackgroundMigration.steal(BACKGROUND_MIGRATION_CLASS)
# Do a manual update in case we lost BG jobs. The expected record count should be 0 or very low.
Packages::PackageFile.where(file_store: nil).each_batch(of: BATCH_SIZE) do |batch, index|
batch.update_all(file_store: LOCAL_STORE)
end
end
def down
# no-op
end
end
e1ae80d6f0a6372bb329d45257d9a0a8ca5b6a83718d2a10ee295b8c4c97f60e
\ No newline at end of file
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20200915185707_ensure_filled_file_store_on_package_files.rb')
RSpec.describe EnsureFilledFileStoreOnPackageFiles, schema: 20200910175553 do
let!(:packages_package_files) { table(:packages_package_files) }
let!(:packages_packages) { table(:packages_packages) }
let!(:namespaces) { table(:namespaces) }
let!(:projects) { table(:projects) }
let!(:namespace) { namespaces.create!(name: 'foo', path: 'foo') }
let!(:project) { projects.create!(namespace_id: namespace.id) }
let!(:package) { packages_packages.create!(project_id: project.id, name: 'bar', package_type: 1) }
before do
constraint_name = 'check_4c5e6bb0b3'
# In order to insert a row with a NULL to fill.
ActiveRecord::Base.connection.execute "ALTER TABLE packages_package_files DROP CONSTRAINT #{constraint_name}"
@file_store_1 = packages_package_files.create!(file_store: 1, file_name: 'foo_1', file: 'foo_1', package_id: package.id)
@file_store_2 = packages_package_files.create!(file_store: 2, file_name: 'foo_2', file: 'foo_2', package_id: package.id)
@file_store_nil = packages_package_files.create!(file_store: nil, file_name: 'foo_nil', file: 'foo_nil', package_id: package.id)
# revert DB structure
ActiveRecord::Base.connection.execute "ALTER TABLE packages_package_files ADD CONSTRAINT #{constraint_name} CHECK ((file_store IS NOT NULL)) NOT VALID"
end
it 'correctly migrates nil file_store to 1' do
migrate!
@file_store_1.reload
@file_store_2.reload
@file_store_nil.reload
expect(@file_store_1.file_store).to eq(1) # unchanged
expect(@file_store_2.file_store).to eq(2) # unchanged
expect(@file_store_nil.file_store).to eq(1) # nil => 1
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