| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
CREATE TABLE IF NOT EXISTS lists ( |
| 16 |
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), |
| 17 |
scope TEXT NOT NULL CHECK (scope IN ('platform', 'project', 'repo', 'creator')), |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
scope_id UUID, |
| 22 |
kind TEXT NOT NULL CHECK (kind IN ( |
| 23 |
'content', 'devlog', 'patches', 'releases', |
| 24 |
'issues', 'announce', 'marketing' |
| 25 |
)), |
| 26 |
title TEXT NOT NULL, |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
required BOOLEAN NOT NULL DEFAULT FALSE, |
| 33 |
owner_id UUID REFERENCES users(id) ON DELETE CASCADE, |
| 34 |
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), |
| 35 |
CONSTRAINT chk_lists_scope_id CHECK ((scope = 'platform') = (scope_id IS NULL)) |
| 36 |
); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
CREATE UNIQUE INDEX IF NOT EXISTS idx_lists_scoped |
| 41 |
ON lists (scope, scope_id, kind) WHERE scope_id IS NOT NULL; |
| 42 |
CREATE UNIQUE INDEX IF NOT EXISTS idx_lists_platform |
| 43 |
ON lists (kind) WHERE scope_id IS NULL; |
| 44 |
|
| 45 |
|
| 46 |
CREATE TABLE IF NOT EXISTS list_subscriptions ( |
| 47 |
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), |
| 48 |
list_id UUID NOT NULL REFERENCES lists(id) ON DELETE CASCADE, |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
user_id UUID REFERENCES users(id) ON DELETE CASCADE, |
| 53 |
email TEXT, |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
state TEXT NOT NULL CHECK (state IN ( |
| 61 |
'pending', 'confirmed', 'imported', 'unsubscribed', 'bounced' |
| 62 |
)), |
| 63 |
source TEXT NOT NULL CHECK (source IN ( |
| 64 |
'landing_form', 'project_page', 'checkout', |
| 65 |
'import', 'admin', 'api' |
| 66 |
)), |
| 67 |
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), |
| 68 |
confirmed_at TIMESTAMPTZ, |
| 69 |
unsubscribed_at TIMESTAMPTZ, |
| 70 |
CONSTRAINT chk_subscription_identity CHECK ((user_id IS NULL) <> (email IS NULL)) |
| 71 |
); |
| 72 |
|
| 73 |
CREATE UNIQUE INDEX IF NOT EXISTS idx_list_subscriptions_user |
| 74 |
ON list_subscriptions (list_id, user_id) WHERE user_id IS NOT NULL; |
| 75 |
CREATE UNIQUE INDEX IF NOT EXISTS idx_list_subscriptions_email |
| 76 |
ON list_subscriptions (list_id, email) WHERE email IS NOT NULL; |
| 77 |
|
| 78 |
CREATE INDEX IF NOT EXISTS idx_list_subscriptions_sendable |
| 79 |
ON list_subscriptions (list_id, state); |
| 80 |
|
| 81 |
|
| 82 |
CREATE TABLE IF NOT EXISTS consent_events ( |
| 83 |
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), |
| 84 |
subscription_id UUID NOT NULL REFERENCES list_subscriptions(id) ON DELETE CASCADE, |
| 85 |
event TEXT NOT NULL CHECK (event IN ( |
| 86 |
'opt_in', 'confirm', 'opt_out', 'bounce', |
| 87 |
'complaint', 'admin_removal', 'import' |
| 88 |
)), |
| 89 |
at TIMESTAMPTZ NOT NULL DEFAULT NOW(), |
| 90 |
ip INET, |
| 91 |
user_agent TEXT, |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
evidence TEXT |
| 97 |
); |
| 98 |
|
| 99 |
CREATE INDEX IF NOT EXISTS idx_consent_events_subscription |
| 100 |
ON consent_events (subscription_id, at DESC); |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
CREATE OR REPLACE FUNCTION consent_events_no_update() RETURNS TRIGGER AS $$ |
| 109 |
BEGIN |
| 110 |
RAISE EXCEPTION 'consent_events is append-only; record a new event instead'; |
| 111 |
END; |
| 112 |
$$ LANGUAGE plpgsql; |
| 113 |
|
| 114 |
DROP TRIGGER IF EXISTS trg_consent_events_no_update ON consent_events; |
| 115 |
CREATE TRIGGER trg_consent_events_no_update |
| 116 |
BEFORE UPDATE ON consent_events |
| 117 |
FOR EACH ROW EXECUTE FUNCTION consent_events_no_update(); |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
INSERT INTO lists (scope, scope_id, kind, title, required, owner_id) |
| 127 |
SELECT 'project', ml.project_id, ml.list_type, ml.name, FALSE, p.user_id |
| 128 |
FROM mailing_lists ml |
| 129 |
JOIN projects p ON p.id = ml.project_id |
| 130 |
ON CONFLICT DO NOTHING; |
| 131 |
|
| 132 |
|
| 133 |
INSERT INTO lists (scope, scope_id, kind, title, required, owner_id) |
| 134 |
VALUES ('platform', NULL, 'marketing', 'Makenotwork updates', FALSE, NULL) |
| 135 |
ON CONFLICT DO NOTHING; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
INSERT INTO list_subscriptions (list_id, user_id, email, state, source, created_at) |
| 140 |
SELECT l.id, |
| 141 |
mls.user_id, |
| 142 |
CASE WHEN mls.user_id IS NULL THEN LOWER(mls.email) END, |
| 143 |
'imported', |
| 144 |
'import', |
| 145 |
mls.subscribed_at |
| 146 |
FROM mailing_list_subscribers mls |
| 147 |
JOIN mailing_lists ml ON ml.id = mls.list_id |
| 148 |
JOIN lists l ON l.scope = 'project' AND l.scope_id = ml.project_id AND l.kind = ml.list_type |
| 149 |
ON CONFLICT DO NOTHING; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
INSERT INTO list_subscriptions (list_id, email, state, source, created_at, unsubscribed_at) |
| 155 |
SELECT l.id, |
| 156 |
LOWER(es.email), |
| 157 |
CASE WHEN es.unsubscribed_at IS NULL THEN 'imported' ELSE 'unsubscribed' END, |
| 158 |
'import', |
| 159 |
es.created_at, |
| 160 |
es.unsubscribed_at |
| 161 |
FROM email_signups es |
| 162 |
CROSS JOIN lists l |
| 163 |
WHERE l.scope = 'platform' AND l.kind = 'marketing' |
| 164 |
ON CONFLICT DO NOTHING; |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
INSERT INTO consent_events (subscription_id, event, at, evidence) |
| 172 |
SELECT ls.id, |
| 173 |
'import', |
| 174 |
ls.created_at, |
| 175 |
CASE l.scope |
| 176 |
WHEN 'platform' THEN |
| 177 |
'Backfilled from email_signups (landing form, single opt-in, ' |
| 178 |
|| 'no record of the copy shown at signup).' |
| 179 |
ELSE |
| 180 |
'Backfilled from mailing_list_subscribers. The source table kept ' |
| 181 |
|| 'no provenance, so a page subscribe and a creator CSV import ' |
| 182 |
|| 'are indistinguishable in this row.' |
| 183 |
END |
| 184 |
FROM list_subscriptions ls |
| 185 |
JOIN lists l ON l.id = ls.list_id |
| 186 |
WHERE ls.source = 'import' |
| 187 |
AND NOT EXISTS ( |
| 188 |
SELECT 1 FROM consent_events ce |
| 189 |
WHERE ce.subscription_id = ls.id AND ce.event = 'import' |
| 190 |
); |
| 191 |
|