2025-04-02 05:41:10 +00:00
|
|
|
import { MigrationInterface, QueryRunner } from 'typeorm';
|
|
|
|
|
|
2025-07-02 05:27:36 +00:00
|
|
|
export class UpdateGlobalDataSources1742905945987 implements MigrationInterface {
|
2025-04-02 05:41:10 +00:00
|
|
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
2025-07-11 05:23:52 +00:00
|
|
|
const constraintExists = await queryRunner.query(`
|
|
|
|
|
SELECT 1
|
|
|
|
|
FROM information_schema.table_constraints
|
|
|
|
|
WHERE constraint_name = 'chk_global_data_source_app_version_id'
|
|
|
|
|
AND table_name = 'data_sources'
|
|
|
|
|
`);
|
|
|
|
|
|
|
|
|
|
if (constraintExists && constraintExists.length > 0) {
|
|
|
|
|
console.log('Constraint chk_global_data_source_app_version_id already exists. Skipping migration.');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-02 05:41:10 +00:00
|
|
|
// Step 1: Set app_version_id to NULL for existing global data sources
|
|
|
|
|
await queryRunner.query(`
|
|
|
|
|
UPDATE data_sources
|
|
|
|
|
SET app_version_id = NULL
|
|
|
|
|
WHERE scope = 'global'
|
|
|
|
|
`);
|
|
|
|
|
|
|
|
|
|
// Step 2: Add a check constraint to ensure app_version_id is NULL for global data sources
|
|
|
|
|
await queryRunner.query(`
|
|
|
|
|
ALTER TABLE data_sources
|
|
|
|
|
ADD CONSTRAINT chk_global_data_source_app_version_id
|
|
|
|
|
CHECK (scope != 'global' OR app_version_id IS NULL)
|
|
|
|
|
`);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-11 05:23:52 +00:00
|
|
|
public async down(queryRunner: QueryRunner): Promise<void> {
|
|
|
|
|
// Drop the constraint if it exists
|
|
|
|
|
const constraintExists = await queryRunner.query(`
|
|
|
|
|
SELECT 1
|
|
|
|
|
FROM information_schema.table_constraints
|
|
|
|
|
WHERE constraint_name = 'chk_global_data_source_app_version_id'
|
|
|
|
|
AND table_name = 'data_sources'
|
|
|
|
|
`);
|
|
|
|
|
|
|
|
|
|
if (constraintExists && constraintExists.length > 0) {
|
|
|
|
|
await queryRunner.query(`
|
|
|
|
|
ALTER TABLE data_sources
|
|
|
|
|
DROP CONSTRAINT chk_global_data_source_app_version_id
|
|
|
|
|
`);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-02 05:41:10 +00:00
|
|
|
}
|