Files
Antoni Nuñez Romeu b4b2bf5834
Run Tests on Branches / Detect Changes (push) Successful in 12s
Run Tests on Branches / Backend Tests (push) Has been skipped
Run Tests on Branches / Frontend Tests (push) Has been skipped
Run Tests on Branches / Frontend Mobile Tests (push) Has been skipped
Run Tests on Branches / Parapharmacy API Tests (push) Has been skipped
Run Tests on Branches / PIP Platform Tests (push) Has been skipped
fix(n8n): resolve duplicate key constraint on tag_entity during workflow import
n8n import:workflow --separate imports each JSON file independently. All 4
workflows shared the same tags (parapharmacy, scraper), so the second import
tried to INSERT duplicate tag names, hitting the unique index on
tag_entity.name.

- Remove tags field from all 4 workflow JSON files (cosmetic, re-add via UI)
- Add cleanup-db.js to wipe workflow data before import
- Add --overwrite flag to import command as safety net
- Add cleanup-db.js volume mount to n8n-init container
2026-07-17 14:50:35 +02:00

58 lines
2.3 KiB
Bash
Executable File

#!/bin/sh
# One-shot init: import + activate workflows (before n8n starts), then seed DB.
# Runs BEFORE n8n so it reads the activated state from DB on startup.
# Skips if already done (flag file in shared n8n_data volume).
set -e
FLAG="/home/node/.n8n/.workflows-imported"
if [ -f "$FLAG" ]; then
echo "[n8n-init] Already initialized, skipping."
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 --overwrite --input=/home/node/workflows/ 2>/dev/null
# ── 3. Activate all workflows via CLI ────────────────────────────────
echo "[n8n-init] Activating workflows..."
TMPFILE=$(mktemp)
n8n export:workflow --all --output="$TMPFILE" 2>/dev/null
node -e "
const fs = require('fs');
const wfs = JSON.parse(fs.readFileSync('$TMPFILE', 'utf8'));
wfs.forEach(w => console.log(w.id));
" | while read -r WF_ID; do
n8n publish:workflow --id="$WF_ID" 2>/dev/null
echo "[n8n-init] Activated $WF_ID"
done
rm -f "$TMPFILE"
echo "[n8n-init] Workflows activated."
# ── 4. Seed parapharmacy DB ──────────────────────────────────────────
echo "[n8n-init] Waiting for parapharmacy-api..."
until wget -qO /dev/null http://parapharmacy-api:3002/api/health 2>/dev/null; do
sleep 2
done
if [ -f /home/node/seed.json ]; then
echo "[n8n-init] Seeding parapharmacy products..."
SEED_RESP=$(wget -qO- --post-file=/home/node/seed.json \
--header="Content-Type: application/json" \
http://parapharmacy-api:3002/api/products/bulk 2>&1)
echo "[n8n-init] Seed result: $SEED_RESP"
else
echo "[n8n-init] WARNING: seed.json not found, skipping seed."
fi
# ── Done ─────────────────────────────────────────────────────────────
touch "$FLAG"
echo "[n8n-init] Initialization complete."