Gitlab Upgrade Fails PG::CheckViolation
While upgrading Gitlab one evening after updating the gpg keys, I ran into the following error during the reconfigure process.
PG::CheckViolation: ERROR: check constraint "check_xxxxxxxxx" of relation "gpg_key_subkeys" is violated by some row
The error itself was pretty straightforward, although I had a good idea of what to do to resolve it, I wanted to do some research to confirm my assumptions before proceeding with the fix and possibly making the situation worse.
Verification
There should only be one entry in the gpg_key_subkeys table. Fire up the psql console.
sudo gitlab-psql
Now run the following SQL query to count the number of entries in the gpg_key_subkeys table.
SELECT COUNT(*) FROM gpg_key_subkeys;
There should only be one entry in the gpg_key_subkeys table. If there are more than one, then you will need to backfill user_id on gpg_key_subkeys from the parent gpg_keys table.
Fix
To backfill the user_id on the gpg_key_subkeys table, you can use the following SQL query. This query will update the user_id in the gpg_key_subkeys table based on the corresponding user_id from the gpg_keys table.
UPDATE gpg_key_subkeys s
SET user_id = k.user_id
FROM gpg_keys k
WHERE s.gpg_key_id = k.id
AND s.user_id IS NULL
AND k.user_id IS NOT NULL;
Cause
The cause of this issue stems from a bug when upgrading to 18.9.0. However, I have run into this problem in the past and unfortunately I don’t remember what that issue was. It may have been the same bug or it could have been a wonky upgrade.