ToolJet/server/data-migrations/1745409631920-AddDataSourceConstraintsForStatic.ts
Midhun G S 1fbb148f16
Deprecated static local data sources (#12687)
* deprecated static local data sources

* changes

* transactions wrapped under dbTransaction wrap

* removed entity manager from app import export service

* Update export app cypress test case.

* update app export test case

* type error fix for loader

* fix for listing of tooljetdb for app import export

* postgrest configs

* added readme

* cleanup

* fix for app import export version order

* Update app export spec

* fix

---------

Co-authored-by: ajith-k-v <ajith.jaban@gmail.com>
2025-06-22 12:59:13 +05:30

45 lines
1.8 KiB
TypeScript

import { MigrationInterface, QueryRunner } from 'typeorm';
export class AddDataSourceConstraintsForStatic1745409631920 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
console.log('Starting migration to add constraints to data_sources table');
// Add check constraint to ensure static type data sources have global scope
await queryRunner.query(`
ALTER TABLE data_sources
ADD CONSTRAINT chk_static_type_global_scope
CHECK (type != 'static' OR scope = 'global');
`);
console.log('Added constraint: static type data sources must have global scope');
// Add unique constraint for combination of kind, type, and organization_id
await queryRunner.query(`
CREATE UNIQUE INDEX idx_unique_static_kind_org
ON public.data_sources (kind, type, organization_id)
WHERE type = 'static';
`);
console.log('Added unique constraint on kind, type, and organization_id');
console.log('Migration completed successfully');
}
public async down(queryRunner: QueryRunner): Promise<void> {
console.log('Starting rollback of data_sources constraints');
// Drop the unique constraint
await queryRunner.query(`
ALTER TABLE public.data_sources
DROP CONSTRAINT IF EXISTS idx_unique_static_kind_org;
`);
console.log('Dropped unique constraint on kind, type, and organization_id');
// Drop the check constraint
await queryRunner.query(`
ALTER TABLE public.data_sources
DROP CONSTRAINT IF EXISTS chk_static_type_global_scope;
`);
console.log('Dropped constraint: static type data sources must have global scope');
console.log('Rollback completed successfully');
}
}