diff --git a/docker-compose.yml b/docker-compose.yml index 99bc510..df7c83b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -163,8 +163,6 @@ services: depends_on: postgres: condition: service_healthy - n8n-init: - condition: service_completed_successfully # --- N8N Init: import + activate workflows BEFORE n8n starts, then seed DB --- n8n-init: @@ -182,12 +180,15 @@ services: - n8n_data:/home/node/.n8n - ./n8n/workflows:/home/node/workflows - ./n8n/init-import.sh:/home/node/init-import.sh:ro + - ./n8n/cleanup-db.js:/home/node/cleanup-db.js:ro - ./n8n/seed.json:/home/node/seed.json:ro depends_on: postgres: condition: service_healthy parapharmacy-api: condition: service_healthy + n8n: + condition: service_healthy volumes: backend_data: diff --git a/n8n/cleanup-db.js b/n8n/cleanup-db.js new file mode 100644 index 0000000..1fa4a01 --- /dev/null +++ b/n8n/cleanup-db.js @@ -0,0 +1,46 @@ +#!/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(); diff --git a/n8n/init-import.sh b/n8n/init-import.sh index c0008bf..0bbe94c 100755 --- a/n8n/init-import.sh +++ b/n8n/init-import.sh @@ -11,9 +11,13 @@ if [ -f "$FLAG" ]; then exit 0 fi +# ── 0. Clean existing workflow data to avoid constraint violations ──── +echo "[n8n-init] Cleaning existing workflow data..." +node /home/node/cleanup-db.js 2>&1 || echo "[n8n-init] Cleanup skipped (first run or no existing data)." + # ── 1. Import workflows via CLI (writes to DB) ─────────────────────── echo "[n8n-init] Importing workflows..." -n8n import:workflow --separate --input=/home/node/workflows/ 2>/dev/null +n8n import:workflow --separate --overwrite --input=/home/node/workflows/ 2>/dev/null # ── 3. Activate all workflows via CLI ──────────────────────────────── echo "[n8n-init] Activating workflows..." diff --git a/n8n/workflows/parapharmacy-all-sources.json b/n8n/workflows/parapharmacy-all-sources.json index b37d6ad..c454318 100644 --- a/n8n/workflows/parapharmacy-all-sources.json +++ b/n8n/workflows/parapharmacy-all-sources.json @@ -129,6 +129,5 @@ } }, "active": false, - "settings": { "executionOrder": "v1" }, - "tags": [{ "name": "parapharmacy" }, { "name": "scraper" }, { "name": "scheduled" }] + "settings": { "executionOrder": "v1" } } diff --git a/n8n/workflows/parapharmacy-manual-scraper.json b/n8n/workflows/parapharmacy-manual-scraper.json index 545a8ca..26004e3 100644 --- a/n8n/workflows/parapharmacy-manual-scraper.json +++ b/n8n/workflows/parapharmacy-manual-scraper.json @@ -166,16 +166,5 @@ "settings": { "executionOrder": "v1" }, - "versionId": "1", - "tags": [ - { - "name": "parapharmacy" - }, - { - "name": "scraper" - }, - { - "name": "webhook" - } - ] + "versionId": "1" } diff --git a/n8n/workflows/parapharmacy-scraper.json b/n8n/workflows/parapharmacy-scraper.json index 6c5903a..4aeee42 100644 --- a/n8n/workflows/parapharmacy-scraper.json +++ b/n8n/workflows/parapharmacy-scraper.json @@ -170,13 +170,5 @@ "settings": { "executionOrder": "v1" }, - "versionId": "1", - "tags": [ - { - "name": "parapharmacy" - }, - { - "name": "scraper" - } - ] + "versionId": "1" } diff --git a/n8n/workflows/parapharmacy-webhook-scraper.json b/n8n/workflows/parapharmacy-webhook-scraper.json index 68305a6..3006788 100644 --- a/n8n/workflows/parapharmacy-webhook-scraper.json +++ b/n8n/workflows/parapharmacy-webhook-scraper.json @@ -94,6 +94,5 @@ "Send to API": { "main": [[{ "node": "Format Response", "type": "main", "index": 0 }]] } }, "active": true, - "settings": { "executionOrder": "v1" }, - "tags": [{ "name": "parapharmacy" }, { "name": "scraper" }] + "settings": { "executionOrder": "v1" } }