Skip to content

fix(migrations): run the 2.1.0 MySQL cross-table statements on any database collation - #37141

Closed
manerow wants to merge 1 commit into
mainfrom
fix/2-1-0-mysql-collation-safe-compares
Closed

manerow wants to merge 1 commit into
mainfrom
fix/2-1-0-mysql-collation-safe-compares

Conversation

@manerow

@manerow manerow commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Issue

No issue. Found while finding why the nightly "Java UI ITs (embedded)" jobs on main and 2.1 have lost their runner every night since 10-20.

Problem

On a MySQL database whose default collation is not utf8mb4_0900_ai_ci (for example utf8mb4_unicode_ci), the 2.1.0 migration fails with Illegal mix of collations ... for operation '='.

Six statements compare ids across two kinds of tables:

  • tables the migrations create with an explicit COLLATE=utf8mb4_0900_ai_ci (the ontology tables, test_case_resolution_status_time_series)
  • older tables that kept the database default (entity_relationship, entity_extension)

MySQL refuses to compare two columns with different collations, even when the tables are empty. So every upgrade on such a database hits it, not only ones with rows to clean.

  • A plain migrate stops at the first of these statements.
  • migrate --force skips it, but a skipped statement is never recorded, so the server then refuses to start: "There are pending migrations to be run on the database: [2.1.0]".

The UI integration tests run on a MySQL with utf8mb4_unicode_ci. Since these statements landed, their server never starts.

What this PR changes

Each of those statements becomes a guarded block (SET / PREPARE / EXECUTE / DEALLOCATE). The block reads, at run time, the collation of the older table's column from information_schema.columns, and applies it to the other side with COLLATE. So:

  • the comparison works on any database collation
  • the older table's index is still used, because the comparison runs in that column's own collation (pinning both sides to one collation would also work, but would turn these lookups on entity_relationship and entity_extension into full scans)

The statements:

They delete and insert the same rows as before, and they stay idempotent. A database that already ran the old text runs the new blocks once more, with no effect.

How it was tested

  • Fresh MySQL 8.3 with utf8mb4_unicode_ci: on main, migrate fails on these statements and the server never starts. With this PR, migrate (no --force) exits 0, every statement is recorded, and the server starts.
  • Fresh MySQL 8.3 with the default utf8mb4_0900_ai_ci: unchanged; migrate exits 0 and the server starts.
  • Seeded data: a draft axiom (no ontology) and a native axiom, each with relationship and version rows. The new blocks delete only the draft's rows.
  • The nightly's embedded UI IT step (OpenSearch) against a server image built from this branch: 10 tests, all pass (1 skipped, as before). On main the server cannot start.
  • Migration unit tests: 141 pass, including MySqlGuardedBlockNamesTest and MySqlMigrationUnitsTest.

The harness side of the nightly failure (leaked containers when the server cannot start) is fixed separately.

@manerow manerow added safe to test Add this label to run secure Github workflows on PRs backend labels Nov 5, 2026
@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

❌ PR checklist incomplete

