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) + ) + `); + } +}