From 7077cf55febb668e73055ed30991e4ad16b88cf7 Mon Sep 17 00:00:00 2001 From: Finn Date: Fri, 22 Nov 2024 22:54:56 -0800 Subject: [PATCH] Don't wipe out code slot names on restart --- db/lock_code_slots.sql.go | 27 ++++++++++++++++++++++++--- db/queries/lock_code_slots.sql | 5 ++++- httpserver/lock.go | 2 +- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/db/lock_code_slots.sql.go b/db/lock_code_slots.sql.go index 222e603..2a39028 100644 --- a/db/lock_code_slots.sql.go +++ b/db/lock_code_slots.sql.go @@ -129,8 +129,31 @@ func (q *Queries) GetLockCodesByCode(ctx context.Context, code string) ([]LockCo return items, nil } +const updateCodeSlot = `-- name: UpdateCodeSlot :exec +UPDATE lock_code_slots SET code = ?, enabled = ?, name = ? WHERE lock = ? AND slot = ? +` + +type UpdateCodeSlotParams struct { + Code string + Enabled bool + Name string + Lock int64 + Slot int64 +} + +func (q *Queries) UpdateCodeSlot(ctx context.Context, arg UpdateCodeSlotParams) error { + _, err := q.db.ExecContext(ctx, updateCodeSlot, + arg.Code, + arg.Enabled, + arg.Name, + arg.Lock, + arg.Slot, + ) + return err +} + const upsertCodeSlot = `-- name: UpsertCodeSlot :exec -INSERT INTO lock_code_slots (lock, slot, code, enabled, name) VALUES (?, ?, ?, ?, ?) ON CONFLICT (lock, slot) DO UPDATE SET code=excluded.code, enabled=excluded.enabled, name=excluded.name +INSERT INTO lock_code_slots (lock, slot, code, enabled, name) VALUES (?, ?, ?, ?, "") ON CONFLICT (lock, slot) DO UPDATE SET code=excluded.code, enabled=excluded.enabled ` type UpsertCodeSlotParams struct { @@ -138,7 +161,6 @@ type UpsertCodeSlotParams struct { Slot int64 Code string Enabled bool - Name string } func (q *Queries) UpsertCodeSlot(ctx context.Context, arg UpsertCodeSlotParams) error { @@ -147,7 +169,6 @@ func (q *Queries) UpsertCodeSlot(ctx context.Context, arg UpsertCodeSlotParams) arg.Slot, arg.Code, arg.Enabled, - arg.Name, ) return err } diff --git a/db/queries/lock_code_slots.sql b/db/queries/lock_code_slots.sql index f0e0236..9fbf809 100644 --- a/db/queries/lock_code_slots.sql +++ b/db/queries/lock_code_slots.sql @@ -1,5 +1,8 @@ -- name: UpsertCodeSlot :exec -INSERT INTO lock_code_slots (lock, slot, code, enabled, name) VALUES (?, ?, ?, ?, ?) ON CONFLICT (lock, slot) DO UPDATE SET code=excluded.code, enabled=excluded.enabled, name=excluded.name; +INSERT INTO lock_code_slots (lock, slot, code, enabled, name) VALUES (?, ?, ?, ?, "") ON CONFLICT (lock, slot) DO UPDATE SET code=excluded.code, enabled=excluded.enabled; + +-- name: UpdateCodeSlot :exec +UPDATE lock_code_slots SET code = ?, enabled = ?, name = ? WHERE lock = ? AND slot = ?; -- name: GetLockCodeBySlot :one SELECT * FROM lock_code_slots WHERE lock = ? AND slot = ?; diff --git a/httpserver/lock.go b/httpserver/lock.go index 8e5b8bb..3c3f45f 100644 --- a/httpserver/lock.go +++ b/httpserver/lock.go @@ -145,7 +145,7 @@ func lockCodeEditHandler(c echo.Context) error { return fmt.Errorf("error pushing code to lock %s (ZWaveDeviceID=%d ID=%d): %v", lock.Name, lock.ZwaveDeviceID, lock.ID, err) } - err = queries.UpsertCodeSlot(ctx, db.UpsertCodeSlotParams{ + err = queries.UpdateCodeSlot(ctx, db.UpdateCodeSlotParams{ Lock: lockID, Slot: slot, Code: newCode,