All checks were successful
Trigger Cloudarix Deploy / call-webhook (push) Successful in 1s
44 lines
1.3 KiB
TypeScript
44 lines
1.3 KiB
TypeScript
import { drizzle } from 'drizzle-orm/node-postgres';
|
|
import { Pool } from 'pg';
|
|
import { sql } from 'drizzle-orm';
|
|
|
|
async function fixConstraint() {
|
|
const dbUrl = process.env.DATABASE_URL || 'postgres://postgres:postgres@localhost:5432/tenantpilot';
|
|
console.log('🔌 Connecting to:', dbUrl.replace(/:[^:@]+@/, ':****@'));
|
|
|
|
const pool = new Pool({ connectionString: dbUrl });
|
|
const db = drizzle(pool);
|
|
|
|
try {
|
|
console.log('⏳ Removing duplicate rows...');
|
|
await db.execute(sql`
|
|
DELETE FROM policy_settings a
|
|
USING policy_settings b
|
|
WHERE a.id > b.id
|
|
AND a.tenant_id = b.tenant_id
|
|
AND a.graph_policy_id = b.graph_policy_id
|
|
AND a.setting_name = b.setting_name
|
|
`);
|
|
|
|
console.log('⏳ Dropping old index...');
|
|
await db.execute(sql`DROP INDEX IF EXISTS policy_settings_upsert_idx`);
|
|
|
|
console.log('⏳ Adding unique constraint...');
|
|
await db.execute(sql`
|
|
ALTER TABLE policy_settings
|
|
ADD CONSTRAINT policy_settings_upsert_unique
|
|
UNIQUE(tenant_id, graph_policy_id, setting_name)
|
|
`);
|
|
|
|
console.log('✅ Constraint added successfully!');
|
|
await pool.end();
|
|
process.exit(0);
|
|
} catch (error) {
|
|
console.error('❌ Failed:', error);
|
|
await pool.end();
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
fixConstraint();
|