Fix change_column_type_concurrently and undo_cleanup_concurrent_column_type_change
If change_column_type_concurrently
is used in a migration, it raises an error ArgumentError: unknown keyword: :type_cast_function
complaining that method rename_column_concurrently
in v2 does not accept keyword argument :type_cast_function
:
Click to expand the error backtrace
StandardError: An error has occurred, all later migrations canceled:
unknown keyword: :type_cast_function
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migration_helpers/v2.rb:118:in `rename_column_concurrently'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migration_helpers.rb:552:in `change_column_type_concurrently'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/db/migrate/20230817040352_convert_shared_runners_duration_to_bigint.rb:12:in `block in up'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/db/migrate/20230817040352_convert_shared_runners_duration_to_bigint.rb:11:in `each'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/db/migrate/20230817040352_convert_shared_runners_duration_to_bigint.rb:11:in `up'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migration_helpers/restrict_gitlab_schema.rb:33:in `block in exec_migration'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/query_analyzer.rb:37:in `within'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migration_helpers/restrict_gitlab_schema.rb:30:in `exec_migration'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migration_helpers/automatic_lock_writes_on_tables.rb:21:in `exec_migration'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migrations/lock_retry_mixin.rb:36:in `ddl_transaction'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migrations/runner_backoff/active_record_mixin.rb:21:in `execute_migration_in_transaction'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migrations/pg_backend_pid.rb:15:in `block in with_advisory_lock_connection'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migrations/pg_backend_pid.rb:12:in `with_advisory_lock_connection'
Caused by:
ArgumentError: unknown keyword: :type_cast_function
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migration_helpers/v2.rb:118:in `rename_column_concurrently'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migration_helpers.rb:552:in `change_column_type_concurrently'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/db/migrate/20230817040352_convert_shared_runners_duration_to_bigint.rb:12:in `block in up'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/db/migrate/20230817040352_convert_shared_runners_duration_to_bigint.rb:11:in `each'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/db/migrate/20230817040352_convert_shared_runners_duration_to_bigint.rb:11:in `up'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migration_helpers/restrict_gitlab_schema.rb:33:in `block in exec_migration'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/query_analyzer.rb:37:in `within'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migration_helpers/restrict_gitlab_schema.rb:30:in `exec_migration'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migration_helpers/automatic_lock_writes_on_tables.rb:21:in `exec_migration'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migrations/lock_retry_mixin.rb:36:in `ddl_transaction'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migrations/runner_backoff/active_record_mixin.rb:21:in `execute_migration_in_transaction'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migrations/pg_backend_pid.rb:15:in `block in with_advisory_lock_connection'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migrations/pg_backend_pid.rb:12:in `with_advisory_lock_connection'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
scripts/regenerate-schema:180:in `run': Command failed: rails aborted! (RuntimeError)
StandardError: An error has occurred, all later migrations canceled:
unknown keyword: :type_cast_function
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migration_helpers/v2.rb:118:in `rename_column_concurrently'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migration_helpers.rb:552:in `change_column_type_concurrently'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/db/migrate/20230817040352_convert_shared_runners_duration_to_bigint.rb:12:in `block in up'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/db/migrate/20230817040352_convert_shared_runners_duration_to_bigint.rb:11:in `each'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/db/migrate/20230817040352_convert_shared_runners_duration_to_bigint.rb:11:in `up'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migration_helpers/restrict_gitlab_schema.rb:33:in `block in exec_migration'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/query_analyzer.rb:37:in `within'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migration_helpers/restrict_gitlab_schema.rb:30:in `exec_migration'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migration_helpers/automatic_lock_writes_on_tables.rb:21:in `exec_migration'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migrations/lock_retry_mixin.rb:36:in `ddl_transaction'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migrations/runner_backoff/active_record_mixin.rb:21:in `execute_migration_in_transaction'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migrations/pg_backend_pid.rb:15:in `block in with_advisory_lock_connection'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migrations/pg_backend_pid.rb:12:in `with_advisory_lock_connection'
Caused by:
ArgumentError: unknown keyword: :type_cast_function
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migration_helpers/v2.rb:118:in `rename_column_concurrently'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migration_helpers.rb:552:in `change_column_type_concurrently'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/db/migrate/20230817040352_convert_shared_runners_duration_to_bigint.rb:12:in `block in up'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/db/migrate/20230817040352_convert_shared_runners_duration_to_bigint.rb:11:in `each'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/db/migrate/20230817040352_convert_shared_runners_duration_to_bigint.rb:11:in `up'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migration_helpers/restrict_gitlab_schema.rb:33:in `block in exec_migration'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/query_analyzer.rb:37:in `within'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migration_helpers/restrict_gitlab_schema.rb:30:in `exec_migration'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migration_helpers/automatic_lock_writes_on_tables.rb:21:in `exec_migration'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migrations/lock_retry_mixin.rb:36:in `ddl_transaction'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migrations/runner_backoff/active_record_mixin.rb:21:in `execute_migration_in_transaction'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migrations/pg_backend_pid.rb:15:in `block in with_advisory_lock_connection'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migrations/pg_backend_pid.rb:12:in `with_advisory_lock_connection'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
from scripts/regenerate-schema:167:in `migrate'
from scripts/regenerate-schema:39:in `block in execute'
from scripts/regenerate-schema:31:in `chdir'
from scripts/regenerate-schema:31:in `execute'
from scripts/regenerate-schema:227:in `<main>'
The migration helper method undo_cleanup_concurrent_column_type_change
is also impacted
Also, if I use the rename_column_concurrently
that change_column_type_concurrently
calls to create a new column for type change from integer
to bigint
similar to the following example, it will lead to a syntax error:
rename_column_concurrently(:ci_project_monthly_usages, :shared_runners_duration, :shared_runners_duration_for_type_change, type: :bigint)
Caused by:
PG::SyntaxError: ERROR: syntax error at or near "("
LINE 1: ...D "shared_runners_duration_for_type_change" bigint(4) /*appl...
^
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migration_helpers.rb:1285:in `create_column_from'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migration_helpers/v2.rb:223:in `setup_renamed_column'
/Users/tian/workspace/gitlab/gitlab-development-kit/gitlab/lib/gitlab/database/migration_helpers/v2.rb:121:in `rename_column_concurrently'
And this error is caused by the copy of old column's precision and scale at https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/database/migration_helpers.rb#L1285-1288
Edited by Tianwen Chen