Repository navigation
fix(db): reconcile schema ownership and integrity across engines (WI-2366) - #568
stefan-ernst wants to merge 3 commits into
Conversation
…2366) Give each PostgreSQL table one authoritative bootstrap definition. The base_tables stub silently won board_configurations, test_folders, test_labels and iteration_types, dropping four cascading foreign keys and relaxing iteration_types; the domain files now own those tables and a migration reconciles orphans and restores the keys. Make configuration_set_item_types the item-type membership source. Workflow resolution goes through the workspace's effective set, and the scalar item_types.configuration_set_id is dropped after backfilling only custom types, preserving the seed's deliberate exclusion of Initiative. Align agent-binding creator deletion: SQLite now allows the creator to be nulled, matching PostgreSQL and the soft-audit model, and the repository reader handles NULL. Enforce exactly one owner and at most one configuration per board owner, reconciling duplicates without losing board columns. Make agent-binding repository replacement atomic with the parent version bump. Cleanup: drop the stale SQLite request_types DDL, the duplicate time-category junction, the unused milestone bootstrap files, duplicate items foreign keys on fresh installs, and the redundant two-column PostgreSQL notification-setting uniqueness.
|
I’d request changes before merging. I reproduced five upgrade issues using the previous SQLite schema and the migration SQL from
The focused companion database and service tests passed on SQLite and PostgreSQL. I did not rerun the full suites. Test commands../core-tests-wi-2366/overlay.sh ../core-wi-2366 -- -tags=test -count=1 -run 'Test(Schema_|Migration_BoardConfigurationOwnershipReconciles|Migration_ItemTypeMembershipBackfillsCustomTypes|GetInitialStatusForItemTypeResolvesWorkspaceSet)' ./internal/database ./internal/services
../core-tests-wi-2366/scripts/native-postgres.sh run -- ../core-tests-wi-2366/overlay.sh ../core-wi-2366 -- -tags=test -count=1 -run 'Test(Schema_|Migration_BoardConfigurationOwnershipReconciles|Migration_ItemTypeMembershipBackfillsCustomTypes|Migration_PostgresSchemaAuthorityRestoresCascades|GetInitialStatusForItemTypeResolvesWorkspaceSet)' ./internal/database ./internal/services |
Address the upgrade-path review findings: - Normalize dual-owner board configurations to their workspace owner before the exactly-one-owner check, so such a row no longer blocks startup. - Remove board columns (and their status mappings) left without a configuration. The SQLite rebuild runs with foreign keys off, so the ownerless-configuration delete did not cascade and a later board could inherit a deleted board's column. - Preserve the sqlite_sequence high-water mark across the binding and board table rebuilds. A deleted binding's id is still referenced by historical agent runs and must not be reused. - Backfill item-type membership only for custom types with no junction membership at all, and join configuration_sets so a scalar pointing at a deleted set cannot fail the foreign key.
|
Thanks — all five are fixed in
New tests cover each case: dual-owner normalization + orphan cleanup, a scalar to a deleted set, a type already moved to another set, and sequence preservation across the binding rebuild. Ran the focused database and service tests on SQLite and PostgreSQL, plus the full |
|
Re-reviewed One remaining finding: Please add |
The base_tables stub defaulted iteration_types.color to '#6b7280' while the fresh schema requires an explicit color. The alignment migration set NOT NULL but left the default, so an upgraded PostgreSQL database accepted a colorless insert that a fresh one rejects. Drop the default and require column_default IS NULL in the migration check.
|
Fixed in
|
Reconciles the database schema findings from the 0.9.6 design review (WI-2366).
Priority fixes
1. One authoritative PostgreSQL bootstrap definition.
base_tables_postgres.sqldeclared eleven tables before their owning domain files. Because the file runs first,CREATE TABLE IF NOT EXISTSwon and silently discarded the domain definition. Four cascading foreign keys were lost (board_configurations.workspace_id/collection_id,test_folders.workspace_id,test_labels.workspace_id) anditeration_typeskept an unreadduration_dayscolumn with a nullablecolor. The duplicate stubs are removed; the domain files now own those tables. Migration20261205_postgres_schema_authorityreconciles orphan rows, restores the four keys, and alignsiteration_typeswith SQLite and the Go model.2.
configuration_set_item_typesis the membership source of truth. Workflow resolution now goes through the submitting workspace's effective configuration set (assigned set, else the global default) and the junction, instead of an item-type-only join with an unorderedLIMIT 1. Migration20261208_item_type_membership_authoritybackfills the junction only for custom (non-built-in) scalar-only rows, so the seed's deliberate exclusion ofInitiativefrom the simplified default set is preserved, then dropsitem_types.configuration_set_id.3. Agent-binding creator deletion. SQLite declared
created_by_user_idNOT NULLwithON DELETE SET NULL, so deleting a distinct creator failed there but nulled the row on PostgreSQL. The creator is a soft audit ref like the sibling agent tables: SQLite now allows the null and the model/reader use*int. Migration20261206_agent_binding_creator_nullablerebuilds the SQLite table.4. One owner and one configuration per board owner.
board_configurationsnow has an exactly-one-ownerCHECKand one partial unique index per owner. Migration20261207_board_configuration_ownershipmerges duplicate configurations by moving theirboard_columnsonto the lowest-id survivor (so columns are not lost), drops unreachable ownerless rows, then adds the constraints.5. Atomic binding repository replacement.
UpdateConfig's parent version bump and the delete-then-insert repository replacement now share one transaction, so a failure cannot publish a newprofile_versionwith the old or a partial repository list.Cleanup
request_typesDDL that never applied.workspace_time_project_categoriesjunction and indexes.milestones_existing*.sqlbootstrap files and stale comments.itemsforeign keys on fresh installs.UNIQUE(configuration_set_id, notification_setting_id)after collapsing duplicates and ensuring the single-column upsert target exists (20261209_notification_setting_unique).notification_templatescomments:template_typeis still read by the notification cache refresh.Deferred (documented in code)
applies_to_all_workspaces,zammad_connection_workspaces): retained until every supported upgrade has run the20260901_zammad_canonical_workspace_scopebackfill.event_type,template_subject,template_body,template_variables,channel_type): need a deliberate audit of existing rows before removal.itemsforeign keys on already-installed SQLite databases: folded into a planneditemsrebuild.Verification
Tests live in the companion core-tests PR. On SQLite the database, repository, services, handlers, and black-box suites pass. On PostgreSQL the database suite (including new fresh-install and upgrade-path tests), repository suite, and black-box suite pass. Two pre-existing PostgreSQL failures reproduce on the base commit and are unrelated to this change:
TestCustomFieldIndexLimitandTestSLAOccurrenceNotificationDeliveryKeyDeduplicates.