-- archive_backup_tables.sql — moved 26 backup/snapshot tables out of pgz_sport
-- Author: cc1@rinet.one  Date: 2026-05-05
-- Re-runnable (idempotent — only moves tables that still match pattern in pgz_sport)
--
-- Snapshot dump kept at: _audit/db_snapshots/backup_tables_20260505_085057.sql.gz (56 MB)
--
-- Patterns matched: _backup_  _premerge_  _pre_  _dedup_  _deprecated_  _garbage_
--                   _pre_godisnjak  _pre_b_switch
--
-- After this:
--   pgz_sport canonical tables: 86 (was 112)
--   pgz_sport_archive holds:    26
--   ~97k rows of stale data archived (no DROP — recoverable via ALTER ... SET SCHEMA back)
--
-- Rollback any single table:
--   ALTER TABLE pgz_sport_archive.<name> SET SCHEMA pgz_sport;
--
-- Rollback all:
--   DO $$ DECLARE r record; BEGIN
--     FOR r IN SELECT relname FROM pg_stat_user_tables WHERE schemaname='pgz_sport_archive' LOOP
--       EXECUTE format('ALTER TABLE pgz_sport_archive.%I SET SCHEMA pgz_sport', r.relname);
--     END LOOP; END $$;
--
-- Permanent drop (after confidence period):
--   DROP SCHEMA pgz_sport_archive CASCADE;

BEGIN;
CREATE SCHEMA IF NOT EXISTS pgz_sport_archive;

DO $$
DECLARE r record;
BEGIN
  FOR r IN
    SELECT relname FROM pg_stat_user_tables
    WHERE schemaname='pgz_sport'
      AND (relname ~ '_backup_|_premerge_|_pre_|_dedup_|_deprecated_|_garbage_|_pre_godisnjak|_pre_b_switch')
    ORDER BY relname
  LOOP
    EXECUTE format('ALTER TABLE pgz_sport.%I SET SCHEMA pgz_sport_archive', r.relname);
    RAISE NOTICE 'moved %', r.relname;
  END LOOP;
END $$;

COMMIT;
