Skip to content

IBX-13235: Added foreign keys between the object state tables - #974

Draft
Steveb-p wants to merge 57 commits into
feature/language-foreign-keys-6.0from
IBX-13235-object-state-foreign-keys
Draft

Steveb-p wants to merge 57 commits into
feature/language-foreign-keys-6.0from
IBX-13235-object-state-foreign-keys

Conversation

@Steveb-p

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

Copy link
Copy Markdown
Contributor
🎫 Issue IBX-13235

Note

Stacked on #964: on SQLite these migrations rebuild the same tables #964 rebuilds for its language keys, and keep them. I'll retarget it to 6.0 once #964 lands.

Related PRs:

Description:

The object state tables referenced each other only by convention, so nothing stopped a deleted state or group from leaving its translations or content assignments behind. They now have foreign keys, all ON UPDATE CASCADE:

  • ibexa_object_state.group_id → ibexa_object_state_group.id, ON DELETE RESTRICT
  • ibexa_object_state_group_language.contentobject_state_group_id → ibexa_object_state_group.id, ON DELETE CASCADE
  • ibexa_object_state_language.contentobject_state_id → ibexa_object_state.id, ON DELETE CASCADE
  • ibexa_object_state_link.contentobject_state_id → ibexa_object_state.id, ON DELETE RESTRICT
  • ibexa_object_state_link.contentobject_id → ibexa_content.id, ON DELETE CASCADE

Translations cascade like the other translation tables (ibexa_content_translation etc.), and the gateway deletes them before the state or group anyway. A state still assigned to content, or a group still holding states, is RESTRICT: the content has to be moved to another state first, which is what ObjectState\Handler is for. Except Handler::delete() deleted the state before moving its content to the group's default state, so with RESTRICT it'd fail, and with CASCADE the content would silently lose its state in that group. It now moves the links first (or deletes them, for the last state of a group) and deletes the state after. deleteGroup() already deleted the links, then the states, then the group.

Deleting content never removed its state links (TreeHandler::removeRawContent() doesn't touch them), so pretty much every site has links of deleted content, and ObjectStateService::getContentCount() counts them. That key cascades, and its migration deletes those links before adding it instead of aborting, as they point at nothing.

Each key has its own migration on top of AbstractAddObjectStateForeignKeyMigration, like #964's language keys: it skips itself when the key is already there, refuses with a row count when a column points at a state or group that doesn't exist, and on SQLite rebuilds the table outside a transaction, with PRAGMA foreign_keys switched off for the rebuild, as #964's do. Only ibexa_object_state_link.contentobject_state_id needed a new index (ibexa_object_state_link_state), the other columns lead an existing primary or unique key.

For tests and the legacy install path, schema.yaml now lists ibexa_object_state_group before ibexa_object_state and ibexa_object_state_link after ibexa_content (the test schema is dropped in reverse), and the clean data inserts them in that order. FixtureImporter now orders a fixture's tables by their foreign keys rather than only putting the language table first, as test_data.yaml lists the states before their group and the links before the content. When a fixture replaces the languages, it also sets the state links aside, puts the set-aside rows back in the same foreign key order as the fixture's own (so the links of objectstates.php don't go in before the content they point at), and skips those whose state, group or content is gone.

For QA:

vendor/bin/phpunit -c phpunit.xml tests/bundle/RepositoryInstaller/Migration/ObjectStateForeignKeysMigrationTest.php

By hand: on a 5.0 database with some content deleted (trash emptied), run ibexa:doctrine:migrations:migrate. It reports how many state links of deleted content it removed. Then delete an object state assigned to some content and check the content moved to the group's default state.

Documentation:

For the 6.0 upgrade guide: AddObjectStateLinkContentForeignKeyMigration deletes the object state assignments of content that doesn't exist anymore. The other AddObjectState*ForeignKeyMigration migrations stop the upgrade when a state, translation or assignment points at a state or group that doesn't exist anymore, naming the column; those rows have to be deleted before migrating again.

ibexa_search_object_word_link gets language_id and
is_main_and_always_available columns, filled from its mask. The field
criteria and sort clause, the full-text and language code criteria and
the language filter of content and location search use the join tables
and those columns instead of arithmetic on the masks. The indexer keeps
writing the word link mask until the bitmask is removed, and field
language ids are compared without their always-available bit until it is
stripped.
ReindexCommand passes the --content-ids values as strings, and the
untyped remove() hands them on to SearchIndex's int parameters, which
strict types turned into a TypeError for deleted or unpublished content.
ibexa_url_alias_ml gets an is_always_available column, and an alias's
languages come from ibexa_url_alias_ml_translation instead of lang_mask,
in cleandata.sql too. The gateway keeps lang_mask in step with them
until the bitmask is removed.
…_available

Same flag as always_available on ibexa_content and ibexa_content_version.
Content types read their languages from ibexa_content_type_name instead
of decoding the type's language_mask, and resolve language ids through
the language handler instead of MaskGenerator. They keep writing the
mask and the always-available bit of their name rows until the bitmask
is removed.
Object states resolve language ids through the language handler instead
of MaskGenerator. They keep writing the masks and the always-available
bit until the bitmask is removed.
Stops writing the masks and drops them, together with MaskGenerator. New
languages get the id after the highest one instead of the next power of
two, which lifts the limit of 62 languages, and the always-available bit
is stripped from the language_id columns, after removing the stale
object state translations eZ Publish left behind.
…en't a language, and from core's test data itself
…bitmask upgrade test

IBX-13070 removed SchemaAssetsFilterBypass, and LegacySchemaImporter no
longer takes one.
The names and fields of a version only matched its languages with the
always-available bit added too, which the language ids don't carry
anymore.
…guage table

The initial language of content, versions and content types, the language of fields,
content names, content type names and object state translations, and the default
language of object states and groups now reference ibexa_content_language, ON DELETE
RESTRICT ON UPDATE CASCADE like the translation tables. The field definition
translations' foreign key, ON DELETE CASCADE until now, follows suit under a new name.

AddLanguageForeignKeysMigration adds them after the bitmask migrations, refusing while
any row holds a language id without a language. SQLite rebuilds the tables.

schema.yaml lists the language table first and the clean data inserts it first;
FixtureImporter inserts it first and detaches every row referencing it when a fixture
replaces the languages; fixtures and tests use language ids that exist.
…gn key

MySQL and MariaDB can't roll back schema changes, so a failure halfway
through the single migration left some of the foreign keys added and
others not. Each foreign key now has its own migration, which adds it
together with its index in one statement and skips itself when the key
is already there, so the migrations' status shows which ones are in.
…key rebuilds

The migrations run outside a transaction on SQLite, so PRAGMA foreign_keys can be switched off around the table rebuild, and back on afterwards, instead of aborting when the connection enforces foreign keys.
@Steveb-p
Steveb-p force-pushed the IBX-13235-object-state-foreign-keys branch from 497009a to 9a656a1 Compare November 19, 2026 13:28
@sonarqubecloud

Copy link
Copy Markdown

@Steveb-p
Steveb-p force-pushed the feature/language-foreign-keys-6.0 branch from 9997c72 to b6a64be Compare November 21, 2026 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant