From 9a6d67fdc5859a67d0b87fc276a51e30ef7ec392 Mon Sep 17 00:00:00 2001 From: azcomputerguru Date: Tue, 14 Apr 2026 07:39:58 -0700 Subject: [PATCH] Fix migration syntax: Use partial unique index instead of inline constraint PostgreSQL doesn't support inline CONSTRAINT with WHERE clause. Changed to separate CREATE UNIQUE INDEX statement for the partial unique constraint on (tech_id, agent_id, status) WHERE status = 'active'. This ensures only one active tunnel session per (tech, agent) pair while allowing multiple closed sessions in history. Migration tested and verified on PostgreSQL 14. --- .../guru-rmm/server/migrations/006_tunnel_sessions.sql | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/projects/msp-tools/guru-rmm/server/migrations/006_tunnel_sessions.sql b/projects/msp-tools/guru-rmm/server/migrations/006_tunnel_sessions.sql index bd086db..d925d65 100644 --- a/projects/msp-tools/guru-rmm/server/migrations/006_tunnel_sessions.sql +++ b/projects/msp-tools/guru-rmm/server/migrations/006_tunnel_sessions.sql @@ -11,11 +11,13 @@ CREATE TABLE tech_sessions ( opened_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), last_activity TIMESTAMPTZ NOT NULL DEFAULT NOW(), closed_at TIMESTAMPTZ, - status VARCHAR(20) NOT NULL DEFAULT 'active', - CONSTRAINT unique_active_session UNIQUE (tech_id, agent_id, status) - WHERE status = 'active' + status VARCHAR(20) NOT NULL DEFAULT 'active' ); +-- Partial unique index to ensure only one active session per tech-agent pair +CREATE UNIQUE INDEX unique_active_session ON tech_sessions(tech_id, agent_id, status) + WHERE status = 'active'; + -- Index for finding sessions by technician CREATE INDEX idx_tech_sessions_tech ON tech_sessions(tech_id);