-- name: UpsertDevice :exec
INSERT INTO devices (user_id, name, type, last_seen)
VALUES (?, ?, ?, ?)
ON CONFLICT (user_id, name) DO UPDATE SET
    type = excluded.type,
    last_seen = excluded.last_seen;

-- name: ListDevicesByUser :many
SELECT user_id, name, type, last_seen
FROM devices
WHERE user_id = ?
ORDER BY name;

-- name: DeleteStaleDevices :exec
DELETE FROM devices
WHERE last_seen < ?;

-- name: DeleteDevice :execrows
DELETE FROM devices
WHERE user_id = ? AND name = ?;
