245_user_cleanup.sql 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. -- Additive only: existing users, credentials, balances and usage history remain intact.
  2. CREATE TABLE IF NOT EXISTS user_cleanup_guards (
  3. user_id BIGINT PRIMARY KEY REFERENCES users(id) ON DELETE CASCADE,
  4. is_system BOOLEAN NOT NULL DEFAULT FALSE,
  5. is_protected BOOLEAN NOT NULL DEFAULT FALSE,
  6. updated_by BIGINT,
  7. updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
  8. );
  9. -- A preview is an immutable, operator-bound confirmation scope and idempotency key.
  10. -- Completed results remain available for reconciliation after a connection failure.
  11. CREATE TABLE IF NOT EXISTS user_cleanup_previews (
  12. id UUID PRIMARY KEY,
  13. actor_user_id BIGINT NOT NULL,
  14. candidate_ids BIGINT[] NOT NULL,
  15. candidate_snapshot JSONB NOT NULL,
  16. created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  17. expires_at TIMESTAMPTZ NOT NULL DEFAULT (NOW() + INTERVAL '10 minutes'),
  18. completed_at TIMESTAMPTZ,
  19. result JSONB
  20. );
  21. CREATE INDEX IF NOT EXISTS idx_user_cleanup_previews_actor_created
  22. ON user_cleanup_previews (actor_user_id, created_at DESC);
  23. -- Login bindings must be released just like the existing DeleteUser workflow,
  24. -- otherwise canonical identity uniqueness prevents the same person registering again.
  25. -- Preserve the original rows for internal recovery; no HTTP API exposes this archive.
  26. CREATE TABLE IF NOT EXISTS user_cleanup_identity_archives (
  27. preview_id UUID NOT NULL REFERENCES user_cleanup_previews(id),
  28. user_id BIGINT NOT NULL,
  29. identities JSONB NOT NULL DEFAULT '[]'::jsonb,
  30. channels JSONB NOT NULL DEFAULT '[]'::jsonb,
  31. adoption_decisions JSONB NOT NULL DEFAULT '[]'::jsonb,
  32. created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  33. PRIMARY KEY (preview_id, user_id)
  34. );
  35. COMMENT ON TABLE user_cleanup_guards IS 'Explicit system/protected users excluded from inactivity cleanup';
  36. COMMENT ON TABLE user_cleanup_previews IS 'Admin-confirmed bounded cleanup scope; soft deletion and audit commit atomically';
  37. COMMENT ON TABLE user_cleanup_identity_archives IS 'Internal-only identity archive; never return through user/admin result APIs';