fix(server): bind machine_uid upsert ON CONFLICT to the partial index (WHERE machine_uid IS NOT NULL)
Bare ON CONFLICT (machine_uid) could not bind to migration 008's partial unique index, so no connect_machines row was persisted for any agent reporting a machine_uid. Confirmed live on 172.16.3.30 with a signed 0.3.0 test agent.
This commit is contained in:
@@ -34,8 +34,12 @@ ALTER TABLE connect_machines ADD COLUMN IF NOT EXISTS machine_uid TEXT;
|
|||||||
|
|
||||||
-- 2. Enforce one row per machine_uid, but ONLY for rows that actually have one.
|
-- 2. Enforce one row per machine_uid, but ONLY for rows that actually have one.
|
||||||
-- A partial UNIQUE index (WHERE machine_uid IS NOT NULL) lets unlimited legacy
|
-- A partial UNIQUE index (WHERE machine_uid IS NOT NULL) lets unlimited legacy
|
||||||
-- NULL rows coexist while making a non-null machine_uid a true dedup key — this
|
-- NULL rows coexist while making a non-null machine_uid a true dedup key. Because
|
||||||
-- is what upsert_machine's `ON CONFLICT (machine_uid)` arbiter binds to.
|
-- this index is PARTIAL, Postgres only binds an ON CONFLICT inference clause to it
|
||||||
|
-- when the clause REPEATS the same predicate: upsert_machine's arbiter must be
|
||||||
|
-- `ON CONFLICT (machine_uid) WHERE machine_uid IS NOT NULL` (a bare
|
||||||
|
-- `ON CONFLICT (machine_uid)` raises "no unique or exclusion constraint matching
|
||||||
|
-- the ON CONFLICT specification" at runtime and persists no row).
|
||||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_connect_machines_machine_uid
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_connect_machines_machine_uid
|
||||||
ON connect_machines (machine_uid)
|
ON connect_machines (machine_uid)
|
||||||
WHERE machine_uid IS NOT NULL;
|
WHERE machine_uid IS NOT NULL;
|
||||||
|
|||||||
@@ -166,7 +166,7 @@ pub async fn upsert_machine(
|
|||||||
r#"
|
r#"
|
||||||
INSERT INTO connect_machines (agent_id, hostname, is_persistent, status, last_seen, machine_uid)
|
INSERT INTO connect_machines (agent_id, hostname, is_persistent, status, last_seen, machine_uid)
|
||||||
VALUES ($1, $2, $3, 'online', NOW(), $4)
|
VALUES ($1, $2, $3, 'online', NOW(), $4)
|
||||||
ON CONFLICT (machine_uid) DO UPDATE SET
|
ON CONFLICT (machine_uid) WHERE machine_uid IS NOT NULL DO UPDATE SET
|
||||||
agent_id = EXCLUDED.agent_id,
|
agent_id = EXCLUDED.agent_id,
|
||||||
hostname = EXCLUDED.hostname,
|
hostname = EXCLUDED.hostname,
|
||||||
status = 'online',
|
status = 'online',
|
||||||
|
|||||||
Reference in New Issue
Block a user