diff options
-rw-r--r-- | db/post_migrate/20180702135109_fill_file_location_to_job_artifacts.rb | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/db/post_migrate/20180702135109_fill_file_location_to_job_artifacts.rb b/db/post_migrate/20180702135109_fill_file_location_to_job_artifacts.rb new file mode 100644 index 00000000000..2bfd3e029f3 --- /dev/null +++ b/db/post_migrate/20180702135109_fill_file_location_to_job_artifacts.rb @@ -0,0 +1,33 @@ +class FillFileLocationToJobArtifacts < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + BATCH_SIZE = 10_000 + TMP_INDEX = 'tmp_index_ci_job_artifacts_on_id_with_null_file_location'.freeze + CI_JOB_ARTIFACT_FILE_LOCATION_HASHED_PATH = 2 # Equavalant to Ci::JobArtifact.file_locations[:hashed_path] + + disable_ddl_transaction! + + class JobArtifact < ActiveRecord::Base + include EachBatch + self.table_name = 'ci_job_artifacts' + end + + def up + unless index_exists_by_name?(:ci_job_artifacts, TMP_INDEX) + # This partial index is to be removed after the clean-up phase of the background migrations for legacy artifacts. + add_concurrent_index(:ci_job_artifacts, :id, where: 'file_location is NULL', name: TMP_INDEX) + end + + # TODO: Use background migrations? + FillFileLocationToJobArtifacts::JobArtifact.where(file_location: nil).each_batch(of: BATCH_SIZE) do |relation| + relation.update_all(file_location: CI_JOB_ARTIFACT_FILE_LOCATION_HASHED_PATH) + end + end + + def down + if index_exists_by_name?(:ci_job_artifacts, TMP_INDEX) + remove_concurrent_index_by_name(:ci_job_artifacts, TMP_INDEX) + end + end +end |