-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add ItemsSyncStep and VehiclesSyncStep for ISSUE-198 #229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
29abdf2
feat: add ItemsSyncStep for ISSUE-198
GitAddRemote ca74985
fix: filter invalid category_attribute from summary; fix param layout…
GitAddRemote bb90b9d
fix: defer parent_uex_id writes to explicit passes; fix stale loaner …
GitAddRemote c25e1de
fix: guard company/vehicle/category-attribute FK refs in vehicles and…
GitAddRemote 33eab1b
fix: exclude unknown category_attribute refs from attributes_summary …
GitAddRemote da71b58
Potential fix for pull request finding
GitAddRemote c562860
fix: delete stale station_item_attribute rows before pass 2 re-insert
GitAddRemote 347aaf6
fix: wrap per-item attribute DELETE + re-insert in a transaction
GitAddRemote 7fead29
fix: move item upsert inside attribute transaction; add uuid-based re…
GitAddRemote d552911
fix: fix uuid reconciliation to avoid phantom rows and FK violations
GitAddRemote d3895b2
fix: make item FK constraints deferrable to allow safe uex_id re-keying
GitAddRemote 3141f7b
fix: drop redundant idx_items_uuid when adding unique uuid index
GitAddRemote 4e65bfa
fix: register all missing migrations in data-source.ts
GitAddRemote File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
backend/src/migrations/1780010901444-AddUniqueUuidToStationItem.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
|
||
| export class AddUniqueUuidToStationItem1780010901444 | ||
| implements MigrationInterface | ||
| { | ||
| public async up(queryRunner: QueryRunner): Promise<void> { | ||
| // Remove duplicate uuid rows before adding the constraint, keeping the | ||
| // most recently synced row for each uuid (NULL uuids are never deduplicated | ||
| // by a UNIQUE constraint so they are left as-is). | ||
| await queryRunner.query(` | ||
| DELETE FROM station_item | ||
| WHERE id IN ( | ||
| SELECT id FROM ( | ||
| SELECT id, | ||
| ROW_NUMBER() OVER (PARTITION BY uuid ORDER BY synced_at DESC, id DESC) AS rn | ||
| FROM station_item | ||
| WHERE uuid IS NOT NULL | ||
| ) ranked | ||
| WHERE rn > 1 | ||
| ) | ||
| `); | ||
|
|
||
| await queryRunner.query( | ||
| `CREATE UNIQUE INDEX "uq_station_item_uuid" ON "station_item" ("uuid") WHERE "uuid" IS NOT NULL`, | ||
| ); | ||
|
|
||
| // Drop the baseline non-unique idx_items_uuid — it is fully superseded by | ||
| // the partial unique index above, which also serves as a lookup index. | ||
| // Keeping both would leave a non-unique index that contradicts the new | ||
| // uniqueness guarantee and could cause the reconciliation SELECT to return | ||
| // an arbitrary row if a duplicate somehow slipped through. | ||
| await queryRunner.query(`DROP INDEX IF EXISTS "idx_items_uuid"`); | ||
| } | ||
|
|
||
| public async down(queryRunner: QueryRunner): Promise<void> { | ||
| await queryRunner.query(`DROP INDEX IF EXISTS "uq_station_item_uuid"`); | ||
| await queryRunner.query( | ||
| `CREATE INDEX "idx_items_uuid" ON "station_item" ("uuid")`, | ||
| ); | ||
| } | ||
| } | ||
89 changes: 89 additions & 0 deletions
89
backend/src/migrations/1780020000000-MakeItemFksDeferrable.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
|
||
| export class MakeItemFksDeferrable1780020000000 implements MigrationInterface { | ||
| public async up(queryRunner: QueryRunner): Promise<void> { | ||
| // Drop the auto-named inline FK from station_item_attribute.item_uex_id | ||
| // and recreate it as DEFERRABLE INITIALLY DEFERRED so the UUID-based | ||
| // uex_id reconciliation in ItemsSyncStep can re-key dependents and update | ||
| // the referenced uex_id within a single transaction without violating the | ||
| // FK mid-statement. | ||
| await queryRunner.query(` | ||
| DO $$ | ||
| DECLARE | ||
| con_name TEXT; | ||
| BEGIN | ||
| SELECT conname INTO con_name | ||
| FROM pg_constraint | ||
| WHERE conrelid = 'station_item_attribute'::regclass | ||
| AND confrelid = 'station_item'::regclass | ||
| AND contype = 'f' | ||
| LIMIT 1; | ||
|
|
||
| IF con_name IS NOT NULL THEN | ||
| EXECUTE format('ALTER TABLE station_item_attribute DROP CONSTRAINT %I', con_name); | ||
| END IF; | ||
| END $$; | ||
| `); | ||
|
|
||
| await queryRunner.query(` | ||
| ALTER TABLE station_item_attribute | ||
| ADD CONSTRAINT fk_item_attr_item_uex_id | ||
| FOREIGN KEY (item_uex_id) | ||
| REFERENCES station_item (uex_id) | ||
| ON DELETE CASCADE | ||
| DEFERRABLE INITIALLY DEFERRED | ||
| `); | ||
|
|
||
| // Drop and recreate the self-referential FK on station_item.parent_uex_id. | ||
| await queryRunner.query(` | ||
| DO $$ | ||
| DECLARE | ||
| con_name TEXT; | ||
| BEGIN | ||
| SELECT conname INTO con_name | ||
| FROM pg_constraint | ||
| WHERE conrelid = 'station_item'::regclass | ||
| AND confrelid = 'station_item'::regclass | ||
| AND contype = 'f' | ||
| LIMIT 1; | ||
|
|
||
| IF con_name IS NOT NULL THEN | ||
| EXECUTE format('ALTER TABLE station_item DROP CONSTRAINT %I', con_name); | ||
| END IF; | ||
| END $$; | ||
| `); | ||
|
|
||
| await queryRunner.query(` | ||
| ALTER TABLE station_item | ||
| ADD CONSTRAINT fk_item_parent_uex_id | ||
| FOREIGN KEY (parent_uex_id) | ||
| REFERENCES station_item (uex_id) | ||
| ON DELETE SET NULL | ||
| DEFERRABLE INITIALLY DEFERRED | ||
| `); | ||
|
GitAddRemote marked this conversation as resolved.
|
||
| } | ||
|
|
||
| public async down(queryRunner: QueryRunner): Promise<void> { | ||
| await queryRunner.query( | ||
| `ALTER TABLE station_item_attribute DROP CONSTRAINT IF EXISTS fk_item_attr_item_uex_id`, | ||
| ); | ||
| await queryRunner.query(` | ||
| ALTER TABLE station_item_attribute | ||
| ADD CONSTRAINT fk_item_attr_item_uex_id | ||
| FOREIGN KEY (item_uex_id) | ||
| REFERENCES station_item (uex_id) | ||
| ON DELETE CASCADE | ||
| `); | ||
|
|
||
| await queryRunner.query( | ||
| `ALTER TABLE station_item DROP CONSTRAINT IF EXISTS fk_item_parent_uex_id`, | ||
| ); | ||
| await queryRunner.query(` | ||
| ALTER TABLE station_item | ||
| ADD CONSTRAINT fk_item_parent_uex_id | ||
| FOREIGN KEY (parent_uex_id) | ||
| REFERENCES station_item (uex_id) | ||
| ON DELETE SET NULL | ||
| `); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.