-- Migration 010: enforce unique emails in users
-- Matches runtime expectation for idx_users_email_unique.

BEGIN;

DO $$
BEGIN
  IF EXISTS (
    SELECT 1
    FROM users
    GROUP BY email
    HAVING COUNT(*) > 1
  ) THEN
    RAISE EXCEPTION 'Cannot create unique index idx_users_email_unique: duplicate emails exist in users';
  END IF;
END $$;

CREATE UNIQUE INDEX IF NOT EXISTS idx_users_email_unique ON users(email);

COMMIT;
