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.
This commit is contained in:
2026-04-14 07:39:58 -07:00
parent 2e6d1a67dd
commit 9a6d67fdc5

View File

@@ -11,11 +11,13 @@ CREATE TABLE tech_sessions (
opened_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), opened_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
last_activity TIMESTAMPTZ NOT NULL DEFAULT NOW(), last_activity TIMESTAMPTZ NOT NULL DEFAULT NOW(),
closed_at TIMESTAMPTZ, closed_at TIMESTAMPTZ,
status VARCHAR(20) NOT NULL DEFAULT 'active', status VARCHAR(20) NOT NULL DEFAULT 'active'
CONSTRAINT unique_active_session UNIQUE (tech_id, agent_id, status)
WHERE status = '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 -- Index for finding sessions by technician
CREATE INDEX idx_tech_sessions_tech ON tech_sessions(tech_id); CREATE INDEX idx_tech_sessions_tech ON tech_sessions(tech_id);