From 5f44ad66a5bd6aaa8832fc2f1745a85f5595f575 Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Wed, 6 May 2026 08:52:33 -0700 Subject: [PATCH] =?UTF-8?q?feat(db):=20migration=20017=20=E2=80=94=20make?= =?UTF-8?q?=20campaign=5Fid=20nullable=20on=20posts=20for=20manual=20oppor?= =?UTF-8?q?tunity=20posts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../017_posts_campaign_nullable.sql | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 app/db/migrations/017_posts_campaign_nullable.sql diff --git a/app/db/migrations/017_posts_campaign_nullable.sql b/app/db/migrations/017_posts_campaign_nullable.sql new file mode 100644 index 0000000..1d569d7 --- /dev/null +++ b/app/db/migrations/017_posts_campaign_nullable.sql @@ -0,0 +1,28 @@ +-- Make campaign_id nullable on posts to support manual opportunity posts +-- that don't belong to a campaign. SQLite requires a full table rebuild +-- to drop a NOT NULL constraint. +CREATE TABLE posts_new ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + campaign_id INTEGER REFERENCES campaigns(id), + variant_id INTEGER REFERENCES campaign_variants(id), + opportunity_id INTEGER REFERENCES opportunities(id) ON DELETE SET NULL, + platform TEXT NOT NULL DEFAULT 'reddit', + target TEXT NOT NULL, + status TEXT NOT NULL DEFAULT 'pending', + url TEXT, + error_msg TEXT, + screenshot_path TEXT, + triggered_by TEXT DEFAULT 'scheduler', + posted_at TEXT NOT NULL DEFAULT (datetime('now')) +); + +INSERT INTO posts_new SELECT id, campaign_id, variant_id, opportunity_id, platform, target, status, url, error_msg, screenshot_path, triggered_by, posted_at FROM posts; + +DROP TABLE posts; +ALTER TABLE posts_new RENAME TO posts; + +CREATE INDEX IF NOT EXISTS idx_posts_campaign ON posts(campaign_id); +CREATE INDEX IF NOT EXISTS idx_posts_opportunity ON posts(opportunity_id); +CREATE INDEX IF NOT EXISTS idx_posts_target ON posts(target); +CREATE INDEX IF NOT EXISTS idx_posts_status ON posts(status); +CREATE INDEX IF NOT EXISTS idx_posts_posted_at ON posts(posted_at);