2021-08-26 18:10:35 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class PrepareCiBuildsMetadataAndCiBuildAsyncIndexes < ActiveRecord::Migration[6.1]
|
|
|
|
include Gitlab::Database::MigrationHelpers
|
|
|
|
|
|
|
|
def up
|
|
|
|
prepare_async_index :ci_builds_metadata, :id_convert_to_bigint, unique: true,
|
2022-08-02 12:09:26 +00:00
|
|
|
name: :index_ci_builds_metadata_on_id_convert_to_bigint
|
2021-08-26 18:10:35 +00:00
|
|
|
|
|
|
|
prepare_async_index :ci_builds_metadata, :build_id_convert_to_bigint, unique: true,
|
2022-08-02 12:09:26 +00:00
|
|
|
name: :index_ci_builds_metadata_on_build_id_convert_to_bigint
|
2021-08-26 18:10:35 +00:00
|
|
|
|
|
|
|
prepare_async_index :ci_builds_metadata, :build_id_convert_to_bigint, where: 'has_exposed_artifacts IS TRUE',
|
2022-08-02 12:09:26 +00:00
|
|
|
name: :index_ci_builds_metadata_on_build_id_int8_and_exposed_artifacts
|
2021-08-26 18:10:35 +00:00
|
|
|
|
|
|
|
prepare_async_index_from_sql(:ci_builds_metadata, :index_ci_builds_metadata_on_build_id_int8_where_interruptible, <<~SQL.squish)
|
|
|
|
CREATE INDEX CONCURRENTLY "index_ci_builds_metadata_on_build_id_int8_where_interruptible"
|
|
|
|
ON "ci_builds_metadata" ("build_id_convert_to_bigint") INCLUDE ("id_convert_to_bigint")
|
|
|
|
WHERE interruptible = true
|
|
|
|
SQL
|
|
|
|
|
|
|
|
prepare_async_index :ci_builds, :id_convert_to_bigint, unique: true,
|
2022-08-02 12:09:26 +00:00
|
|
|
name: :index_ci_builds_on_converted_id
|
2021-08-26 18:10:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
unprepare_async_index_by_name :ci_builds, :index_ci_builds_on_converted_id
|
|
|
|
|
|
|
|
unprepare_async_index_by_name :ci_builds_metadata, :index_ci_builds_metadata_on_build_id_int8_where_interruptible
|
|
|
|
|
|
|
|
unprepare_async_index_by_name :ci_builds_metadata, :index_ci_builds_metadata_on_build_id_int8_and_exposed_artifacts
|
|
|
|
|
|
|
|
unprepare_async_index_by_name :ci_builds_metadata, :index_ci_builds_metadata_on_build_id_convert_to_bigint
|
|
|
|
|
|
|
|
unprepare_async_index_by_name :ci_builds_metadata, :index_ci_builds_metadata_on_id_convert_to_bigint
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def prepare_async_index_from_sql(table_name, index_name, definition)
|
|
|
|
return unless async_index_creation_available?
|
|
|
|
|
|
|
|
return if index_name_exists?(table_name, index_name)
|
|
|
|
|
2021-10-28 21:10:02 +00:00
|
|
|
async_index = Gitlab::Database::AsyncIndexes::PostgresAsyncIndex.find_or_create_by!(name: index_name) do |rec|
|
2021-08-26 18:10:35 +00:00
|
|
|
rec.table_name = table_name
|
|
|
|
rec.definition = definition
|
|
|
|
end
|
|
|
|
|
|
|
|
Gitlab::AppLogger.info(
|
|
|
|
message: 'Prepared index for async creation',
|
|
|
|
table_name: async_index.table_name,
|
|
|
|
index_name: async_index.name)
|
|
|
|
end
|
|
|
|
end
|