-- Minstrel's own backup: its settings and its runs. -- name: GetBackupSettings :one SELECT * FROM backup_settings WHERE id = true; -- name: SaveBackupSettings :exec UPDATE backup_settings SET enabled = $1, destination = $2, folder = $3, endpoint = $4, bucket = $5, access_key = $6, secret_key = $7, run_hour = $8, upload_limit_kib = $9, updated_at = now() WHERE id = true; -- name: StartBackupRun :one INSERT INTO backup_runs (trigger) VALUES ($1) RETURNING id, started_at; -- name: FinishBackupRun :exec UPDATE backup_runs SET status = $2, finished_at = now(), location = $3, bytes = $4, error = $5 WHERE id = $1; -- name: ListBackupRuns :many SELECT id, trigger, status, started_at, finished_at, location, bytes, error FROM backup_runs ORDER BY started_at DESC LIMIT $1; -- name: BackupRunTimes :one -- The times the verdict turns on. A run that never happened is the epoch, -- so the row always exists. failing_since is the first failure after the last -- success, and the epoch when the latest finished run succeeded. SELECT COALESCE((SELECT max(started_at) FROM backup_runs WHERE status = 'succeeded'), 'epoch'::timestamptz)::timestamptz AS last_success, COALESCE((SELECT max(started_at) FROM backup_runs), 'epoch'::timestamptz)::timestamptz AS last_attempt, COALESCE((SELECT min(started_at) FROM backup_runs WHERE status = 'failed' AND started_at > COALESCE((SELECT max(started_at) FROM backup_runs WHERE status = 'succeeded'), 'epoch'::timestamptz)), 'epoch'::timestamptz)::timestamptz AS failing_since; -- name: FailUnfinishedBackupRuns :execrows UPDATE backup_runs SET status = 'failed', finished_at = now(), error = 'The server stopped while the backup was running.' WHERE status = 'running'; -- name: PruneBackupRuns :execrows DELETE FROM backup_runs WHERE status <> 'running' AND started_at < $1;