From efb1b2bdad65a8641e5143fa76846cf9be9cb841 Mon Sep 17 00:00:00 2001 From: enlineawork Date: Wed, 9 Sep 2026 21:09:10 -0300 Subject: [PATCH] fix(F6.1): align DB operational context check with area-owned model --- ...-1-area-owned-operational-context-check.ts | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 api-v3/src/database/migrations/1790094500000-f6-1-area-owned-operational-context-check.ts diff --git a/api-v3/src/database/migrations/1790094500000-f6-1-area-owned-operational-context-check.ts b/api-v3/src/database/migrations/1790094500000-f6-1-area-owned-operational-context-check.ts new file mode 100644 index 0000000..d735b5f --- /dev/null +++ b/api-v3/src/database/migrations/1790094500000-f6-1-area-owned-operational-context-check.ts @@ -0,0 +1,35 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +/** + * F5/F6 permits physical Inventory to belong to an Área without persisting a + * Company snapshot. Company snapshots are optional creation/history metadata; + * current ownership is resolved from area_company_relations. + * + * The F4 paired CHECK survived the trigger migration and contradicted that + * model. Keep only the invariant that a Company snapshot cannot exist without + * an Área. + */ +export class F61AreaOwnedOperationalContextCheck1790094500000 implements MigrationInterface { + name='F61AreaOwnedOperationalContextCheck1790094500000'; + + public async up(queryRunner:QueryRunner):Promise{ + await queryRunner.query('ALTER TABLE assets DROP CONSTRAINT IF EXISTS chk_assets_operational_assignment_pair'); + await queryRunner.query(` + ALTER TABLE assets + ADD CONSTRAINT chk_assets_operational_assignment_pair + CHECK (operator_company_id IS NULL OR operational_area_id IS NOT NULL) + `); + } + + public async down(queryRunner:QueryRunner):Promise{ + await queryRunner.query('ALTER TABLE assets DROP CONSTRAINT IF EXISTS chk_assets_operational_assignment_pair'); + await queryRunner.query(` + ALTER TABLE assets + ADD CONSTRAINT chk_assets_operational_assignment_pair + CHECK ( + (operational_area_id IS NULL AND operator_company_id IS NULL) + OR (operational_area_id IS NOT NULL AND operator_company_id IS NOT NULL) + ) + `); + } +}