fix(devices): device deletion detaches reading history instead of 500ing
Deleting any device whose stats rows still referenced it failed with 500: reading_history.device_id is a NO-ACTION FK by design (the rows are real captured reading stats — Users reads them), so a blind DELETE violated the constraint. Surfaced by the Android app's new logout deregister call, but the admin Devices page Deletes hit the same stone. Fix: DetachReadingHistoryFromDevice — NULL the device association on the user's history rows before the device DELETE (single handler pass). device_id is nullable; the stats survive, the device frees its identifier. sqlc regenerated.
This commit is contained in:
1 parent
313268a7ae
commit
d49a9e6ca2
4 files changed
+34
No files matched your search
@@ -2362,6 +2362,20 @@ func (q *Queries) DeleteUserSystemCollection(ctx context.Context, arg DeleteUser
|
||||
return err
|
||||
}
|
||||
|
||||
const DetachReadingHistoryFromDevice = `-- name: DetachReadingHistoryFromDevice :exec
|
||||
UPDATE reading_history SET device_id = NULL WHERE device_id = $1
|
||||
`
|
||||
|
||||
// Device deletion (deregister on logout): detach the device's reading
|
||||
// history instead of deleting it — these rows are real captured stats
|
||||
// (the Users section reads them); reading_history.device_id is NO ACTION
|
||||
// so a blind device delete 500s once any history anchors to the device.
|
||||
// device_id is nullable: NULL keeps the session, loses the association.
|
||||
func (q *Queries) DetachReadingHistoryFromDevice(ctx context.Context, deviceID pgtype.UUID) error {
|
||||
_, err := q.db.Exec(ctx, DetachReadingHistoryFromDevice, deviceID)
|
||||
return err
|
||||
}
|
||||
|
||||
const DeviceExistsForUser = `-- name: DeviceExistsForUser :one
|
||||
SELECT EXISTS(
|
||||
SELECT 1 FROM devices
|
||||
|
||||
Reference in new issue
Block a user