Repository navigation
Conversation
8aafcd3 to
e1b65d9
Compare
28485ee to
565498f
Compare
e1b65d9 to
1322b10
Compare
565498f to
1bef7c1
Compare
1322b10 to
7c5dbff
Compare
32391a2 to
31972d9
Compare
09eff57 to
741c80b
Compare
31972d9 to
b082d9e
Compare
741c80b to
4be3607
Compare
d502579 to
9f380d6
Compare
4be3607 to
ab14946
Compare
| } | ||
|
|
||
| $ids = array_map('intval', $this->connection->fetchFirstColumn('SELECT id FROM ' . self::LANGUAGE_TABLE . ' ORDER BY id')); | ||
| if ($ids === [] || $ids === range(2, count($ids) + 1)) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.', |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.' |
There was a problem hiding this comment.
#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?
There was a problem hiding this comment.
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.
9f380d6 to
86e1c98
Compare
0690ada to
bb4c971
Compare
alongosz
left a comment
There was a problem hiding this comment.
From my side this idea is still okay.
| $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', | ||
| }); |
There was a problem hiding this comment.
renumber-language-ids-sqlite.sql and renumber-language-ids-postgresql.sql are byte for byte the same, can we keep one?
| $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$/'; |
There was a problem hiding this comment.
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?
240853c to
1d13bc4
Compare
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.
…mn has no cascading foreign key
…he language renumbering
…ySQL language renumbering
…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.
9997c72 to
b6a64be
Compare
1d13bc4 to
4f4691e
Compare
|




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.
RenumberLanguageIdsMigrationgives 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 renumbersibexa_content_languageone language at a time in ascending order, so a new id is free by the time it's used, and theON UPDATE CASCADEforeign 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 alanguage_id/*_language_idcolumn 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_idis in its primary key,language_idholds 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 overridesisTransactional()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_keysinside 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.phpBy 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 CASCADEforeign key toibexa_content_language. Without it,RenumberLanguageIdsMigrationaborts, 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, withdoctrine:migrations:execute --up.