#!/usr/bin/env node /** * Clean n8n workflow data before fresh import to avoid constraint violations. */ let Client; try { Client = require('/usr/local/lib/node_modules/n8n/node_modules/pg').Client; } catch { Client = require('pg').Client; } const client = new Client({ host: process.env.DB_POSTGRESDB_HOST || 'postgres', port: parseInt(process.env.DB_POSTGRESDB_PORT || '5432', 10), database: process.env.DB_POSTGRESDB_DATABASE || 'farmafinder', user: process.env.DB_POSTGRESDB_USER || 'farmafinder', password: process.env.DB_POSTGRESDB_PASSWORD, }); async function run() { await client.connect(); console.log('[cleanup-db] Connected to PostgreSQL'); try { for (const table of [ 'workflows_tags', 'webhook_entity', 'execution_entity', 'workflow_statistics', 'workflow_archive', 'workflow_entity', ]) { try { const r = await client.query(`DELETE FROM ${table}`); if (r.rowCount > 0) console.log(`[cleanup-db] Deleted ${r.rowCount} rows from ${table}`); } catch (e) { if (e.code !== '42P01') console.error(`[cleanup-db] ${table}:`, e.message); } } console.log('[cleanup-db] Done'); } finally { await client.end(); } } run();