ALTER TABLE users
  ADD COLUMN IF NOT EXISTS user_role TEXT NOT NULL DEFAULT 'member';

UPDATE users
SET user_role = CASE
  WHEN is_admin THEN 'admin'
  ELSE 'member'
END
WHERE user_role NOT IN ('owner', 'admin', 'member');

WITH first_user AS (
  SELECT id
  FROM users
  ORDER BY created_at ASC, id ASC
  LIMIT 1
)
UPDATE users
SET user_role = 'owner'
WHERE id IN (SELECT id FROM first_user)
  AND is_admin = true;

ALTER TABLE users
  DROP CONSTRAINT IF EXISTS users_user_role_check;

ALTER TABLE users
  ADD CONSTRAINT users_user_role_check
  CHECK (user_role IN ('owner', 'admin', 'member'));
