Skip to content

IBX-13142: Renumbered the languages 2, 3, 4… in the order of their ids - #965

Open
Steveb-p wants to merge 9 commits into
feature/language-foreign-keys-6.0from
feature/language-renumber-6.0
Open

Steveb-p wants to merge 9 commits into
feature/language-foreign-keys-6.0from
feature/language-renumber-6.0

Conversation

@Steveb-p

@Steveb-p Steveb-p commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor
🎫 Issue IBX-13142

Warning

Stacked on #964. Has to land together with ibexa/product-catalog#1687, ibexa/share#235 and ibexa/translations-management#450, which give the remaining language id columns foreign keys. Without them the migration refuses to run.

Related PRs:

Description:

With the bitmask every language got a power of two, up to 2^62, and since #946 a new language gets the id after the highest one. So an upgraded site with many languages keeps ids up to 2^62 and creates new ones past them, way over 2^53, the largest integer JavaScript represents exactly.

RenumberLanguageIdsMigration gives the languages the ids a fresh install would, 2, 3, 4… in the order of their old ids, and does nothing when they're already like that (a fresh install, or a rerun). It renumbers ibexa_content_language one language at a time in ascending order, so a new id is free by the time it's used, and the ON UPDATE CASCADE foreign keys from #964 carry the new ids to every table referencing it, the packages' tables included, rewriting each row once. Languages keeping their id (at least the first one, usually the main language) aren't touched. It refuses to run, listing the columns, while a column referencing the language table, or a language_id/*_language_id column without a foreign key to another table, has no foreign key cascading updates. It's dated a day after #964's foreign keys, leaving packages room to add theirs before it.

On MySQL and MariaDB I hit an InnoDB limitation: a cascade into a primary key column fails while another column of the same row still references the language being renumbered. That's exactly ibexa_object_state_group_language (real_language_id is in its primary key, language_id holds the same language), so there its rows are set aside in a temporary table and inserted back with the new ids. The migration doesn't change the schema, so it overrides isTransactional() to run in one transaction on MySQL too, and a failure leaves nothing half renumbered.

On SQLite it relies on the cascades as well. SQLite ignores PRAGMA foreign_keys inside a transaction, so there the migration runs without one, and switches foreign keys on for the renumbering when the connection doesn't enforce them, back off afterwards. So with #964 switching them off for its table rebuilds, the upgrade goes through in one run whatever the connection's setting.

For QA:

vendor/bin/phpunit -c phpunit.xml tests/bundle/RepositoryInstaller/Migration/LanguageBitmaskUpgradeSequenceTest.php
DATABASE="mysql://user:pass@127.0.0.1/testdb" vendor/bin/phpunit -c phpunit-integration-legacy.xml tests/integration/RepositoryInstaller/Migration/RenumberLanguageIdsMigrationTest.php

By hand: upgrade a 5.0 database with a few languages, then check that the languages are numbered 2, 3, 4… in their old order, content and content types keep their translations, and the next language created gets the next id.

Documentation:

For the 6.0 upgrade guide: language ids change on upgrade (2, 3, 4… in their old order). Anything outside the database keeping a language id, like a REST client or a custom integration, has to look it up again, and the persistence cache has to be cleared after migrating. Project tables holding language ids need an ON UPDATE CASCADE foreign key to ibexa_content_language. Without it, RenumberLanguageIdsMigration aborts, listing the columns, and the migrations resume from it once the keys exist. Project migrations sort after Ibexa's, so a project migration adding the keys has to be run on its own first, with doctrine:migrations:execute --up.

