104_migrate_notify_emails_to_struct.sql 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. -- Migrate notification email lists from old []string format to new []NotifyEmailEntry format
  2. -- Old: ["a@x.com", "b@x.com"]
  3. -- New: [{"email":"a@x.com","disabled":false,"verified":true}, ...]
  4. -- Existing emails are marked as verified=false (unverified), disabled=false (enabled)
  5. -- 1. User balance notification emails
  6. UPDATE users
  7. SET balance_notify_extra_emails = (
  8. SELECT COALESCE(
  9. jsonb_agg(jsonb_build_object('email', elem::text, 'disabled', false, 'verified', false)),
  10. '[]'::jsonb
  11. )::text
  12. FROM jsonb_array_elements_text(balance_notify_extra_emails::jsonb) AS elem
  13. )
  14. WHERE balance_notify_extra_emails IS NOT NULL
  15. AND balance_notify_extra_emails <> '[]'
  16. AND balance_notify_extra_emails <> ''
  17. AND (balance_notify_extra_emails::jsonb -> 0) IS NOT NULL
  18. AND jsonb_typeof(balance_notify_extra_emails::jsonb -> 0) = 'string';
  19. -- 2. Admin account quota notification emails
  20. UPDATE settings
  21. SET value = (
  22. SELECT COALESCE(
  23. jsonb_agg(jsonb_build_object('email', elem::text, 'disabled', false, 'verified', false)),
  24. '[]'::jsonb
  25. )::text
  26. FROM jsonb_array_elements_text(value::jsonb) AS elem
  27. )
  28. WHERE key = 'account_quota_notify_emails'
  29. AND value IS NOT NULL
  30. AND value <> '[]'
  31. AND value <> ''
  32. AND (value::jsonb -> 0) IS NOT NULL
  33. AND jsonb_typeof(value::jsonb -> 0) = 'string';