Module: Gitlab::Database::MigrationHelpers

Includes:
AsyncIndexes::MigrationHelpers, DynamicModelHelpers, Gitlab::Database::Migrations::BackgroundMigrationHelpers, Gitlab::Database::Migrations::BatchedBackgroundMigrationHelpers, RenameTableHelpers
Included in:
BackgroundMigration::DeleteOrphanedDeployments, BackgroundMigration::LegacyUploadsMigrator, BackgroundMigration::RemoveAllTraceExpirationDates, CascadingNamespaceSettings, V2, PartitioningMigrationHelpers::IndexHelpers, PartitioningMigrationHelpers::TableManagementHelpers
Defined in:
lib/gitlab/database/migration_helpers.rb,
lib/gitlab/database/migration_helpers/v2.rb,
lib/gitlab/database/migration_helpers/restrict_gitlab_schema.rb,
lib/gitlab/database/migration_helpers/loose_foreign_key_helpers.rb,
lib/gitlab/database/migration_helpers/cascading_namespace_settings.rb

Defined Under Namespace

Modules: CascadingNamespaceSettings, LooseForeignKeyHelpers, RestrictGitlabSchema, V2

Constant Summary collapse

MAX_IDENTIFIER_NAME_LENGTH =
63
DEFAULT_TIMESTAMP_COLUMNS =
%i[created_at updated_at].freeze

Constants included from DynamicModelHelpers

DynamicModelHelpers::BATCH_SIZE

Constants included from Gitlab::Database::Migrations::BatchedBackgroundMigrationHelpers

Gitlab::Database::Migrations::BatchedBackgroundMigrationHelpers::BATCH_CLASS_NAME, Gitlab::Database::Migrations::BatchedBackgroundMigrationHelpers::BATCH_MIN_DELAY, Gitlab::Database::Migrations::BatchedBackgroundMigrationHelpers::BATCH_MIN_VALUE, Gitlab::Database::Migrations::BatchedBackgroundMigrationHelpers::BATCH_SIZE, Gitlab::Database::Migrations::BatchedBackgroundMigrationHelpers::SUB_BATCH_SIZE

Constants included from Gitlab::Database::Migrations::BackgroundMigrationHelpers

Gitlab::Database::Migrations::BackgroundMigrationHelpers::BATCH_SIZE, Gitlab::Database::Migrations::BackgroundMigrationHelpers::JOB_BUFFER_SIZE

Instance Method Summary collapse

Methods included from AsyncIndexes::MigrationHelpers

#async_index_creation_available?, #prepare_async_index, #unprepare_async_index, #unprepare_async_index_by_name

Methods included from RenameTableHelpers

#finalize_table_rename, #rename_table_safely, #undo_finalize_table_rename, #undo_rename_table_safely

Methods included from Gitlab::Database::Migrations::BatchedBackgroundMigrationHelpers

#queue_batched_background_migration

Methods included from Gitlab::Database::Migrations::BackgroundMigrationHelpers

#delete_job_tracking, #delete_queued_jobs, #finalize_background_migration, #migrate_in, #queue_background_migration_jobs_by_range_at_intervals, #requeue_background_migration_jobs_by_range_at_intervals

Instance Method Details

#add_check_constraint(table, check, constraint_name, validate: true) ⇒ Object

Adds a check constraint to a table

This method is the generic helper for adding any check constraint More specialized helpers may use it (e.g. add_text_limit or add_not_null)

This method only requires minimal locking:

  • The constraint is added using NOT VALID This allows us to add the check constraint without validating it

  • The check will be enforced for new data (inserts) coming in

  • If `validate: true` the constraint is also validated Otherwise, validate_check_constraint() can be used at a later stage

  • Check comments on add_concurrent_foreign_key for more info

table - The table the constraint will be added to check - The check clause to add

e.g. 'char_length(name) <= 5' or 'store IS NOT NULL'

constraint_name - The name of the check constraint (otherwise auto-generated)

Should be unique per table (not per column)

validate - Whether to validate the constraint in this call


1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
# File 'lib/gitlab/database/migration_helpers.rb', line 1242

