-- One-shot / re-runnable local cleanup for descrybe-v2. -- Keeps: Platform Demo, A1 Slovenija, Guide Shopify, Guide WooCommerce -- (matched case-insensitively by name). -- Keeps plans: Free/Starter/Growth/Business/Enterprise + A1 + Platform Demo -- + Legacy + archived Basic/Professional + Merkur trial (code/catalog). -- Deletes orphan users that only belonged to removed companies. -- Usage: -- psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -f scripts/cleanup-dev-tenants.sql -- Never prints connection secrets. BEGIN; CREATE TEMP TABLE keep_companies ON COMMIT DROP AS SELECT id, name FROM companies WHERE lower(name) = 'platform demo' OR lower(name) = 'a1 slovenija' OR lower(name) LIKE '%shopify%' OR lower(name) LIKE '%woocommerce%' OR lower(name) ~ '(^|[^a-z0-9])woo([^a-z0-9]|$)' OR ( lower(name) ~ '(^|[^a-z0-9])a1([^a-z0-9]|$)' AND lower(name) NOT LIKE 'vars-page%' ) OR ( lower(name) LIKE '%demo%' AND lower(name) NOT LIKE '%auth smoke%' ); DO $$ DECLARE n int; BEGIN SELECT COUNT(*) INTO n FROM keep_companies; IF n < 4 THEN RAISE EXCEPTION 'keep_companies too small (%) - aborting cleanup', n; END IF; END $$; CREATE TEMP TABLE delete_companies ON COMMIT DROP AS SELECT c.id, c.name FROM companies c WHERE c.id NOT IN (SELECT id FROM keep_companies); CREATE TEMP TABLE cleanup_stats ( key text PRIMARY KEY, value bigint NOT NULL ) ON COMMIT DROP; INSERT INTO cleanup_stats(key, value) VALUES ('companies_kept', (SELECT COUNT(*) FROM keep_companies)), ('companies_deleted', (SELECT COUNT(*) FROM delete_companies)), ('plans_before', (SELECT COUNT(*) FROM plans)), ('users_before', (SELECT COUNT(*) FROM users)), ('sessions_before', (SELECT COUNT(*) FROM sessions)); -- Opaque session store (token/data/expiry) — wipe so deleted-user cookies die. DELETE FROM sessions; DELETE FROM companies WHERE id IN (SELECT id FROM delete_companies); CREATE TEMP TABLE keep_users ON COMMIT DROP AS SELECT u.id, u.email FROM users u WHERE lower(u.email) IN ( 'demo@descrybe.local', 'demo@descrybe.test', 'a1-primary@descrybe.local', 'guide-shopify@descrybe.local', 'guide-woocommerce@descrybe.local' ) OR EXISTS ( SELECT 1 FROM memberships m WHERE m.user_id = u.id ); DELETE FROM users WHERE id NOT IN (SELECT id FROM keep_users); CREATE TEMP TABLE keep_plans ON COMMIT DROP AS SELECT p.id, p.name FROM plans p WHERE lower(p.name) IN ( 'free', 'starter', 'growth', 'business', 'enterprise', 'a1', 'platform demo', 'legacy', 'basic', 'professional', 'merkur trial' ) OR EXISTS ( SELECT 1 FROM company_plans cp WHERE cp.plan_id = p.id ); DELETE FROM plans WHERE id NOT IN (SELECT id FROM keep_plans); INSERT INTO cleanup_stats(key, value) VALUES ('plans_after', (SELECT COUNT(*) FROM plans)), ('companies_after', (SELECT COUNT(*) FROM companies)), ('users_after', (SELECT COUNT(*) FROM users)), ('sessions_after', (SELECT COUNT(*) FROM sessions)); SELECT key, value FROM cleanup_stats ORDER BY key; SELECT id, name AS kept_company FROM keep_companies ORDER BY lower(name); SELECT id, name AS kept_plan FROM plans ORDER BY id; SELECT id, email AS kept_user FROM users ORDER BY email; COMMIT;