Fixing Missing Postgres Constraints

I have Omnibus GitLab 14.1.2 CE running on an Ubuntu server. The data was recently migrated from a source-installed instance that existed for about nine years and which had been upgraded/migrated through various GitLab versions during that time. The primary reason for switching to Omnibus was to make upgrades much smoother and use the GitLab-managed postgres database.

However, during the last couple of Ominbus upgrades, I have encountered several different PG::UndefinedObject: ERROR: constraint "example_pkey" of relation "examples" does not exist errors. Here are the two most recent errors:
PG::UndefinedObject: ERROR: constraint “taggings_pkey” of relation “taggings” does not exist
PG::UndefinedObject: ERROR: constraint “ci_builds_pkey” of relation “ci_builds” does not exist

I have encountered similar errors before and posted questions on Reddit and GitLab issues but not gotten much traction. After finding this forum today, I did some searching and found folks with similar problems but there’s been no explanation of the root cause of these errors or the recommended way of fixing them.

I’d appreciate any answers to the following:

  • What is the root cause of these constraint errors?
  • What is the recommended way of fixing these errors?
  • Is there a way to compare the database (tables, constraints, etc.) against an expected schema and fix the differences to make sure I don’t encounter these errors during future upgrades?
  • Some things I have read imply that this is the result of incomplete migration tasks. Is there a way to replay those migrations to make sure the database is up-to-date?

Two years later and I’m still dealing with upgrade issues related to database schema inconsistencies. There was talk of GitLab writing a migration to fix these issues, but that hasn’t happened.

Has anyone found a way to detect/fix database schema inconsistencies?