@Steveb-p
Steveb-p force-pushed the feature/language-renumber-6.0 branch from 8aafcd3 to e1b65d9 Compare November 17, 2026 11:19
@Steveb-p
Steveb-p force-pushed the feature/language-foreign-keys-6.0 branch from 28485ee to 565498f Compare November 17, 2026 11:19
@Steveb-p
Steveb-p force-pushed the feature/language-renumber-6.0 branch from e1b65d9 to 1322b10 Compare November 17, 2026 11:50
@Steveb-p
Steveb-p force-pushed the feature/language-foreign-keys-6.0 branch from 565498f to 1bef7c1 Compare November 17, 2026 11:50
@Steveb-p
Steveb-p force-pushed the feature/language-renumber-6.0 branch from 1322b10 to 7c5dbff Compare November 17, 2026 12:28
@Steveb-p
Steveb-p marked this pull request as ready for review November 17, 2026 16:23
@Steveb-p
Steveb-p force-pushed the feature/language-foreign-keys-6.0 branch from 32391a2 to 31972d9 Compare November 18, 2026 11:50
@Steveb-p
Steveb-p force-pushed the feature/language-renumber-6.0 branch from 09eff57 to 741c80b Compare November 18, 2026 11:50
@Steveb-p
Steveb-p force-pushed the feature/language-foreign-keys-6.0 branch from 31972d9 to b082d9e Compare November 18, 2026 12:13
@Steveb-p
Steveb-p force-pushed the feature/language-renumber-6.0 branch from 741c80b to 4be3607 Compare November 18, 2026 12:13
@Steveb-p
Steveb-p force-pushed the feature/language-foreign-keys-6.0 branch from d502579 to 9f380d6 Compare November 18, 2026 13:06
@Steveb-p
Steveb-p force-pushed the feature/language-renumber-6.0 branch from 4be3607 to ab14946 Compare November 18, 2026 13:06
}

