ReviewOS

also looking at this

loghqorg/loghq

Promote main to production: signed-out chrome and the subscriber tables

#17
Merged glennmichael123 wants to merge main into production
54 files +4252 -1353

Review threads live on the whole diff, not on one commit, so none are shown here - a thread's line means something in the branch's final form, and painting it into an intermediate step would put it on code it is not about.

database/migrations/0000000025-create-subscriber-tables.sqladded+58-0
Changes to database/migrations/0000000025-create-subscriber-tables.sql
@@ -0,0 +1,58 @@
1-- SQLITE DIALECT, WRITTEN BY HAND. See stacksjs/stacks#2346.
2--
3-- The two tables behind POST /api/email/subscribe. Without them the newsletter
4-- form on the homepage answered 500 in production: the route is registered
5-- (routes/api.ts) and points at the framework's SubscriberEmailAction, which
6-- opens with `Subscriber.where('email', email).first()` against a table this
7-- app had never created.
8--
9-- They were missing because `buddy generate:migrations` deliberately does not
10-- emit them. Framework-owned models stopped being generated once app/Models
11-- defines the schema (stacksjs/stacks#2220), and this app does not set
12-- `database.models.includeFrameworkDefaults`. So the generator leaves these 64
13-- framework tables alone by design, and an app that USES one of their actions
14-- has to bring the table itself. Hand-written rather than generated for that
15-- reason: running the generator here emits Postgres syntax and a duplicate of
16-- the read_key migration beside it.
17--
18-- Shapes follow storage/framework/defaults/app/Models/{Subscriber,SubscriberEmail}.ts
19-- exactly, because the framework action writes through those models.
20--
21-- The unique index on email is load-bearing, not decoration. The action's fast
22-- path checks for an existing subscriber and then creates one, so two requests
23-- for the same address can both pass the check; it catches the loser's unique
24-- violation and reports "already subscribed" (stacksjs/stx#1957). With no
25-- index there is no violation to catch and the address is stored twice.
26--
27-- `uuid` is what the footer's unsubscribe link carries
28-- (/api/email/unsubscribe?token=<uuid>), so it is unique here too.
29
30CREATE TABLE IF NOT EXISTS "subscribers" (
31 "id" INTEGER PRIMARY KEY,
32 "uuid" varchar(255),
33 "email" varchar(255) not null,
34 "status" varchar(255) not null default 'subscribed',
35 "source" varchar(100) default 'homepage',
36 "unsubscribed_at" timestamp,
37 "user_id" bigint REFERENCES "users"("id"),
38 "created_at" timestamp not null default CURRENT_TIMESTAMP,
39 "updated_at" timestamp
40);
41
42CREATE UNIQUE INDEX IF NOT EXISTS "subscribers_subscribers_email_unique" ON "subscribers" ("email");
43CREATE UNIQUE INDEX IF NOT EXISTS "subscribers_subscribers_uuid_unique" ON "subscribers" ("uuid");
44
45-- One row per signup event, kept even after the subscriber unsubscribes, which
46-- is why it is a separate table rather than a column on `subscribers`.
47CREATE TABLE IF NOT EXISTS "subscriber_emails" (
48 "id" INTEGER PRIMARY KEY,
49 "uuid" varchar(255),
50 "email" varchar(255) not null,
51 "source" varchar(100) default 'homepage',
52 "subscriber_id" bigint REFERENCES "subscribers"("id"),
53 "created_at" timestamp not null default CURRENT_TIMESTAMP,
54 "updated_at" timestamp
55);
56
57CREATE UNIQUE INDEX IF NOT EXISTS "subscriber_emails_subscriber_emails_uuid_unique" ON "subscriber_emails" ("uuid");
58CREATE INDEX IF NOT EXISTS "subscriber_emails_email" ON "subscriber_emails" ("email");