ALTER TABLE users
  ADD COLUMN IF NOT EXISTS username TEXT;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1
    FROM pg_constraint
    WHERE conname = 'uq_users_username'
  ) THEN
    ALTER TABLE users
      ADD CONSTRAINT uq_users_username UNIQUE (username);
  END IF;
END $$;

CREATE INDEX IF NOT EXISTS idx_users_username ON users(username);
