From a39c9831c5c51c2ac6a7382b7477d681442c38ed Mon Sep 17 00:00:00 2001 From: ZGaetano Date: Thu, 21 May 2026 00:14:01 -0400 Subject: [PATCH] cluster: dedupe rows + enforce unique hostname index Migration 004 wrapped table creation in IF NOT EXISTS, so deploys with a pre-existing cluster_nodes table never picked up the inline UNIQUE constraint and accumulated duplicate hostnames on every container restart. This migration purges older duplicates and adds the unique index idempotently so the ON CONFLICT (hostname) upsert finally works. --- .../007-cluster-dedupe-hostname.sql | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 services/mam-api/src/db/migrations/007-cluster-dedupe-hostname.sql diff --git a/services/mam-api/src/db/migrations/007-cluster-dedupe-hostname.sql b/services/mam-api/src/db/migrations/007-cluster-dedupe-hostname.sql new file mode 100644 index 0000000..4f13252 --- /dev/null +++ b/services/mam-api/src/db/migrations/007-cluster-dedupe-hostname.sql @@ -0,0 +1,20 @@ +-- 007 — De-duplicate cluster_nodes by hostname and enforce uniqueness. +-- +-- Migration 004 created the table with `CREATE TABLE IF NOT EXISTS` and an +-- inline UNIQUE constraint; on deploys where the table predated 004 the +-- constraint was never applied, which let the same hostname accumulate +-- multiple rows (one per container restart in some setups). +-- +-- This migration: +-- 1. Deletes older duplicates keeping only the most-recently-seen row +-- per hostname. +-- 2. Adds a UNIQUE INDEX on (hostname) which is idempotent and satisfies +-- the ON CONFLICT (hostname) upsert in routes/cluster.js. + +DELETE FROM cluster_nodes a +USING cluster_nodes b +WHERE a.hostname = b.hostname + AND a.last_seen < b.last_seen; + +CREATE UNIQUE INDEX IF NOT EXISTS cluster_nodes_hostname_uniq + ON cluster_nodes (hostname);