2023-04-08 12:09:10 +08:00
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/jmoiron/sqlx"
|
2023-04-10 15:15:25 +08:00
|
|
|
"github.com/knadh/koanf/v2"
|
2023-04-08 12:09:10 +08:00
|
|
|
"github.com/knadh/stuffbin"
|
|
|
|
)
|
|
|
|
|
|
|
|
// V2_5_0 performs the DB migrations.
|
|
|
|
func V2_5_0(db *sqlx.DB, fs stuffbin.FileSystem, ko *koanf.Koanf) error {
|
|
|
|
// Insert new preference settings.
|
|
|
|
if _, err := db.Exec(`
|
|
|
|
INSERT INTO settings (key, value) VALUES
|
2023-05-18 19:25:59 +08:00
|
|
|
('upload.extensions', '["jpg","jpeg","png","gif","svg","*"]'),
|
2023-04-11 14:03:13 +08:00
|
|
|
('app.enable_public_archive_rss_content', 'false'),
|
|
|
|
('bounce.actions', '{"soft": {"count": 2, "action": "none"}, "hard": {"count": 2, "action": "blocklist"}, "complaint" : {"count": 2, "action": "blocklist"}}')
|
2023-04-08 12:09:10 +08:00
|
|
|
ON CONFLICT DO NOTHING;
|
|
|
|
`); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-18 19:25:59 +08:00
|
|
|
if _, err := db.Exec(`
|
|
|
|
DELETE FROM settings WHERE key IN ('bounce.count', 'bounce.action');
|
|
|
|
|
|
|
|
-- Add the content_type column.
|
|
|
|
ALTER TABLE media ADD COLUMN IF NOT EXISTS content_type TEXT NOT NULL DEFAULT 'application/octet-stream';
|
|
|
|
|
|
|
|
-- Fill the content type column for existing files (which would only be images at this point).
|
|
|
|
UPDATE media SET content_type = CASE
|
|
|
|
WHEN LOWER(SUBSTRING(filename FROM '.([^.]+)$')) = 'svg' THEN 'image/svg+xml'
|
|
|
|
ELSE 'image/' || LOWER(SUBSTRING(filename FROM '.([^.]+)$'))
|
|
|
|
END;
|
|
|
|
|
|
|
|
`); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := db.Exec(`
|
|
|
|
CREATE TABLE IF NOT EXISTS campaign_media (
|
|
|
|
campaign_id INTEGER REFERENCES campaigns(id) ON DELETE CASCADE ON UPDATE CASCADE,
|
|
|
|
|
|
|
|
-- Media items may be deleted, so media_id is nullable
|
|
|
|
-- and a copy of the original name is maintained here.
|
|
|
|
media_id INTEGER NULL REFERENCES media(id) ON DELETE SET NULL ON UPDATE CASCADE,
|
|
|
|
|
|
|
|
filename TEXT NOT NULL DEFAULT ''
|
|
|
|
);
|
|
|
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_camp_media_id ON campaign_media (campaign_id, media_id);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_camp_media_camp_id ON campaign_media(campaign_id);
|
|
|
|
`); err != nil {
|
2023-04-11 14:03:13 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-08 12:09:10 +08:00
|
|
|
return nil
|
|
|
|
}
|