def add_check_constraint(table, check, constraint_name, validate: true)
  # Transactions would result in ALTER TABLE locks being held for the
  # duration of the transaction, defeating the purpose of this method.
  validate_not_in_transaction!(:add_check_constraint)

  validate_check_constraint_name!(constraint_name)

  if check_constraint_exists?(table, constraint_name)
    warning_message = <<~MESSAGE
      Check constraint was not created because it exists already
      (this may be due to an aborted migration or similar)
      table: #{table}, check: #{check}, constraint name: #{constraint_name}
    MESSAGE

    Gitlab::AppLogger.warn warning_message
  else
    # Only add the constraint without validating it
    # Even though it is fast, ADD CONSTRAINT requires an EXCLUSIVE lock
    # Use with_lock_retries to make sure that this operation
    # will not timeout on tables accessed by many processes
    with_lock_retries do
      execute <<-EOF.strip_heredoc
      ALTER TABLE #{table}
      ADD CONSTRAINT #{constraint_name}
      CHECK ( #{check} )
      NOT VALID;
      EOF
    end
  end

  if validate
    validate_check_constraint(table, constraint_name)
  end
end

#add_column_with_default(table, column, type, default:, limit: nil, allow_null: false) ⇒ Object

Deprecated.

With PostgreSQL 11, adding columns with a default does not lead to a table rewrite anymore. As such, this method is not needed anymore and the default `add_column` helper should be used. This helper is subject to be removed in a >13.0 release.

Adds a column with a default value without locking an entire table.


573
574
575
576
577
# File 'lib/gitlab/database/migration_helpers.rb', line 573

def add_column_with_default(table, column, type, default:, limit: nil, allow_null: false)
  raise 'Deprecated: add_column_with_default does not support being passed blocks anymore' if block_given?

  add_column(table, column, type, default: default, limit: limit, null: allow_null)
end

#add_concurrent_foreign_key(source, target, column:, on_delete: :cascade, target_column: :id, name: nil, validate: true, reverse_lock_order: false) ⇒ Object

Adds a foreign key with only minimal locking on the tables involved.

This method only requires minimal locking

source - The source table containing the foreign key. target - The target table the key points to. column - The name of the column to create the foreign key on. target_column - The name of the referenced column, defaults to “id”. on_delete - The action to perform when associated data is removed,

defaults to "CASCADE".

name - The name of the foreign key. validate - Flag that controls whether the new foreign key will be validated after creation.

If the flag is not set, the constraint will only be enforced for new data.

reverse_lock_order - Flag that controls whether we should attempt to acquire locks in the reverse

order of the ALTER TABLE. This can be useful in situations where the foreign
key creation could deadlock with another process.

269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/gitlab/database/migration_helpers.rb', line 269

def add_concurrent_foreign_key(source, target, column:, on_delete: :cascade, target_column: :id, name: nil, validate: true, reverse_lock_order: false)
  # Transactions would result in ALTER TABLE locks being held for the
  # duration of the transaction, defeating the purpose of this method.
  if transaction_open?
    raise 'add_concurrent_foreign_key can not be run inside a transaction'
  end

  options = {
    column: column,
    on_delete: on_delete,
    name: name.presence || concurrent_foreign_key_name(source, column),
    primary_key: target_column
  }

  if foreign_key_exists?(source, target, **options)
    warning_message = "Foreign key not created because it exists already " \
      "(this may be due to an aborted migration or similar): " \
      "source: #{source}, target: #{target}, column: #{options[:column]}, "\
      "name: #{options[:name]}, on_delete: #{options[:on_delete]}"

    Gitlab::AppLogger.warn warning_message
  else
    # Using NOT VALID allows us to create a key without immediately
    # validating it. This means we keep the ALTER TABLE lock only for a
    # short period of time. The key _is_ enforced for any newly created
    # data.

    with_lock_retries do
      execute("LOCK TABLE #{target}, #{source} IN SHARE ROW EXCLUSIVE MODE") if reverse_lock_order

      execute <<-EOF.strip_heredoc
      ALTER TABLE #{source}
      ADD CONSTRAINT #{options[:name]}
      FOREIGN KEY (#{options[:column]})
      REFERENCES #{target} (#{target_column})
      #{on_delete_statement(options[:on_delete])}
      NOT VALID;
      EOF
    end
  end

  # Validate the existing constraint. This can potentially take a very
  # long time to complete, but fortunately does not lock the source table
  # while running.
  # Disable this check by passing `validate: false` to the method call
  # The check will be enforced for new data (inserts) coming in,
  # but validating existing data is delayed.
  #
  # Note this is a no-op in case the constraint is VALID already

  if validate
    disable_statement_timeout do
      execute("ALTER TABLE #{source} VALIDATE CONSTRAINT #{options[:name]};")
    end
  end
end

#add_concurrent_index(table_name, column_name, options = {}) ⇒ Object

Creates a new index, concurrently

Example:

add_concurrent_index :users, :some_column

See Rails' `add_index` for more info on the available arguments.


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/gitlab/database/migration_helpers.rb', line 141

def add_concurrent_index(table_name, column_name, options = {})
  if transaction_open?
    raise 'add_concurrent_index can not be run inside a transaction, ' \
      'you can disable transactions by calling disable_ddl_transaction! ' \
      'in the body of your migration class'
  end

  options = options.merge({ algorithm: :concurrently })

  if index_exists?(table_name, column_name, **options)
    name = options[:name] || index_name(table_name, column_name)
    _, schema = table_name.to_s.split('.').reverse

    if index_invalid?(name, schema: schema)
      say "Index being recreated because the existing version was INVALID: table_name: #{table_name}, column_name: #{column_name}"

      remove_concurrent_index_by_name(table_name, name)
    else
      say "Index not created because it already exists (this may be due to an aborted migration or similar): table_name: #{table_name}, column_name: #{column_name}"

      return
    end
  end

  disable_statement_timeout do
    add_index(table_name, column_name, **options)
  end

  # We created this index. Now let's remove the queuing entry for async creation in case it's still there.
  unprepare_async_index(table_name, column_name, **options)
end

#add_not_null_constraint(table, column, constraint_name: nil, validate: true) ⇒ Object

Migration Helpers for managing not null constraints


1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
# File 'lib/gitlab/database/migration_helpers.rb', line 1389

def add_not_null_constraint(table, column, constraint_name: nil, validate: true)
  if column_is_nullable?(table, column)
    add_check_constraint(
      table,
      "#{column} IS NOT NULL",
      not_null_constraint_name(table, column, name: constraint_name),
      validate: validate
    )
  else
    warning_message = <<~MESSAGE
      NOT NULL check constraint was not created:
      column #{table}.#{column} is already defined as `NOT NULL`
    MESSAGE

    Gitlab::AppLogger.warn warning_message
  end
end

#add_text_limit(table, column, limit, constraint_name: nil, validate: true) ⇒ Object

Migration Helpers for adding limit to text columns


1367
1368
1369
1370
1371
1372
1373
1374
# File 'lib/gitlab/database/migration_helpers.rb', line 1367

def add_text_limit(table, column, limit, constraint_name: nil, validate: true)
  add_check_constraint(
    table,
    "char_length(#{column}) <= #{limit}",
    text_limit_name(table, column, name: constraint_name),
    validate: validate
  )
end

#add_timestamps_with_timezone(table_name, options = {}) ⇒ Object

Adds `created_at` and `updated_at` columns with timezone information.

This method is an improved version of Rails' built-in method `add_timestamps`.

By default, adds `created_at` and `updated_at` columns, but these can be specified as:

add_timestamps_with_timezone(:my_table, columns: [:created_at, :deleted_at])

This allows you to create just the timestamps you need, saving space.

Available options are:

:default - The default value for the column.
:null - When set to `true` the column will allow NULL values.
      The default is to not allow NULL values.
:columns - the column names to create. Must end with `_at`.
           Default value: `DEFAULT_TIMESTAMP_COLUMNS`

All options are optional.


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gitlab/database/migration_helpers.rb', line 46

def add_timestamps_with_timezone(table_name, options = {})
  columns = options.fetch(:columns, DEFAULT_TIMESTAMP_COLUMNS)

  columns.each do |column_name|
    validate_timestamp_column_name!(column_name)

    add_column(
      table_name,
      column_name,
      :datetime_with_timezone,
      default: options[:default],
      null: options[:null] || false
    )
  end
end

#backfill_conversion_of_integer_to_bigint(table, columns, primary_key: :id, batch_size: 20_000, sub_batch_size: 1000, interval: 2.minutes) ⇒ Object

Backfills the new columns used in an integer-to-bigint conversion using background migrations.

  • This helper should be called from a post-deployment migration.

  • In order for this helper to work properly, the new columns must be first initialized with the `initialize_conversion_of_integer_to_bigint` helper.

  • It tracks the scheduled background jobs through Gitlab::Database::BackgroundMigration::BatchedMigration, which allows a more thorough check that all jobs succeeded in the cleanup migration and is way faster for very large tables.

    Note: this helper is intended to be used in a post-deployment migration, to ensure any new code is deployed (including background job changes) before we begin processing the background migration.

    This helper is part 2 of a multi-step migration process:

    1. initialize_conversion_of_integer_to_bigint to create the new columns and database trigger

    2. backfill_conversion_of_integer_to_bigint to copy historic data using background migrations

    3. remaining steps TBD, see #288005

table - The name of the database table containing the column columns - The name, or an array of names, of the column(s) we want to convert to bigint. primary_key - The name of the primary key column (most often :id) batch_size - The number of rows to schedule in a single background migration sub_batch_size - The smaller batches that will be used by each scheduled job

to update the table. Useful to keep each update at ~100ms while executing
more updates per interval (2.minutes)
Note that each execution of a sub-batch adds a constant 100ms sleep
 time in between the updates, which must be taken into account
 while calculating the batch, sub_batch and interval values.

interval - The time interval between every background migration

example: Assume that we have figured out that updating 200 records of the events

table takes ~100ms on average.

We can set the sub_batch_size to 200, leave the interval to the default

and set the batch_size to 50_000 which will require
~50s = (50000 / 200) * (0.1 + 0.1) to complete and leaves breathing space
between the scheduled jobs

890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
# File 'lib/gitlab/database/migration_helpers.rb', line 890

def backfill_conversion_of_integer_to_bigint(
  table,
  columns,
  primary_key: :id,
  batch_size: 20_000,
  sub_batch_size: 1000,
  interval: 2.minutes
)

  unless table_exists?(table)
    raise "Table #{table} does not exist"
  end

  unless column_exists?(table, primary_key)
    raise "Column #{primary_key} does not exist on #{table}"
  end

  conversions = Array.wrap(columns).to_h do |column|
    raise ArgumentError, "Column #{column} does not exist on #{table}" unless column_exists?(table, column)

    temporary_name = convert_to_bigint_column(column)
    raise ArgumentError, "Column #{temporary_name} does not exist on #{table}" unless column_exists?(table, temporary_name)

    [column, temporary_name]
  end

  queue_batched_background_migration(
    'CopyColumnUsingBackgroundMigrationJob',
    table,
    primary_key,
    conversions.keys,
    conversions.values,
    job_interval: interval,
    batch_size: batch_size,
    sub_batch_size: sub_batch_size)
end

#backfill_iids(table) ⇒ Object

Note this should only be used with very small tables


1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
# File 'lib/gitlab/database/migration_helpers.rb', line 1171

def backfill_iids(table)
  sql = <<-END
    UPDATE #{table}
    SET iid = #{table}_with_calculated_iid.iid_num
    FROM (
      SELECT id, ROW_NUMBER() OVER (PARTITION BY project_id ORDER BY id ASC) AS iid_num FROM #{table}
    ) AS #{table}_with_calculated_iid
    WHERE #{table}.id = #{table}_with_calculated_iid.id
  END

  execute(sql)
end

#change_column_type_concurrently(table, column, new_type, type_cast_function: nil, batch_column_name: :id) ⇒ Object

Changes the type of a column concurrently.

table - The table containing the column. column - The name of the column to change. new_type - The new column type.


654
655
656
657
658
# File 'lib/gitlab/database/migration_helpers.rb', line 654

def change_column_type_concurrently(table, column, new_type, type_cast_function: nil, batch_column_name: :id)
  temp_column = "#{column}_for_type_change"

  rename_column_concurrently(table, column, temp_column, type: new_type, type_cast_function: type_cast_function, batch_column_name: batch_column_name)
end

#check_constraint_exists?(table, constraint_name) ⇒ Boolean

Returns:

  • (Boolean)

1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
# File 'lib/gitlab/database/migration_helpers.rb', line 1202

def check_constraint_exists?(table, constraint_name)
  # Constraint names are unique per table in Postgres, not per schema
  # Two tables can have constraints with the same name, so we filter by
  # the table name in addition to using the constraint_name
  check_sql = <<~SQL
    SELECT COUNT(*)
    FROM pg_catalog.pg_constraint con
      INNER JOIN pg_catalog.pg_class rel
        ON rel.oid = con.conrelid
      INNER JOIN pg_catalog.pg_namespace nsp
        ON nsp.oid = con.connamespace
    WHERE con.contype = 'c'
    AND con.conname = #{connection.quote(constraint_name)}
    AND nsp.nspname = #{connection.quote(current_schema)}
    AND rel.relname = #{connection.quote(table)}
  SQL

  connection.select_value(check_sql) > 0
end

#check_constraint_name(table, column, type) ⇒ Object

Returns the name for a check constraint

type:

  • Any value, as long as it is unique

  • Constraint names are unique per table in Postgres, and, additionally, we can have multiple check constraints over a column So we use the (table, column, type) triplet as a unique name

  • e.g. we use 'max_length' when adding checks for text limits

    or 'not_null' when adding a NOT NULL constraint
    

1194
1195
1196
1197
1198
1199
1200
# File 'lib/gitlab/database/migration_helpers.rb', line 1194

def check_constraint_name(table, column, type)
  identifier = "#{table}_#{column}_check_#{type}"
  # Check concurrent_foreign_key_name() for info on why we use a hash
  hashed_identifier = Digest::SHA256.hexdigest(identifier).first(10)

  "check_#{hashed_identifier}"
end

#check_not_null_constraint_exists?(table, column, constraint_name: nil) ⇒ Boolean

Returns:

  • (Boolean)

1421
1422
1423
1424
1425
1426
# File 'lib/gitlab/database/migration_helpers.rb', line 1421

def check_not_null_constraint_exists?(table, column, constraint_name: nil)
  check_constraint_exists?(
    table,
    not_null_constraint_name(table, column, name: constraint_name)
  )
end

#check_text_limit_exists?(table, column, constraint_name: nil) ⇒ Boolean

Returns:

  • (Boolean)

1384
1385
1386
# File 'lib/gitlab/database/migration_helpers.rb', line 1384

def check_text_limit_exists?(table, column, constraint_name: nil)
  check_constraint_exists?(table, text_limit_name(table, column, name: constraint_name))
end

#check_trigger_permissions!(table) ⇒ Object


1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
# File 'lib/gitlab/database/migration_helpers.rb', line 1106

def check_trigger_permissions!(table)
  unless Grant.create_and_execute_trigger?(table)
    dbname = ApplicationRecord.database.database_name
    user = ApplicationRecord.database.username

    raise <<-EOF
Your database user is not allowed to create, drop, or execute triggers on the
table #{table}.

If you are using PostgreSQL you can solve this by logging in to the GitLab
database (#{dbname}) using a super user and running:

    ALTER #{user} WITH SUPERUSER

This query will grant the user super user permissions, ensuring you don't run
into similar problems in the future (e.g. when new tables are created).
    EOF
  end
end

#cleanup_concurrent_column_rename(table, old, new) ⇒ Object

Cleans up a concurrent column name.

This method takes care of removing previously installed triggers as well as removing the old column.

table - The name of the database table. old - The name of the old column. new - The name of the new column.


756
757
758
759
760
761
762
763
764
# File 'lib/gitlab/database/migration_helpers.rb', line 756

def cleanup_concurrent_column_rename(table, old, new)
  trigger_name = rename_trigger_name(table, old, new)

  check_trigger_permissions!(table)

  remove_rename_triggers(table, trigger_name)

  remove_column(table, old)
end

#cleanup_concurrent_column_type_change(table, column) ⇒ Object

Performs cleanup of a concurrent type change.

table - The table containing the column. column - The name of the column to change. new_type - The new column type.


675
676
677
678
679
680
681
682
683
684
# File 'lib/gitlab/database/migration_helpers.rb', line 675

def cleanup_concurrent_column_type_change(table, column)
  temp_column = "#{column}_for_type_change"

  transaction do
    # This has to be performed in a transaction as otherwise we might have
    # inconsistent data.
    cleanup_concurrent_column_rename(table, column, temp_column)
    rename_column(table, temp_column, column)
  end
end

#column_for(table, name) ⇒ Object

Returns the column for the given table and column name.


1050
1051
1052
1053
1054
1055
1056
1057
# File 'lib/gitlab/database/migration_helpers.rb', line 1050

def column_for(table, name)
  name = name.to_s

  column = columns(table).find { |column| column.name == name }
  raise(missing_schema_object_message(table, "column", name)) if column.nil?

  column
end

#concurrent_foreign_key_name(table, column, prefix: 'fk_') ⇒ Object

Returns the name for a concurrent foreign key.

PostgreSQL constraint names have a limit of 63 bytes. The logic used here is based on Rails' foreign_key_name() method, which unfortunately is private so we can't rely on it directly.

prefix:

  • The default prefix is `fk_` for backward compatibility with the existing

concurrent foreign key helpers.

  • For standard rails foreign keys the prefix is `fk_rails_`


356
357
358
359
360
361
# File 'lib/gitlab/database/migration_helpers.rb', line 356

def concurrent_foreign_key_name(table, column, prefix: 'fk_')
  identifier = "#{table}_#{column}_fk"
  hashed_identifier = Digest::SHA256.hexdigest(identifier).first(10)

  "#{prefix}#{hashed_identifier}"
end

#convert_to_bigint_column(column) ⇒ Object


796
797
798
# File 'lib/gitlab/database/migration_helpers.rb', line 796

def convert_to_bigint_column(column)
  "#{column}_convert_to_bigint"
end

#copy_check_constraints(table, old, new, schema: nil) ⇒ Object

Copies all check constraints for the old column to the new column.

table - The table containing the columns. old - The old column. new - The new column. schema - The schema the table is defined for

If it is not provided, then the current_schema is used

1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
# File 'lib/gitlab/database/migration_helpers.rb', line 1315

def copy_check_constraints(table, old, new, schema: nil)
  if transaction_open?
    raise 'copy_check_constraints can not be run inside a transaction'
  end

  unless column_exists?(table, old)
    raise "Column #{old} does not exist on #{table}"
  end

  unless column_exists?(table, new)
    raise "Column #{new} does not exist on #{table}"
  end

  table_with_schema = schema.present? ? "#{schema}.#{table}" : table

  check_constraints_for(table, old, schema: schema).each do |check_c|
    validate = !(check_c["constraint_def"].end_with? "NOT VALID")

    # Normalize:
    # - Old constraint definitions:
    #    '(char_length(entity_path) <= 5500)'
    # - Definitionss from pg_get_constraintdef(oid):
    #    'CHECK ((char_length(entity_path) <= 5500))'
    # - Definitions from pg_get_constraintdef(oid, pretty_bool):
    #    'CHECK (char_length(entity_path) <= 5500)'
    # - Not valid constraints: 'CHECK (...) NOT VALID'
    # to a single format that we can use:
    #    '(char_length(entity_path) <= 5500)'
    check_definition = check_c["constraint_def"]
                        .sub(/^\s*(CHECK)?\s*\({0,2}/, '(')
                        .sub(/\){0,2}\s*(NOT VALID)?\s*$/, ')')

    constraint_name = begin
      if check_definition == "(#{old} IS NOT NULL)"
        not_null_constraint_name(table_with_schema, new)
      elsif check_definition.start_with? "(char_length(#{old}) <="
        text_limit_name(table_with_schema, new)
      else
        check_constraint_name(table_with_schema, new, 'copy_check_constraint')
      end
    end

    add_check_constraint(
      table_with_schema,
      check_definition.gsub(old.to_s, new.to_s),
      constraint_name,
      validate: validate
    )
  end
end

#copy_foreign_keys(table, old, new) ⇒ Object

Copies all foreign keys for the old column to the new column.

table - The table containing the columns and indexes. old - The old column. new - The new column.


1040
1041
1042
1043
1044
1045
1046
1047
# File 'lib/gitlab/database/migration_helpers.rb', line 1040

def copy_foreign_keys(table, old, new)
  foreign_keys_for(table, old).each do |fk|
    add_concurrent_foreign_key(fk.from_table,
                               fk.to_table,
                               column: new,
                               on_delete: fk.on_delete)
  end
end

#copy_indexes(table, old, new) ⇒ Object

Copies all indexes for the old column to a new column.

table - The table containing the columns and indexes. old - The old column. new - The new column.


993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
# File 'lib/gitlab/database/migration_helpers.rb', line 993

def copy_indexes(table, old, new)
  old = old.to_s
  new = new.to_s

  indexes_for(table, old).each do |index|
    new_columns = index.columns.map do |column|
      column == old ? new : column
    end

    # This is necessary as we can't properly rename indexes such as
    # "ci_taggings_idx".
    unless index.name.include?(old)
      raise "The index #{index.name} can not be copied as it does not "\
        "mention the old column. You have to rename this index manually first."
    end

    name = index.name.gsub(old, new)

    options = {
      unique: index.unique,
      name: name,
      length: index.lengths,
      order: index.orders
    }

    options[:using] = index.using if index.using
    options[:where] = index.where if index.where

    unless index.opclasses.blank?
      opclasses = index.opclasses.dup

      # Copy the operator classes for the old column (if any) to the new
      # column.
      opclasses[new] = opclasses.delete(old) if opclasses[old]

      options[:opclass] = opclasses
    end

    add_concurrent_index(table, new_columns, options)
  end
end

#create_extension(extension) ⇒ Object


1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
# File 'lib/gitlab/database/migration_helpers.rb', line 1428

def create_extension(extension)
  execute('CREATE EXTENSION IF NOT EXISTS %s' % extension)
rescue ActiveRecord::StatementInvalid => e
  dbname = ApplicationRecord.database.database_name
  user = ApplicationRecord.database.username

  warn(<<~MSG) if e.to_s =~ /permission denied/
    GitLab requires the PostgreSQL extension '#{extension}' installed in database '#{dbname}', but
    the database user is not allowed to install the extension.

    You can either install the extension manually using a database superuser:

      CREATE EXTENSION IF NOT EXISTS #{extension}

    Or, you can solve this by logging in to the GitLab
    database (#{dbname}) using a superuser and running:

        ALTER #{user} WITH SUPERUSER

    This query will grant the user superuser permissions, ensuring any database extensions
    can be installed through migrations.

    For more information, refer to https://docs.gitlab.com/ee/install/postgresql_extensions.html.
  MSG

  raise
end

#create_or_update_plan_limit(limit_name, plan_name, limit_value) ⇒ Object


1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
# File 'lib/gitlab/database/migration_helpers.rb', line 1158

def create_or_update_plan_limit(limit_name, plan_name, limit_value)
  limit_name_quoted = quote_column_name(limit_name)
  plan_name_quoted = quote(plan_name)
  limit_value_quoted = quote(limit_value)

  execute <<~SQL
    INSERT INTO plan_limits (plan_id, #{limit_name_quoted})
    SELECT id, #{limit_value_quoted} FROM plans WHERE name = #{plan_name_quoted} LIMIT 1
    ON CONFLICT (plan_id) DO UPDATE SET #{limit_name_quoted} = EXCLUDED.#{limit_name_quoted};
  SQL
end

#create_table_with_constraints(table_name, **options, &block) ⇒ Object

Deprecated.

Use `create_table` in V2 instead

Creates a new table, optionally allowing the caller to add check constraints to the table. Aside from that addition, this method should behave identically to Rails' `create_table` method.

Example:

create_table_with_constraints :some_table do |t|
  t.integer :thing, null: false
  t.text :other_thing

  t.check_constraint :thing_is_not_null, 'thing IS NOT NULL'
  t.text_limit :other_thing, 255
end

See Rails' `create_table` for more info on the available arguments.


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/gitlab/database/migration_helpers.rb', line 93

def create_table_with_constraints(table_name, **options, &block)
  helper_context = self

  with_lock_retries do
    check_constraints = []

    create_table(table_name, **options) do |t|
      t.define_singleton_method(:check_constraint) do |name, definition|
        helper_context.send(:validate_check_constraint_name!, name) # rubocop:disable GitlabSecurity/PublicSend

        check_constraints << { name: name, definition: definition }
      end

      t.define_singleton_method(:text_limit) do |column_name, limit, name: nil|
        # rubocop:disable GitlabSecurity/PublicSend
        name = helper_context.send(:text_limit_name, table_name, column_name, name: name)
        helper_context.send(:validate_check_constraint_name!, name)
        # rubocop:enable GitlabSecurity/PublicSend

        column_name = helper_context.quote_column_name(column_name)
        definition = "char_length(#{column_name}) <= #{limit}"

        check_constraints << { name: name, definition: definition }
      end

      t.instance_eval(&block) unless block.nil?
    end

    next if check_constraints.empty?

    constraint_clauses = check_constraints.map do |constraint|
      "ADD CONSTRAINT #{quote_table_name(constraint[:name])} CHECK (#{constraint[:definition]})"
    end

    execute(<<~SQL)
      ALTER TABLE #{quote_table_name(table_name)}
      #{constraint_clauses.join(",\n")}
    SQL
  end
end

#define_batchable_model(table_name, connection: self.connection) ⇒ Object


12
13
14
# File 'lib/gitlab/database/migration_helpers.rb', line 12

def define_batchable_model(table_name, connection: self.connection)
  super(table_name, connection: connection)
end

#disable_statement_timeoutObject

Long-running migrations may take more than the timeout allowed by the database. Disable the session's statement timeout to ensure migrations don't get killed prematurely.

There are two possible ways to disable the statement timeout:

  • Per transaction (this is the preferred and default mode)

  • Per connection (requires a cleanup after the execution)

When using a per connection disable statement, code must be inside a block so we can automatically execute `RESET statement_timeout` after block finishes otherwise the statement will still be disabled until connection is dropped or `RESET statement_timeout` is executed


376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/gitlab/database/migration_helpers.rb', line 376

def disable_statement_timeout
  if block_given?
    if statement_timeout_disabled?
      # Don't do anything if the statement_timeout is already disabled
      # Allows for nested calls of disable_statement_timeout without
      # resetting the timeout too early (before the outer call ends)
      yield
    else
      begin
        execute('SET statement_timeout TO 0')

        yield
      ensure
        execute('RESET statement_timeout')
      end
    end
  else
    unless transaction_open?
      raise <<~ERROR
        Cannot call disable_statement_timeout() without a transaction open or outside of a transaction block.
        If you don't want to use a transaction wrap your code in a block call:

        disable_statement_timeout { # code that requires disabled statement here }

        This will make sure statement_timeout is disabled before and reset after the block execution is finished.
      ERROR
    end

    execute('SET LOCAL statement_timeout TO 0')
  end
end

#drop_extension(extension) ⇒ Object


1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
# File 'lib/gitlab/database/migration_helpers.rb', line 1456

def drop_extension(extension)
  execute('DROP EXTENSION IF EXISTS %s' % extension)
rescue ActiveRecord::StatementInvalid => e
  dbname = ApplicationRecord.database.database_name
  user = ApplicationRecord.database.username

  warn(<<~MSG) if e.to_s =~ /permission denied/
    This migration attempts to drop the PostgreSQL extension '#{extension}'
    installed in database '#{dbname}', but the database user is not allowed
    to drop the extension.

    You can either drop the extension manually using a database superuser:

      DROP EXTENSION IF EXISTS #{extension}

    Or, you can solve this by logging in to the GitLab
    database (#{dbname}) using a superuser and running:

        ALTER #{user} WITH SUPERUSER

    This query will grant the user superuser permissions, ensuring any database extensions
    can be dropped through migrations.

    For more information, refer to https://docs.gitlab.com/ee/install/postgresql_extensions.html.
  MSG

  raise
end

#each_batch(table_name, connection: self.connection, **kwargs) ⇒ Object


16
17
18
# File 'lib/gitlab/database/migration_helpers.rb', line 16

def each_batch(table_name, connection: self.connection, **kwargs)
  super(table_name, connection: connection, **kwargs)
end

#each_batch_range(table_name, connection: self.connection, **kwargs) ⇒ Object


20
21
22
# File 'lib/gitlab/database/migration_helpers.rb', line 20

def each_batch_range(table_name, connection: self.connection, **kwargs)
  super(table_name, connection: connection, **kwargs)
end

#ensure_batched_background_migration_is_finished(job_class_name:, table_name:, column_name:, job_arguments:) ⇒ Object


946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
# File 'lib/gitlab/database/migration_helpers.rb', line 946

def ensure_batched_background_migration_is_finished(job_class_name:, table_name:, column_name:, job_arguments:)
  migration = Gitlab::Database::BackgroundMigration::BatchedMigration
    .for_configuration(job_class_name, table_name, column_name, job_arguments).first

  configuration = {
    job_class_name: job_class_name,
    table_name: table_name,
    column_name: column_name,
    job_arguments: job_arguments
  }

  if migration.nil?
    Gitlab::AppLogger.warn "Could not find batched background migration for the given configuration: #{configuration}"
  elsif !migration.finished?
    raise "Expected batched background migration for the given configuration to be marked as 'finished', " \
      "but it is '#{migration.status_name}':" \
      "\t#{configuration}" \
      "\n\n" \
      "Finalize it manually by running" \
      "\n\n" \
      "\tsudo gitlab-rake gitlab:background_migrations:finalize[#{job_class_name},#{table_name},#{column_name},'#{job_arguments.to_json.gsub(',', '\,')}']" \
      "\n\n" \
      "For more information, check the documentation" \
      "\n\n" \
      "\thttps://docs.gitlab.com/ee/user/admin_area/monitoring/background_migrations.html#database-migrations-failing-because-of-batched-background-migration-not-finished"
  end
end

#false_valueObject


458
459
460
# File 'lib/gitlab/database/migration_helpers.rb', line 458

def false_value
  Database.false_value
end

#foreign_key_exists?(source, target = nil, **options) ⇒ Boolean

Returns:

  • (Boolean)

338
339
340
341
342
343
# File 'lib/gitlab/database/migration_helpers.rb', line 338

def foreign_key_exists?(source, target = nil, **options)
  foreign_keys(source).any? do |foreign_key|
    tables_match?(target.to_s, foreign_key.to_table.to_s) &&
      options_match?(foreign_key.options, options)
  end
end

#foreign_keys_for(table, column) ⇒ Object

Returns an Array containing the foreign keys for the given column.


982
983
984
985
986
# File 'lib/gitlab/database/migration_helpers.rb', line 982

def foreign_keys_for(table, column)
  column = column.to_s

  foreign_keys(table).select { |fk| fk.column == column }
end

#index_exists_by_name?(table, index) ⇒ Boolean

Fetches indexes on a column by name for postgres.

This will include indexes using an expression on the column, for example: `CREATE INDEX CONCURRENTLY index_name ON table (LOWER(column));`

We can remove this when upgrading to Rails 5 with an updated `index_exists?`:

Or this can be removed when we no longer support postgres < 9.5, so we can use `CREATE INDEX IF NOT EXISTS`.

Returns:

  • (Boolean)

1136
1137
1138
1139
1140
1141
1142
1143
1144
# File 'lib/gitlab/database/migration_helpers.rb', line 1136

def index_exists_by_name?(table, index)
  # We can't fall back to the normal `index_exists?` method because that
  # does not find indexes without passing a column name.
  if indexes(table).map(&:name).include?(index.to_s)
    true
  else
    postgres_exists_by_name?(table, index)
  end
end

#index_invalid?(index_name, schema: nil) ⇒ Boolean

Returns:

  • (Boolean)

173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/gitlab/database/migration_helpers.rb', line 173

def index_invalid?(index_name, schema: nil)
  index_name = connection.quote(index_name)
  schema = connection.quote(schema) if schema
  schema ||= 'current_schema()'

  connection.select_value(<<~SQL)
    select not i.indisvalid
    from pg_class c
    inner join pg_index i
      on c.oid = i.indexrelid
    inner join pg_namespace n
      on n.oid = c.relnamespace
    where n.nspname = #{schema}
      and c.relname = #{index_name}
  SQL
end

#indexes_for(table, column) ⇒ Object

Returns an Array containing the indexes for the given column


975
976
977
978
979
# File 'lib/gitlab/database/migration_helpers.rb', line 975

def indexes_for(table, column)
  column = column.to_s

  indexes(table).select { |index| index.columns.include?(column) }
end

#initialize_conversion_of_integer_to_bigint(table, columns, primary_key: :id) ⇒ Object

Initializes the conversion of a set of integer columns to bigint

It can be used for converting both a Primary Key and any Foreign Keys that may reference it or any other integer column that we may want to upgrade (e.g. columns that store IDs, but are not set as FKs).

  • For primary keys and Foreign Keys (or other columns) defined as NOT NULL,

    the new bigint column is added with a hardcoded NOT NULL DEFAULT 0
    which allows us to skip a very costly verification step once we
    are ready to switch it.
    

    This is crucial for Primary Key conversions, because setting a column

    as the PK converts even check constraints to NOT NULL constraints
    and forces an inline re-verification of the whole table.
    
  • It sets up a trigger to keep the two columns in sync.

    Note: this helper is intended to be used in a regular (pre-deployment) migration.

    This helper is part 1 of a multi-step migration process:

    1. initialize_conversion_of_integer_to_bigint to create the new columns and database trigger

    2. backfill_conversion_of_integer_to_bigint to copy historic data using background migrations

    3. remaining steps TBD, see #288005

table - The name of the database table containing the column columns - The name, or array of names, of the column(s) that we want to convert to bigint. primary_key - The name of the primary key column (most often :id)


825
826
827
# File 'lib/gitlab/database/migration_helpers.rb', line 825

def initialize_conversion_of_integer_to_bigint(table, columns, primary_key: :id)
  create_temporary_columns_and_triggers(table, columns, primary_key: primary_key, data_type: :bigint)
end

#install_rename_triggers(table, old, new, trigger_name: nil) ⇒ Object

Installs triggers in a table that keep a new column in sync with an old one.

table - The name of the table to install the trigger in. old_column - The name of the old column. new_column - The name of the new column. trigger_name - The name of the trigger to use (optional).


635
636
637
# File 'lib/gitlab/database/migration_helpers.rb', line 635

def install_rename_triggers(table, old, new, trigger_name: nil)
  Gitlab::Database::UnidirectionalCopyTrigger.on_table(table, connection: connection).create(old, new, trigger_name: trigger_name)
end

#postgres_exists_by_name?(table, name) ⇒ Boolean

Returns:

  • (Boolean)

1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
# File 'lib/gitlab/database/migration_helpers.rb', line 1146

def postgres_exists_by_name?(table, name)
  index_sql = <<~SQL
    SELECT COUNT(*)
    FROM pg_catalog.pg_indexes
    WHERE schemaname = #{connection.quote(current_schema)}
      AND tablename = #{connection.quote(table)}
      AND indexname = #{connection.quote(name)}
  SQL

  connection.select_value(index_sql).to_i > 0
end

#remove_check_constraint(table, constraint_name) ⇒ Object


1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
# File 'lib/gitlab/database/migration_helpers.rb', line 1291

def remove_check_constraint(table, constraint_name)
  # This is technically not necessary, but aligned with add_check_constraint
  # and allows us to continue use with_lock_retries here
  validate_not_in_transaction!(:remove_check_constraint)

  validate_check_constraint_name!(constraint_name)

  # DROP CONSTRAINT requires an EXCLUSIVE lock
  # Use with_lock_retries to make sure that this will not timeout
  with_lock_retries do
    execute <<-EOF.strip_heredoc
    ALTER TABLE #{table}
    DROP CONSTRAINT IF EXISTS #{constraint_name}
    EOF
  end
end

#remove_concurrent_index(table_name, column_name, options = {}) ⇒ Object

Removes an existed index, concurrently

Example:

remove_concurrent_index :users, :some_column

See Rails' `remove_index` for more info on the available arguments.


197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/gitlab/database/migration_helpers.rb', line 197

def remove_concurrent_index(table_name, column_name, options = {})
  if transaction_open?
    raise 'remove_concurrent_index can not be run inside a transaction, ' \
      'you can disable transactions by calling disable_ddl_transaction! ' \
      'in the body of your migration class'
  end

  options = options.merge({ algorithm: :concurrently })

  unless index_exists?(table_name, column_name, **options)
    Gitlab::AppLogger.warn "Index not removed because it does not exist (this may be due to an aborted migration or similar): table_name: #{table_name}, column_name: #{column_name}"
    return
  end

  disable_statement_timeout do
    remove_index(table_name, **options.merge({ column: column_name }))
  end

  # We removed this index. Now let's make sure it's not queued for async creation.
  unprepare_async_index(table_name, column_name, **options)
end

#remove_concurrent_index_by_name(table_name, index_name, options = {}) ⇒ Object

Removes an existing index, concurrently

Example:

remove_concurrent_index :users, "index_X_by_Y"

See Rails' `remove_index` for more info on the available arguments.


226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/gitlab/database/migration_helpers.rb', line 226

def remove_concurrent_index_by_name(table_name, index_name, options = {})
  if transaction_open?
    raise 'remove_concurrent_index_by_name can not be run inside a transaction, ' \
      'you can disable transactions by calling disable_ddl_transaction! ' \
      'in the body of your migration class'
  end

  index_name = index_name[:name] if index_name.is_a?(Hash)

  raise 'remove_concurrent_index_by_name must get an index name as the second argument' if index_name.blank?

  options = options.merge({ algorithm: :concurrently })

  unless index_exists_by_name?(table_name, index_name)
    Gitlab::AppLogger.warn "Index not removed because it does not exist (this may be due to an aborted migration or similar): table_name: #{table_name}, index_name: #{index_name}"
    return
  end

  disable_statement_timeout do
    remove_index(table_name, **options.merge({ name: index_name }))
  end

  # We removed this index. Now let's make sure it's not queued for async creation.
  unprepare_async_index_by_name(table_name, index_name, **options)
end

#remove_foreign_key_if_exists(source, target = nil, **kwargs) ⇒ Object


1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
# File 'lib/gitlab/database/migration_helpers.rb', line 1072

def remove_foreign_key_if_exists(source, target = nil, **kwargs)
  reverse_lock_order = kwargs.delete(:reverse_lock_order)
  return unless foreign_key_exists?(source, target, **kwargs)

  if target && reverse_lock_order && transaction_open?
    execute("LOCK TABLE #{target}, #{source} IN ACCESS EXCLUSIVE MODE")
  end

  if target
    remove_foreign_key(source, target, **kwargs)
  else
    remove_foreign_key(source, **kwargs)
  end
end

#remove_foreign_key_without_error(*args, **kwargs) ⇒ Object


1087
1088
1089
1090
# File 'lib/gitlab/database/migration_helpers.rb', line 1087

def remove_foreign_key_without_error(*args, **kwargs)
  remove_foreign_key(*args, **kwargs)
rescue ArgumentError
end

#remove_not_null_constraint(table, column, constraint_name: nil) ⇒ Object


1414
1415
1416
1417
1418
1419
# File 'lib/gitlab/database/migration_helpers.rb', line 1414

def remove_not_null_constraint(table, column, constraint_name: nil)
  remove_check_constraint(
    table,
    not_null_constraint_name(table, column, name: constraint_name)
  )
end

#remove_rename_triggers(table, trigger) ⇒ Object

Removes the triggers used for renaming a column concurrently.


640
641
642
# File 'lib/gitlab/database/migration_helpers.rb', line 640

def remove_rename_triggers(table, trigger)
  Gitlab::Database::UnidirectionalCopyTrigger.on_table(table, connection: connection).drop(trigger)
end

#remove_text_limit(table, column, constraint_name: nil) ⇒ Object


1380
1381
1382
# File 'lib/gitlab/database/migration_helpers.rb', line 1380

def remove_text_limit(table, column, constraint_name: nil)
  remove_check_constraint(table, text_limit_name(table, column, name: constraint_name))
end

#remove_timestamps(table_name, options = {}) ⇒ Object

To be used in the `#down` method of migrations that use `#add_timestamps_with_timezone`.

Available options are:

:columns - the column names to remove. Must be one
           Default value: `DEFAULT_TIMESTAMP_COLUMNS`

All options are optional.


70
71
72
73
74
75
# File 'lib/gitlab/database/migration_helpers.rb', line 70

def remove_timestamps(table_name, options = {})
  columns = options.fetch(:columns, DEFAULT_TIMESTAMP_COLUMNS)
  columns.each do |column_name|
    remove_column(table_name, column_name)
  end
end

#rename_column_concurrently(table, old, new, type: nil, type_cast_function: nil, batch_column_name: :id) ⇒ Object

Renames a column without requiring downtime.

Concurrent renames work by using database triggers to ensure both the old and new column are in sync. However, this method will not remove the triggers or the old column automatically; this needs to be done manually in a post-deployment migration. This can be done using the method `cleanup_concurrent_column_rename`.

table - The name of the database table containing the column. old - The old column name. new - The new column name. type - The type of the new column. If no type is given the old column's

type is used.

batch_column_name - option is for tables without primary key, in this

case another unique integer column can be used. Example: :user_id

594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
# File 'lib/gitlab/database/migration_helpers.rb', line 594

def rename_column_concurrently(table, old, new, type: nil, type_cast_function: nil, batch_column_name: :id)
  unless column_exists?(table, batch_column_name)
    raise "Column #{batch_column_name} does not exist on #{table}"
  end

  if transaction_open?
    raise 'rename_column_concurrently can not be run inside a transaction'
  end

  check_trigger_permissions!(table)

  create_column_from(table, old, new, type: type, batch_column_name: batch_column_name, type_cast_function: type_cast_function)

  install_rename_triggers(table, old, new)
end

#rename_constraint(table_name, old_name, new_name) ⇒ Object


1485
1486
1487
1488
1489
1490
# File 'lib/gitlab/database/migration_helpers.rb', line 1485

def rename_constraint(table_name, old_name, new_name)
  execute <<~SQL
    ALTER TABLE #{quote_table_name(table_name)}
    RENAME CONSTRAINT #{quote_column_name(old_name)} TO #{quote_column_name(new_name)}
  SQL
end

#rename_trigger_name(table, old, new) ⇒ Object

Returns the (base) name to use for triggers when renaming columns.


645
646
647
# File 'lib/gitlab/database/migration_helpers.rb', line 645

def rename_trigger_name(table, old, new)
  Gitlab::Database::UnidirectionalCopyTrigger.on_table(table, connection: connection).name(old, new)
end

#replace_sql(column, pattern, replacement) ⇒ Object

This will replace the first occurrence of a string in a column with the replacement using `regexp_replace`


1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
# File 'lib/gitlab/database/migration_helpers.rb', line 1061

def replace_sql(column, pattern, replacement)
  quoted_pattern = Arel::Nodes::Quoted.new(pattern.to_s)
  quoted_replacement = Arel::Nodes::Quoted.new(replacement.to_s)

  replace = Arel::Nodes::NamedFunction.new(
    "regexp_replace", [column, quoted_pattern, quoted_replacement]
  )

  Arel::Nodes::SqlLiteral.new(replace.to_sql)
end

#restore_conversion_of_integer_to_bigint(table, columns, primary_key: :id) ⇒ Object

Reverts `cleanup_conversion_of_integer_to_bigint`

table - The name of the database table containing the columns columns - The name, or array of names, of the column(s) that we have converted to bigint. primary_key - The name of the primary key column (most often :id)


850
851
852
# File 'lib/gitlab/database/migration_helpers.rb', line 850

def restore_conversion_of_integer_to_bigint(table, columns, primary_key: :id)
  create_temporary_columns_and_triggers(table, columns, primary_key: primary_key, data_type: :int)
end

#revert_backfill_conversion_of_integer_to_bigint(table, columns, primary_key: :id) ⇒ Object

Reverts `backfill_conversion_of_integer_to_bigint`

table - The name of the database table containing the column columns - The name, or an array of names, of the column(s) we want to convert to bigint. primary_key - The name of the primary key column (most often :id)


932
933
934
935
936
937
938
939
940
941
942
943
944
# File 'lib/gitlab/database/migration_helpers.rb', line 932

def revert_backfill_conversion_of_integer_to_bigint(table, columns, primary_key: :id)
  columns = Array.wrap(columns)

  conditions = ActiveRecord::Base.sanitize_sql([
    'job_class_name = :job_class_name AND table_name = :table_name AND column_name = :column_name AND job_arguments = :job_arguments',
    job_class_name: 'CopyColumnUsingBackgroundMigrationJob',
    table_name: table,
    column_name: primary_key,
    job_arguments: [columns, columns.map { |column| convert_to_bigint_column(column) }].to_json
  ])

  execute("DELETE FROM batched_background_migrations WHERE #{conditions}")
end

#revert_initialize_conversion_of_integer_to_bigint(table, columns) ⇒ Object Also known as: cleanup_conversion_of_integer_to_bigint

Reverts `initialize_conversion_of_integer_to_bigint`

table - The name of the database table containing the columns columns - The name, or array of names, of the column(s) that we're converting to bigint.


833
834
835
836
837
838
839
840
841
# File 'lib/gitlab/database/migration_helpers.rb', line 833

def revert_initialize_conversion_of_integer_to_bigint(table, columns)
  columns = Array.wrap(columns)
  temporary_columns = columns.map { |column| convert_to_bigint_column(column) }

  trigger_name = rename_trigger_name(table, columns, temporary_columns)
  remove_rename_triggers(table, trigger_name)

  temporary_columns.each { |column| remove_column(table, column) }
end

#sidekiq_queue_length(queue_name) ⇒ Object


1100
1101
1102
1103
1104
# File 'lib/gitlab/database/migration_helpers.rb', line 1100

def sidekiq_queue_length(queue_name)
  Sidekiq.redis do |conn|
    conn.llen("queue:#{queue_name}")
  end
end

#sidekiq_queue_migrate(queue_from, to:) ⇒ Object


1092
1093
1094
1095
1096
1097
1098
# File 'lib/gitlab/database/migration_helpers.rb', line 1092

def sidekiq_queue_migrate(queue_from, to:)
  while sidekiq_queue_length(queue_from) > 0
    Sidekiq.redis do |conn|
      conn.rpoplpush "queue:#{queue_from}", "queue:#{to}"
    end
  end
end

#true_valueObject


454
455
456
# File 'lib/gitlab/database/migration_helpers.rb', line 454

def true_value
  Database.true_value
end

#undo_change_column_type_concurrently(table, column) ⇒ Object

Reverses operations performed by change_column_type_concurrently.

table - The table containing the column. column - The name of the column to change.


664
665
666
667
668
# File 'lib/gitlab/database/migration_helpers.rb', line 664

def undo_change_column_type_concurrently(table, column)
  temp_column = "#{column}_for_type_change"

  undo_rename_column_concurrently(table, column, temp_column)
end

#undo_cleanup_concurrent_column_rename(table, old, new, type: nil, batch_column_name: :id) ⇒ Object

Reverses the operations performed by cleanup_concurrent_column_rename.

This method adds back the old_column removed by cleanup_concurrent_column_rename. It also adds back the (old_column > new_column) trigger that is removed by cleanup_concurrent_column_rename.

table - The name of the database table containing the column. old - The old column name. new - The new column name. type - The type of the old column. If no type is given the new column's

type is used.

batch_column_name - option is for tables without primary key, in this

case another unique integer column can be used. Example: :user_id

780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
# File 'lib/gitlab/database/migration_helpers.rb', line 780

def undo_cleanup_concurrent_column_rename(table, old, new, type: nil, batch_column_name: :id)
  unless column_exists?(table, batch_column_name)
    raise "Column #{batch_column_name} does not exist on #{table}"
  end

  if transaction_open?
    raise 'undo_cleanup_concurrent_column_rename can not be run inside a transaction'
  end

  check_trigger_permissions!(table)

  create_column_from(table, new, old, type: type, batch_column_name: batch_column_name)

  install_rename_triggers(table, old, new)
end

#undo_cleanup_concurrent_column_type_change(table, column, old_type, type_cast_function: nil, batch_column_name: :id, limit: nil) ⇒ Object

Reverses operations performed by cleanup_concurrent_column_type_change.

table - The table containing the column. column - The name of the column to change. old_type - The type of the original column used with change_column_type_concurrently. type_cast_function - Required if the conversion back to the original type is not automatic batch_column_name - option for tables without a primary key, in this case

another unique integer column can be used. Example: :user_id

694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
# File 'lib/gitlab/database/migration_helpers.rb', line 694

def undo_cleanup_concurrent_column_type_change(table, column, old_type, type_cast_function: nil, batch_column_name: :id, limit: nil)
  Gitlab::Database::QueryAnalyzers::RestrictAllowedSchemas.require_ddl_mode!

  temp_column = "#{column}_for_type_change"

  # Using a descriptive name that includes orinal column's name risks
  # taking us above the 63 character limit, so we use a hash
  identifier = "#{table}_#{column}_for_type_change"
  hashed_identifier = Digest::SHA256.hexdigest(identifier).first(10)
  temp_undo_cleanup_column = "tmp_undo_cleanup_column_#{hashed_identifier}"

  unless column_exists?(table, batch_column_name)
    raise "Column #{batch_column_name} does not exist on #{table}"
  end

  if transaction_open?
    raise 'undo_cleanup_concurrent_column_type_change can not be run inside a transaction'
  end

  check_trigger_permissions!(table)

  begin
    create_column_from(
      table,
      column,
      temp_undo_cleanup_column,
      type: old_type,
      batch_column_name: batch_column_name,
      type_cast_function: type_cast_function,
      limit: limit
    )

    transaction do
      # This has to be performed in a transaction as otherwise we might
      # have inconsistent data.
      rename_column(table, column, temp_column)
      rename_column(table, temp_undo_cleanup_column, column)

      install_rename_triggers(table, column, temp_column)
    end
  rescue StandardError
    # create_column_from can not run inside a transaction, which means
    #  that there is a risk that if any of the operations that follow it
    #  fail, we'll be left with an inconsistent schema
    # For those reasons, we make sure that we drop temp_undo_cleanup_column
    #  if an error is caught
    if column_exists?(table, temp_undo_cleanup_column)
      remove_column(table, temp_undo_cleanup_column)
    end

    raise
  end
end

#undo_rename_column_concurrently(table, old, new) ⇒ Object

Reverses operations performed by rename_column_concurrently.

This method takes care of removing previously installed triggers as well as removing the new column.

table - The name of the database table. old - The name of the old column. new - The name of the new column.


618
619
620
621
622
623
624
625
626
# File 'lib/gitlab/database/migration_helpers.rb', line 618

def undo_rename_column_concurrently(table, old, new)
  trigger_name = rename_trigger_name(table, old, new)

  check_trigger_permissions!(table)

  remove_rename_triggers(table, trigger_name)

  remove_column(table, new)
end

#update_column_in_batches(table, column, value, batch_size: nil, batch_column_name: :id) ⇒ Object

Updates the value of a column in batches.

This method updates the table in batches of 5% of the total row count. A `batch_size` option can also be passed to set this to a fixed number. This method will continue updating rows until no rows remain.

When given a block this method will yield two values to the block:

  1. An instance of `Arel::Table` for the table that is being updated.

  2. The query to run as an Arel object.

By supplying a block one can add extra conditions to the queries being executed. Note that the same block is used for all queries.

Example:

update_column_in_batches(:projects, :foo, 10) do |table, query|
  query.where(table[:some_column].eq('hello'))
end

This would result in this method updating only rows where `projects.some_column` equals “hello”.

table - The name of the table. column - The name of the column to update. value - The value for the column.

The `value` argument is typically a literal. To perform a computed update, an Arel literal can be used instead:

update_value = Arel.sql('bar * baz')

update_column_in_batches(:projects, :foo, update_value) do |table, query|
  query.where(table[:some_column].eq('hello'))
end

Rubocop's Metrics/AbcSize metric is disabled for this method as Rubocop determines this method to be too complex while there's no way to make it less “complex” without introducing extra methods (which actually will make things more complex).

`batch_column_name` option is for tables without primary key, in this case another unique integer column can be used. Example: :user_id

rubocop: disable Metrics/AbcSize


507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'lib/gitlab/database/migration_helpers.rb', line 507

def update_column_in_batches(table, column, value, batch_size: nil, batch_column_name: :id)
  if transaction_open?
    raise 'update_column_in_batches can not be run inside a transaction, ' \
      'you can disable transactions by calling disable_ddl_transaction! ' \
      'in the body of your migration class'
  end

  table = Arel::Table.new(table)

  count_arel = table.project(Arel.star.count.as('count'))
  count_arel = yield table, count_arel if block_given?

  total = exec_query(count_arel.to_sql).to_a.first['count'].to_i

  return if total == 0

  if batch_size.nil?
    # Update in batches of 5% until we run out of any rows to update.
    batch_size = ((total / 100.0) * 5.0).ceil
    max_size = 1000

    # The upper limit is 1000 to ensure we don't lock too many rows. For
    # example, for "merge_requests" even 1% of the table is around 35 000
    # rows for GitLab.com.
    batch_size = max_size if batch_size > max_size
  end

  start_arel = table.project(table[batch_column_name]).order(table[batch_column_name].asc).take(1)
  start_arel = yield table, start_arel if block_given?
  start_id = exec_query(start_arel.to_sql).to_a.first[batch_column_name.to_s].to_i

  loop do
    stop_arel = table.project(table[batch_column_name])
      .where(table[batch_column_name].gteq(start_id))
      .order(table[batch_column_name].asc)
      .take(1)
      .skip(batch_size)

    stop_arel = yield table, stop_arel if block_given?
    stop_row = exec_query(stop_arel.to_sql).to_a.first

    update_arel = Arel::UpdateManager.new
      .table(table)
      .set([[table[column], value]])
      .where(table[batch_column_name].gteq(start_id))

    if stop_row
      stop_id = stop_row[batch_column_name.to_s].to_i
      start_id = stop_id
      update_arel = update_arel.where(table[batch_column_name].lt(stop_id))
    end

    update_arel = yield table, update_arel if block_given?

    execute(update_arel.to_sql)

    # There are no more rows left to update.
    break unless stop_row
  end
end

#validate_check_constraint(table, constraint_name) ⇒ Object


1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
# File 'lib/gitlab/database/migration_helpers.rb', line 1277

def validate_check_constraint(table, constraint_name)
  validate_check_constraint_name!(constraint_name)

  unless check_constraint_exists?(table, constraint_name)
    raise missing_schema_object_message(table, "check constraint", constraint_name)
  end

  disable_statement_timeout do
    # VALIDATE CONSTRAINT only requires a SHARE UPDATE EXCLUSIVE LOCK
    # It only conflicts with other validations and creating indexes
    execute("ALTER TABLE #{table} VALIDATE CONSTRAINT #{constraint_name};")
  end
end

#validate_foreign_key(source, column, name: nil) ⇒ Object


326
327
328
329
330
331
332
333
334
335
336
# File 'lib/gitlab/database/migration_helpers.rb', line 326

def validate_foreign_key(source, column, name: nil)
  fk_name = name || concurrent_foreign_key_name(source, column)

  unless foreign_key_exists?(source, name: fk_name)
    raise missing_schema_object_message(source, "foreign key", fk_name)
  end

  disable_statement_timeout do
    execute("ALTER TABLE #{source} VALIDATE CONSTRAINT #{fk_name};")
  end
end

#validate_not_null_constraint(table, column, constraint_name: nil) ⇒ Object


1407
1408
1409
1410
1411
1412
# File 'lib/gitlab/database/migration_helpers.rb', line 1407

def validate_not_null_constraint(table, column, constraint_name: nil)
  validate_check_constraint(
    table,
    not_null_constraint_name(table, column, name: constraint_name)
  )
end

#validate_text_limit(table, column, constraint_name: nil) ⇒ Object


1376
1377
1378
# File 'lib/gitlab/database/migration_helpers.rb', line 1376

def validate_text_limit(table, column, constraint_name: nil)
  validate_check_constraint(table, text_limit_name(table, column, name: constraint_name))
end

#with_lock_retries(*args, **kwargs, &block) ⇒ Object

Executes the block with a retry mechanism that alters the lock_timeout and sleep_time between attempts. The timings can be controlled via the timing_configuration parameter. If the lock was not acquired within the retry period, a last attempt is made without using lock_timeout.

Note this helper uses subtransactions when run inside an already open transaction.

Examples

# Invoking without parameters
with_lock_retries do
  drop_table :my_table
end

# Invoking with custom +timing_configuration+
t = [
  [1.second, 1.second],
  [2.seconds, 2.seconds]
]

with_lock_retries(timing_configuration: t) do
  drop_table :my_table # this will be retried twice
end

# Disabling the retries using an environment variable
> export DISABLE_LOCK_RETRIES=true

with_lock_retries do
  drop_table :my_table # one invocation, it will not retry at all
end

Parameters

  • timing_configuration - [[ActiveSupport::Duration, ActiveSupport::Duration], …] lock timeout for the block, sleep time before the next iteration, defaults to `Gitlab::Database::WithLockRetries::DEFAULT_TIMING_CONFIGURATION`

  • logger - [Gitlab::JsonLogger]

  • env - [Hash] custom environment hash, see the example with `DISABLE_LOCK_RETRIES`


441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/gitlab/database/migration_helpers.rb', line 441

def with_lock_retries(*args, **kwargs, &block)
  raise_on_exhaustion = !!kwargs.delete(:raise_on_exhaustion)
  merged_args = {
    connection: connection,
    klass: self.class,
    logger: Gitlab::BackgroundMigration::Logger,
    allow_savepoints: true
  }.merge(kwargs)

  Gitlab::Database::WithLockRetries.new(**merged_args)
    .run(raise_on_exhaustion: raise_on_exhaustion, &block)
end