-- ─────────────────────────────────────────────────────────────────────────────
-- 060 — Correct 059: local backups go to R2, not to local disk
--
-- 059 created a `posix` repository on the assumption that
-- `test/database/backups/...` was a directory on the developer's machine. It is
-- not — it is an R2 KEY PREFIX. Both environments write to the same bucket and
-- differ only in the leading segment:
--
--   test/database/backups/databases/<timestamp>_<database>.sql
--   production/database/backups/databases/<timestamp>_<database>.sql
--
-- So a developer machine backs up to R2 exactly as the server does; local disk
-- is only a staging area for the bytes on their way up, and the staging file is
-- deleted once the upload is confirmed.
--
-- Corrected forward rather than by editing 059, because 059 has already been
-- applied and its checksum recorded. Rewriting an applied migration is exactly
-- the silent drift the ledger exists to catch — so the ledger gets to keep
-- doing its job, and the history stays truthful about what was believed when.
--
-- CONSEQUENCE WORTH STATING: dev dumps are now charged against the SAME 1 GB
-- ceiling as production ones, because they are in the same bucket. A working
-- dev database dumps to roughly 95 MB, so retention on the dev policy is a real
-- decision rather than a formality — three retained dev dumps is close to a
-- third of the entire budget.
-- ─────────────────────────────────────────────────────────────────────────────

BEGIN;

-- Deactivated, not deleted. Backup runs may already reference it, and a
-- dangling repositoryId on a historical run is worse than an inactive row that
-- explains itself.
UPDATE platform.backup_repositories
   SET "isActive" = false,
       "name"     = 'Local disk (superseded by 060 — dev backups go to R2)',
       "updatedOn" = CURRENT_TIMESTAMP
 WHERE "code" = 'local-disk';

-- The dev policy stays, but it now writes to R2 under the `test/` prefix.
-- Retention drops from 3 to 2: at ~95 MB a dump, 3 would be ~285 MB of a 1 GB
-- budget shared with production.
UPDATE platform.backup_policies
   SET "name"           = 'Local pg_dump → R2 (dev)',
       "retentionCount" = 2,
       "updatedOn"      = CURRENT_TIMESTAMP
 WHERE "id" = 'bkp_local_dump';

-- Record what the R2 repository actually holds, now that two kinds of object
-- live under it. Purely descriptive; the prefixes are resolved at runtime from
-- BACKUP_ENV_PREFIX so one row stays correct in both environments.
UPDATE platform.backup_repositories
   SET "pathPrefix" = '{env}/database/backups',
       "updatedOn"  = CURRENT_TIMESTAMP
 WHERE "code" = 'r2-primary';

COMMIT;
