v0.7.0.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package migrations
  2. import (
  3. "github.com/jmoiron/sqlx"
  4. "github.com/knadh/koanf"
  5. "github.com/knadh/stuffbin"
  6. )
  7. // V0_7_0 performs the DB migrations for v.0.7.0.
  8. func V0_7_0(db *sqlx.DB, fs stuffbin.FileSystem, ko *koanf.Koanf) error {
  9. // Check if the subscriber_status.blocklisted enum value exists. If not,
  10. // it has to be created (for the change from blacklisted -> blocklisted).
  11. var bl bool
  12. if err := db.Get(&bl, `SELECT 'blocklisted' = ANY(ENUM_RANGE(NULL::subscriber_status)::TEXT[])`); err != nil {
  13. return err
  14. }
  15. // If `blocklist` doesn't exist, add it to the subscriber_status enum,
  16. // and update existing statuses to this value. Unfortunately, it's not possible
  17. // to remove the enum value `blacklisted` (until PG10).
  18. if !bl {
  19. tx, err := db.Begin()
  20. if err != nil {
  21. return err
  22. }
  23. defer tx.Rollback()
  24. if _, err := tx.Exec(`
  25. -- Change the status column to text.
  26. ALTER TABLE subscribers ALTER COLUMN status TYPE TEXT;
  27. -- Change all statuses from 'blacklisted' to 'blocklisted'.
  28. UPDATE subscribers SET status='blocklisted' WHERE status='blacklisted';
  29. -- Remove the old enum.
  30. DROP TYPE subscriber_status CASCADE;
  31. -- Create new enum with the new values.
  32. CREATE TYPE subscriber_status AS ENUM ('enabled', 'disabled', 'blocklisted');
  33. -- Change the text status column to the new enum.
  34. ALTER TABLE subscribers ALTER COLUMN status TYPE subscriber_status
  35. USING (status::subscriber_status);
  36. `); err != nil {
  37. return err
  38. }
  39. if err := tx.Commit(); err != nil {
  40. return err
  41. }
  42. }
  43. _, err := db.Exec(`
  44. ALTER TABLE media DROP COLUMN IF EXISTS width,
  45. DROP COLUMN IF EXISTS height,
  46. ADD COLUMN IF NOT EXISTS provider TEXT NOT NULL DEFAULT '';
  47. -- 'blacklisted' to 'blocklisted' ENUM rename is not possible (until pg10),
  48. -- so just add the new value and ignore the old one.
  49. CREATE TABLE IF NOT EXISTS settings (
  50. key TEXT NOT NULL UNIQUE,
  51. value JSONB NOT NULL DEFAULT '{}',
  52. updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
  53. );
  54. CREATE INDEX IF NOT EXISTS idx_settings_key ON settings(key);
  55. -- Insert default settings if the table is empty.
  56. INSERT INTO settings (key, value) SELECT k, v::JSONB FROM (VALUES
  57. ('app.root_url', '"http://localhost:9000"'),
  58. ('app.favicon_url', '""'),
  59. ('app.from_email', '"listmonk <noreply@listmonk.yoursite.com>"'),
  60. ('app.logo_url', '"http://localhost:9000/public/static/logo.png"'),
  61. ('app.concurrency', '10'),
  62. ('app.message_rate', '10'),
  63. ('app.batch_size', '1000'),
  64. ('app.max_send_errors', '1000'),
  65. ('app.notify_emails', '["admin1@mysite.com", "admin2@mysite.com"]'),
  66. ('privacy.unsubscribe_header', 'true'),
  67. ('privacy.allow_blocklist', 'true'),
  68. ('privacy.allow_export', 'true'),
  69. ('privacy.allow_wipe', 'true'),
  70. ('privacy.exportable', '["profile", "subscriptions", "campaign_views", "link_clicks"]'),
  71. ('upload.provider', '"filesystem"'),
  72. ('upload.filesystem.upload_path', '"uploads"'),
  73. ('upload.filesystem.upload_uri', '"/uploads"'),
  74. ('upload.s3.aws_access_key_id', '""'),
  75. ('upload.s3.aws_secret_access_key', '""'),
  76. ('upload.s3.aws_default_region', '"ap-south-1"'),
  77. ('upload.s3.bucket', '""'),
  78. ('upload.s3.bucket_domain', '""'),
  79. ('upload.s3.bucket_path', '"/"'),
  80. ('upload.s3.bucket_type', '"public"'),
  81. ('upload.s3.expiry', '"14d"'),
  82. ('smtp',
  83. '[{"enabled":true, "host":"smtp.yoursite.com","port":25,"auth_protocol":"cram","username":"username","password":"password","hello_hostname":"","max_conns":10,"idle_timeout":"15s","wait_timeout":"5s","max_msg_retries":2,"tls_enabled":true,"tls_skip_verify":false,"email_headers":[]},
  84. {"enabled":false, "host":"smtp2.yoursite.com","port":587,"auth_protocol":"plain","username":"username","password":"password","hello_hostname":"","max_conns":10,"idle_timeout":"15s","wait_timeout":"5s","max_msg_retries":2,"tls_enabled":false,"tls_skip_verify":false,"email_headers":[]}]'),
  85. ('messengers', '[]')) vals(k, v) WHERE NOT EXISTS(SELECT * FROM settings LIMIT 1);
  86. `)
  87. if err != nil {
  88. return err
  89. }
  90. // `provider` in the media table is a new field. If there's provider config available
  91. // and no provider value exists in the media table, set it.
  92. prov := ko.String("upload.provider")
  93. if prov != "" {
  94. if _, err := db.Exec(`UPDATE media SET provider=$1 WHERE provider=''`, prov); err != nil {
  95. return err
  96. }
  97. }
  98. return nil
  99. }