This PR cannot be merged until the following are addressed on its linked issue:

  • No GitHub issue is linked. Link an issue in the Development section of the PR (or add Fixes #12345 to the description). For a same-org cross-repo issue, add Fixes open-metadata/<repo>#123 to the description.

The fields live on the linked issue in the Shipping project (open the issue → right sidebar → Projects). After you set them, re-run this check (or push a commit) — issue/project changes do not re-trigger it automatically.

Maintainers can bypass this check by adding the skip-pr-checks label.

Comment on lines +1098 to +1112
SET @ontology_axiom_relationship_cleanup_sql = CONCAT(
'DELETE FROM entity_relationship WHERE (fromEntity = ''ontologyAxiom'' AND fromId IN (SELECT id COLLATE ',
(SELECT COLLATION_NAME FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = 'entity_relationship' AND column_name = 'fromId'),
' FROM ontology_axiom_entity WHERE json_extract(json, ''$.ontology.id'') IS NULL)) OR (toEntity = ''ontologyAxiom'' AND toId IN (SELECT id COLLATE ',
(SELECT COLLATION_NAME FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = 'entity_relationship' AND column_name = 'toId'),
' FROM ontology_axiom_entity WHERE json_extract(json, ''$.ontology.id'') IS NULL))'
);
PREPARE ontology_axiom_relationship_cleanup_stmt FROM @ontology_axiom_relationship_cleanup_sql;
EXECUTE ontology_axiom_relationship_cleanup_stmt;
DEALLOCATE PREPARE ontology_axiom_relationship_cleanup_stmt;
SET @ontology_axiom_extension_cleanup_sql = CONCAT(
'DELETE FROM entity_extension WHERE id IN (SELECT id COLLATE ',
(SELECT COLLATION_NAME FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = 'entity_extension' AND column_name = 'id'),
' FROM ontology_axiom_entity WHERE json_extract(json, ''$.ontology.id'') IS NULL)'
);

@gitar-bot gitar-bot Bot Nov 5, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Bug: COLLATE fails when the database charset isn't utf8mb4

Each guarded block puts COLLATE <entity_relationship/entity_extension column collation> on a utf8mb4 column (ontology_axiom_entity.id, ontology_change_set_entity.id, test_case_resolution_status_time_series.id, all pinned to utf8mb4_0900_ai_ci). The lookup only reads COLLATION_NAME. entity_relationship and entity_extension were created in flyway v001 with no charset, so they use the database default. On an older install whose default charset is utf8mb3 or latin1, the generated SQL becomes id COLLATE utf8mb3_general_ci or id COLLATE latin1_swedish_ci. MySQL rejects that with ER 1253 (COLLATION '...' is not valid for CHARACTER SET 'utf8mb4'), so the 2.1.0 migration and the server startup still fail. The original text didn't fail there, because MySQL converts the non-utf8mb4 side to utf8mb4 for these implicit compares. So the change can break databases that used to migrate cleanly, which contradicts the 'any database collation' goal.

Fix: also read CHARACTER_SET_NAME and build CONVERT(id USING <charset>) COLLATE <collation> (UUIDs are ASCII, so the conversion loses nothing). That keeps the index on the entity_relationship/entity_extension side and works for every charset.

Convert to the target column's charset before applying its collation (same pattern for every block):

SET @ontology_axiom_extension_cleanup_sql = (
  SELECT CONCAT(
    'DELETE FROM entity_extension WHERE id IN (SELECT CONVERT(id USING ', CHARACTER_SET_NAME, ') COLLATE ', COLLATION_NAME,
    ' FROM ontology_axiom_entity WHERE json_extract(json, ''$.ontology.id'') IS NULL)')
  FROM information_schema.columns
  WHERE table_schema = DATABASE() AND table_name = 'entity_extension' AND column_name = 'id'
);

Was this helpful? React with 👍 / 👎

@gitar-bot

gitar-bot Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Code Review ⚠️ Changes requested 0 closed / 1 findings

🔴 High risk · Dynamic SQL changes 2.1.0 cleanup deletes and incident backfill to run across collations; comparison semantics could remove or associate unintended records.

Guards the six 2.1.0 MySQL cross-table statements with run-time collation lookups so they work on any database collation, with idempotent behavior preserved. Changes requested: the generated COLLATE clause fails with ER 1253 when the database charset is not utf8mb4 (such as utf8mb3 or latin1), which would break upgrades that previously migrated cleanly. The fix is to also read CHARACTER_SET_NAME and use CONVERT(id USING <charset>) COLLATE <collation> in the affected blocks in schemaChanges.sql and postDataMigrationSQLScript.sql.

⚠️ Bug: COLLATE fails when the database charset isn't utf8mb4

📄 bootstrap/sql/migrations/native/2.1.0/mysql/schemaChanges.sql:1098-1112 📄 bootstrap/sql/migrations/native/2.1.0/mysql/schemaChanges.sql:1203-1217 📄 bootstrap/sql/migrations/native/2.1.0/mysql/schemaChanges.sql:1232-1241 📄 bootstrap/sql/migrations/native/2.1.0/mysql/postDataMigrationSQLScript.sql:28-30

Each guarded block puts COLLATE <entity_relationship/entity_extension column collation> on a utf8mb4 column (ontology_axiom_entity.id, ontology_change_set_entity.id, test_case_resolution_status_time_series.id, all pinned to utf8mb4_0900_ai_ci). The lookup only reads COLLATION_NAME. entity_relationship and entity_extension were created in flyway v001 with no charset, so they use the database default. On an older install whose default charset is utf8mb3 or latin1, the generated SQL becomes id COLLATE utf8mb3_general_ci or id COLLATE latin1_swedish_ci. MySQL rejects that with ER 1253 (COLLATION '...' is not valid for CHARACTER SET 'utf8mb4'), so the 2.1.0 migration and the server startup still fail. The original text didn't fail there, because MySQL converts the non-utf8mb4 side to utf8mb4 for these implicit compares. So the change can break databases that used to migrate cleanly, which contradicts the 'any database collation' goal.

Fix: also read CHARACTER_SET_NAME and build CONVERT(id USING <charset>) COLLATE <collation> (UUIDs are ASCII, so the conversion loses nothing). That keeps the index on the entity_relationship/entity_extension side and works for every charset.

Convert to the target column's charset before applying its collation (same pattern for every block)
SET @ontology_axiom_extension_cleanup_sql = (
  SELECT CONCAT(
    'DELETE FROM entity_extension WHERE id IN (SELECT CONVERT(id USING ', CHARACTER_SET_NAME, ') COLLATE ', COLLATION_NAME,
    ' FROM ontology_axiom_entity WHERE json_extract(json, ''$.ontology.id'') IS NULL)')
  FROM information_schema.columns
  WHERE table_schema = DATABASE() AND table_name = 'entity_extension' AND column_name = 'id'
);
🤖 Prompt for agents
Code Review: Guards the six 2.1.0 MySQL cross-table statements with run-time collation lookups so they work on any database collation, with idempotent behavior preserved. Changes requested: the generated `COLLATE` clause fails with ER 1253 when the database charset is not utf8mb4 (such as utf8mb3 or latin1), which would break upgrades that previously migrated cleanly. The fix is to also read `CHARACTER_SET_NAME` and use `CONVERT(id USING <charset>) COLLATE <collation>` in the affected blocks in `schemaChanges.sql` and `postDataMigrationSQLScript.sql`.

1. ⚠️ Bug: COLLATE fails when the database charset isn't utf8mb4
   Files: bootstrap/sql/migrations/native/2.1.0/mysql/schemaChanges.sql:1098-1112, bootstrap/sql/migrations/native/2.1.0/mysql/schemaChanges.sql:1203-1217, bootstrap/sql/migrations/native/2.1.0/mysql/schemaChanges.sql:1232-1241, bootstrap/sql/migrations/native/2.1.0/mysql/postDataMigrationSQLScript.sql:28-30

   Each guarded block puts `COLLATE <entity_relationship/entity_extension column collation>` on a `utf8mb4` column (`ontology_axiom_entity.id`, `ontology_change_set_entity.id`, `test_case_resolution_status_time_series.id`, all pinned to `utf8mb4_0900_ai_ci`). The lookup only reads `COLLATION_NAME`. `entity_relationship` and `entity_extension` were created in flyway v001 with no charset, so they use the database default. On an older install whose default charset is `utf8mb3` or `latin1`, the generated SQL becomes `id COLLATE utf8mb3_general_ci` or `id COLLATE latin1_swedish_ci`. MySQL rejects that with ER 1253 (`COLLATION '...' is not valid for CHARACTER SET 'utf8mb4'`), so the 2.1.0 migration and the server startup still fail. The original text didn't fail there, because MySQL converts the non-utf8mb4 side to utf8mb4 for these implicit compares. So the change can break databases that used to migrate cleanly, which contradicts the 'any database collation' goal.
   
   Fix: also read `CHARACTER_SET_NAME` and build `CONVERT(id USING <charset>) COLLATE <collation>` (UUIDs are ASCII, so the conversion loses nothing). That keeps the index on the `entity_relationship`/`entity_extension` side and works for every charset.

   Fix (Convert to the target column's charset before applying its collation (same pattern for every block)):
   SET @ontology_axiom_extension_cleanup_sql = (
     SELECT CONCAT(
       'DELETE FROM entity_extension WHERE id IN (SELECT CONVERT(id USING ', CHARACTER_SET_NAME, ') COLLATE ', COLLATION_NAME,
       ' FROM ontology_axiom_entity WHERE json_extract(json, ''$.ontology.id'') IS NULL)')
     FROM information_schema.columns
     WHERE table_schema = DATABASE() AND table_name = 'entity_extension' AND column_name = 'id'
   );

Verification

Rules No rules evaluated

Functional validation Not enabled

Set up

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Compact
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Powered by Gitar — free for open source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend safe to test Add this label to run secure Github workflows on PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant