WITH fallback_owner AS (
  SELECT id
  FROM users
  WHERE user_role = 'admin'
  ORDER BY created_at ASC, id ASC
  LIMIT 1
)
UPDATE users
SET
  user_role = 'owner',
  is_admin = true,
  updated_at = now()
WHERE id IN (SELECT id FROM fallback_owner)
  AND NOT EXISTS (
    SELECT 1
    FROM users
    WHERE user_role = 'owner'
  );
