Files
dh-inspeccion-v2/api-v3/src/database/migrations/1787853600000-phase-d5-3-8-local-inventory-structure.ts
T

90 lines
3.9 KiB
TypeScript

import { MigrationInterface, QueryRunner } from 'typeorm';
export class PhaseD538LocalInventoryStructure1787853600000 implements MigrationInterface {
name = 'PhaseD538LocalInventoryStructure1787853600000';
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE asset_import_plan_items DROP CONSTRAINT chk_asset_import_plan_item_kind
`);
await queryRunner.query(`
ALTER TABLE asset_import_plan_items ADD CONSTRAINT chk_asset_import_plan_item_kind CHECK (
entity_kind IN (
'DEPARTMENT','ORGANIZATION','AREA','AREA_DEPARTMENT_RELATION','FIELD','OPERATOR_RELATION',
'LEGAL_RIGHT','LEGAL_RIGHT_ORGANIZATION','INSTALLATION','LOCAL_STRUCTURE','TECHNICAL_ASSET'
)
)
`);
await queryRunner.query(`
INSERT INTO asset_types (code,name,description,can_be_root,is_active,operational_role)
SELECT
'estructura_local',
'Estructura local (fuente)',
'Nodo estructural provisional que conserva la nomenclatura propia de cada operadora o Área/Yacimiento. No implica una clasificación física normalizada por DH hasta su revisión.',
false,true,'GENERIC'
WHERE NOT EXISTS (SELECT 1 FROM asset_types WHERE lower(code)='estructura_local')
`);
await queryRunner.query(`
INSERT INTO asset_attribute_definitions (asset_type_id,code,name,data_type,is_required,is_active,sort_order)
SELECT type.id,definition.code,definition.name,'TEXT',false,true,definition.sort_order
FROM asset_types type
CROSS JOIN (VALUES
('nivel_fuente','Nivel informado por la fuente',10),
('clasificacion_fuente','Clasificación local informada',20),
('ruta_fuente','Ruta / nomenclatura de origen',30)
) AS definition(code,name,sort_order)
WHERE lower(type.code)='estructura_local'
AND NOT EXISTS (
SELECT 1 FROM asset_attribute_definitions existing
WHERE existing.asset_type_id=type.id AND lower(existing.code)=lower(definition.code)
)
`);
await queryRunner.query(`
INSERT INTO asset_type_parent_rules (child_type_id,parent_type_id)
SELECT local.id,parent.id
FROM asset_types local
JOIN asset_types parent ON lower(parent.code) IN ('area','yacimiento','locacion')
WHERE lower(local.code)='estructura_local'
ON CONFLICT DO NOTHING
`);
await queryRunner.query(`
INSERT INTO asset_type_parent_rules (child_type_id,parent_type_id)
SELECT child.id,local.id
FROM asset_types child
CROSS JOIN asset_types local
WHERE lower(local.code)='estructura_local'
AND child.operational_role='GENERIC'
AND lower(child.code) NOT IN ('yacimiento','locacion','estructura_local','pozo','ducto','colector')
ON CONFLICT DO NOTHING
`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE asset_import_plan_items DROP CONSTRAINT chk_asset_import_plan_item_kind`);
await queryRunner.query(`
ALTER TABLE asset_import_plan_items ADD CONSTRAINT chk_asset_import_plan_item_kind CHECK (
entity_kind IN (
'DEPARTMENT','ORGANIZATION','AREA','AREA_DEPARTMENT_RELATION','FIELD','OPERATOR_RELATION',
'LEGAL_RIGHT','LEGAL_RIGHT_ORGANIZATION','INSTALLATION','TECHNICAL_ASSET'
)
)
`);
await queryRunner.query(`
DELETE FROM asset_type_parent_rules rule
USING asset_types local
WHERE lower(local.code)='estructura_local'
AND (rule.child_type_id=local.id OR rule.parent_type_id=local.id)
`);
await queryRunner.query(`
DELETE FROM asset_attribute_definitions definition
USING asset_types local
WHERE definition.asset_type_id=local.id AND lower(local.code)='estructura_local'
`);
await queryRunner.query(`DELETE FROM asset_types WHERE lower(code)='estructura_local'`);
}
}