-- SPDX-License-Identifier: Apache-2.0
--
-- 0004_downgrade.sql — manual downgrade from v0.8.0 layout to v0.7.1 layout.
--
-- This file is documentation only. It is NOT applied by `run_migrations`.
-- A v0.8.0 → v0.7.1 downgrade is supported only when the data dir has
-- exactly one tenant (the reserved `default` tenant) and no other tenants
-- have ever been created. If multiple tenants exist, extract the target
-- tenant first via `solo tenants backup <id> --to <path>` (P6), wipe the
-- data dir, install the v0.7.1 binary, then restore from the backup using
-- whatever per-version restore path v0.7.1 supports — there is no direct
-- multi-tenant → single-tenant collapse.
--
-- WARNING: stop all Solo processes BEFORE running the steps below. A
-- running daemon holding open file handles will block the moves on
-- Windows (and silently race on POSIX).
--
-- Steps:
--
--   1. Stop all Solo processes; confirm with `solo doctor` (should fail
--      to connect to the daemon socket / port).
--   2. cd <data_dir>
--   3. Verify exactly one tenant is active in the registry:
--        sqlcipher tenants_index.db -cmd "PRAGMA key = 'x''<32-byte-hex>''';" \
--                                   -cmd "SELECT count(*) FROM tenants WHERE status = 'active';"
--      The result MUST be 1. If 0 → the data dir is empty post-migration,
--      nothing to downgrade. If 2+ → abort, use `solo tenants backup`
--      first.
--   4. Verify the one tenant is the reserved `default`:
--        sqlcipher tenants_index.db -cmd "PRAGMA key = 'x''<32-byte-hex>''';" \
--                                   -cmd "SELECT tenant_id FROM tenants WHERE status = 'active';"
--      Result MUST be `default`. Any other id → abort.
--   5. mv tenants/default.db                       solo.db
--   6. mv tenants/default.db-wal                   solo.db-wal           (if exists)
--   7. mv tenants/default.db-shm                   solo.db-shm           (if exists)
--   8. mv tenants/default.hnsw_episodes.hnsw.data  hnsw_episodes.hnsw.data   (if exists)
--   9. mv tenants/default.hnsw_episodes.hnsw.graph hnsw_episodes.hnsw.graph  (if exists)
--   (also move _bak / _tmp pairs if present — keep the snapshot
--   convention identical between v0.7.1 and v0.8.0 file naming)
--  10. rm tenants_index.db
--  11. rm tenants_index.db-wal                                              (if exists)
--  12. rm tenants_index.db-shm                                              (if exists)
--  13. rmdir tenants/                                                       (must be empty)
--  14. Start the v0.7.1 binary against the data dir. `solo doctor` should
--      report `schema_version : 3` and a single working memory store.
--
-- After downgrade, attempting to boot a v0.8.0 binary against this data
-- dir runs migration 0004 again (the v0.7.1 → v0.8.0 mass-data-move
-- helper), which is idempotent. Round-trips are safe.
