Welcome to mirror list, hosted at ThFree Co, Russian Federation.

20210802131812_finalize_convert_deployments_bigint.rb « post_migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 067b7166cf3fe632b3f337ee0870ad21f358639b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# frozen_string_literal: true

class FinalizeConvertDeploymentsBigint < ActiveRecord::Migration[6.1]
  include Gitlab::Database::MigrationHelpers

  disable_ddl_transaction!

  TABLE_NAME = 'deployments'
  COLUMN_NAME = 'deployable_id'
  COLUMN_NAME_BIGINT = "#{COLUMN_NAME}_convert_to_bigint"
  def up
    ensure_batched_background_migration_is_finished(
      job_class_name: 'CopyColumnUsingBackgroundMigrationJob',
      table_name: TABLE_NAME,
      column_name: 'id',
      job_arguments: [[COLUMN_NAME], [COLUMN_NAME_BIGINT]]
    )

    swap
  end

  def down
    swap
  end

  def swap
    old_index_name = 'index_deployments_on_deployable_type_and_deployable_id'
    bigint_index_name = 'index_deployments_on_deployable_type_and_deployable_id_bigint'
    add_concurrent_index TABLE_NAME, ['deployable_type', COLUMN_NAME_BIGINT], name: bigint_index_name

    with_lock_retries(raise_on_exhaustion: true) do
      # Swap columns
      temp_name = "#{COLUMN_NAME}_tmp"
      execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN #{quote_column_name(COLUMN_NAME)} TO #{quote_column_name(temp_name)}"
      execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN #{quote_column_name(COLUMN_NAME_BIGINT)} TO #{quote_column_name(COLUMN_NAME)}"
      execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN #{quote_column_name(temp_name)} TO #{quote_column_name(COLUMN_NAME_BIGINT)}"

      function_name = Gitlab::Database::UnidirectionalCopyTrigger.on_table(TABLE_NAME, connection: connection).name(COLUMN_NAME, COLUMN_NAME_BIGINT)
      execute "ALTER FUNCTION #{quote_table_name(function_name)} RESET ALL"

      execute "DROP INDEX #{old_index_name}"
      rename_index TABLE_NAME, bigint_index_name, old_index_name
    end
  end
end