-- apps/web/src/server/db/migrations/0091_users_pii_consistency.sql
-- Backfill BOTH drift directions, then couple PII columns. missing_* = 0 (null-out is safe).
UPDATE users SET phone_index = NULL, phone_hint = NULL WHERE phone IS NULL;
UPDATE users SET email_index = NULL WHERE email IS NULL;

ALTER TABLE users ADD CONSTRAINT users_phone_cols_consistent CHECK (
  (phone IS NULL AND phone_index IS NULL AND phone_hint IS NULL)
  OR (phone IS NOT NULL AND phone_index IS NOT NULL AND phone_hint IS NOT NULL)
);
ALTER TABLE users ADD CONSTRAINT users_email_cols_consistent CHECK (
  (email IS NULL AND email_index IS NULL)
  OR (email IS NOT NULL AND email_index IS NOT NULL)
);