$ids = array_map('intval', $this->connection->fetchFirstColumn('SELECT id FROM ' . self::LANGUAGE_TABLE . ' ORDER BY id'));
if ($ids === [] || $ids === range(2, count($ids) + 1)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ids pass 2^53 only with a language on bit 53 or higher. Why renumber every upgraded site instead of only those? For the rest it rewrites most of ibexa_content_field and ibexa_search_object_word_link for nothing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'd rather keep a single path. With a threshold, the renumbering would only ever run on sites with more than 52 languages: the biggest and most customised ones, and the ones we'd never see it run on before. Renumbering everywhere gives every 6.0 database the same ids as a fresh install, and one rule for projects ("ids change on upgrade, give your language columns a cascading key") instead of one that only applies past 52 languages.

It isn't free, though. On 3M fields and 10M word links, renumbering 2/4/8 to 2/3/4 takes 164 s on MySQL, 112 s on MariaDB and 351 s on PostgreSQL. Your other comment helps there: going one language at a time instead of through negative ids brings it down to 103 s / 66 s / 164 s, so I'll switch to that.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is however one time operation if I did not miss anything, I don't we should care about performance that much here after giving this a second look. WDYT?

-- The ids go negative first: primary keys are checked row by row, and a new id may still belong to another language.
-- The foreign keys cascade both steps.
-- There's no WHERE, as MySQL can't refer to a temporary table twice in one statement. COALESCE keeps the ids missing from the mapping.
UPDATE ibexa_content_language SET id = COALESCE(-(SELECT r.new_id FROM ibexa_content_language_renumbering r WHERE r.old_id = ibexa_content_language.id), id);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why through negative ids? A new id is never above the old one, so one language at a time in ascending order can't collide, and the big tables cascade once instead of twice.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Switched to that in 7316ae4: one UPDATE per language in ascending order, so the big tables cascade once. On 3M fields and 10M word links it went from 164 s to 103 s on MySQL, 112 s to 66 s on MariaDB, and 351 s to 164 s on PostgreSQL.

$this->abortIfLeftBehind(
array_keys(array_filter($columns, static fn (bool $cascades): bool => !$cascades)),
sprintf(
'Add a foreign key to "%s" with ON UPDATE CASCADE to each of them, in a migration that runs before this one.',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Project migrations sort after every Ibexa one (IbexaMigrationComparator), so how does a project add its key "before this one"? And a language_id column pointing at a project's own table has no way out at all.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right on both counts. In 240853c a language_id/*_language_id column with a foreign key to another table is left out, and the abort no longer asks for a migration that runs before this one. It aborts before changing anything, so the project adds the keys (with plain SQL, or its own migration run on its own with doctrine:migrations:execute --up) and migrates again from here. I've put the same in the upgrade note in the description.

if ($this->isSqlite() && !(bool)$this->connection->fetchOne('PRAGMA foreign_keys')) {
$this->abortIfLeftBehind(
array_diff(array_keys($columns), self::RENUMBERED_WITHOUT_FOREIGN_KEYS),
'SQLite doesn\'t enforce foreign keys on this connection, so only the core columns get renumbered. Run this migration with PRAGMA foreign_keys = ON.'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

#964's migrations abort with PRAGMA foreign_keys on, this one aborts with it off once a package table is there (product-catalog, share). So on SQLite the upgrade can't go in one run?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not anymore. #964 now switches PRAGMA foreign_keys off for its table rebuilds, and in 1d13bc4 this one does the opposite: on SQLite it runs outside a transaction and switches the foreign keys on for the renumbering when the connection doesn't enforce them, then back off. The cascades reach the packages' tables either way, so the SQL file renumbering only core's columns and its abort are gone, and the upgrade goes through in one run whatever the setting.

@Steveb-p
Steveb-p force-pushed the feature/language-foreign-keys-6.0 branch from 9f380d6 to 86e1c98 Compare November 18, 2026 15:35
@Steveb-p
Steveb-p force-pushed the feature/language-renumber-6.0 branch 2 times, most recently from 0690ada to bb4c971 Compare November 18, 2026 15:56
@alongosz alongosz added the Doc needed The changes require some documentation label Nov 18, 2026

@alongosz alongosz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

From my side this idea is still okay.

Comment on lines +100 to +104
$this->addSqlFile(match (true) {
$this->isSqlite() => __DIR__ . '/sql/renumber-language-ids-sqlite.sql',
$this->isPostgreSQL() => __DIR__ . '/sql/renumber-language-ids-postgresql.sql',
default => __DIR__ . '/sql/renumber-language-ids-mysql.sql',
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

renumber-language-ids-sqlite.sql and renumber-language-ids-postgresql.sql are byte for byte the same, can we keep one?

Suggested change
$this->addSqlFile(match (true) {
$this->isSqlite() => __DIR__ . '/sql/renumber-language-ids-sqlite.sql',
$this->isPostgreSQL() => __DIR__ . '/sql/renumber-language-ids-postgresql.sql',
default => __DIR__ . '/sql/renumber-language-ids-mysql.sql',
});
$this->addSqlFile(match (true) {
$this->isPostgreSQL(), $this->isSqlite() => __DIR__ . '/sql/renumber-language-ids.sql',
default => __DIR__ . '/sql/renumber-language-ids-mysql.sql',
});

{
private const string LANGUAGE_TABLE = 'ibexa_content_language';

private const string LANGUAGE_ID_COLUMN_PATTERN = '/(?:^|_)language_id$/';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Without a FK only language_id / *_language_id names are caught. A project column like lang_id or languageId (ORM default naming strategy) holding language ids is skipped silently and keeps the old ids, which then belong to other languages. Not sure we can catch that here, but maybe worth a line in the upgrade doc next to the FK requirement?

@Steveb-p
Steveb-p force-pushed the feature/language-renumber-6.0 branch from 240853c to 1d13bc4 Compare November 19, 2026 15:19
The bitmask gave every language a power of two, up to 2^62, and 6.0 numbers a new
language after the highest one, so an upgraded site with many languages keeps
creating ids JavaScript can't represent exactly (past 2^53).

RenumberLanguageIdsMigration gives the languages the ids a fresh install would, in
the order of their old ids, and does nothing once they're already 2, 3, 4… It
renumbers ibexa_content_language in two steps through negative ids, and the
ON UPDATE CASCADE foreign keys carry the new ids to the tables referencing it; the
search word links, which have none, are updated alongside. Languages keeping
their id are left out, so their rows aren't rewritten twice. It refuses while a
foreign key to the language table doesn't cascade updates. On SQLite without
enforced foreign keys it updates every column itself.

InnoDB fails to cascade a new id into a primary key column while another column of the
same row references the same language, so on MySQL and MariaDB the object state group
translations are set aside and inserted back with the new ids. The migration runs in
one transaction on every platform.
…negative ids

In ascending order a new id is free by the time it's used, so the foreign keys cascade once instead of twice.
…of the language renumbering

A project's language_id column with a foreign key to its own table no longer blocks the renumbering. The abort now tells to add the missing keys and migrate again, as a project migration can't sort before this one.
The renumbering runs outside a transaction on SQLite, enabling PRAGMA foreign_keys around it when the connection doesn't enforce them, so the cascades reach the packages' tables too. That replaces the SQL file renumbering only core's columns, and the upgrade goes through in one run either way.
@Steveb-p
Steveb-p force-pushed the feature/language-foreign-keys-6.0 branch from 9997c72 to b6a64be Compare November 21, 2026 16:16
@Steveb-p
Steveb-p force-pushed the feature/language-renumber-6.0 branch from 1d13bc4 to 4f4691e Compare November 21, 2026 16:16
@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
E Reliability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants