Skip to content

Add NOT NULL constraint to merge_request_context_commits

What does this MR do and why?

In order to shard merge_request_context_commits on project_id, we need to retrieve project_id from its related merge request. By not having a NOT NULL constraint on this column, we're unable to use desired_sharding_key to automatically backfill this table.

Database

Up

main: == [advisory_lock_connection] object_id: 127340, pg_backend_pid: 70188
main: == [advisory_lock_connection] object_id: 128600, pg_backend_pid: 70210
main: == 20240612132227 AddNotNullToMergeRequestContextCommitsOnMergeRequestId: migrating
main: -- current_schema(nil)
main:    -> 0.0003s
main: -- transaction_open?(nil)
main:    -> 0.0000s
main: -- transaction_open?(nil)
main:    -> 0.0000s
main: -- execute("ALTER TABLE merge_request_context_commits\nADD CONSTRAINT check_1dc6b5f2ac\nCHECK ( merge_request_id IS NOT NULL )\nNOT VALID;\n")
main:    -> 0.0008s
main: -- execute("SET statement_timeout TO 0")
main:    -> 0.0002s
main: -- execute("ALTER TABLE merge_request_context_commits VALIDATE CONSTRAINT check_1dc6b5f2ac;")
main:    -> 0.0003s
main: -- execute("RESET statement_timeout")
main:    -> 0.0003s
main: == 20240612132227 AddNotNullToMergeRequestContextCommitsOnMergeRequestId: migrated (0.0189s)

Down

main: == [advisory_lock_connection] object_id: 127340, pg_backend_pid: 70188
main: == 20240612132227 AddNotNullToMergeRequestContextCommitsOnMergeRequestId: reverting
main: -- transaction_open?(nil)
main:    -> 0.0000s
main: -- transaction_open?(nil)
main:    -> 0.0000s
main: -- execute("            ALTER TABLE merge_request_context_commits\n            DROP CONSTRAINT IF EXISTS check_1dc6b5f2ac\n")
main:    -> 0.0008s
main: == 20240612132227 AddNotNullToMergeRequestContextCommitsOnMergeRequestId: reverted (0.0129s)

Related to #444232 (closed) and !146557 (diffs)

Merge request reports