NGU-Web/server/src/migrations/031_blog.sql
Zaldimmar d374f9ac17 Add a blog: posts at /blog/:id and a Community page section
Admin → Blog → Posts (migration 031): our own posts with a
content-block body, cover image, excerpt, date, tags, and authors
shown as person tiles, either People records or a guest name and
photo. A post is a draft until published; Listed works as it does
elsewhere. Posts are on the QR page too.

Admin → Blog → Community section: the first section of the
Community page, with its heading and blurb from the admin, in two
halves, each pinned first and then newest up to its slots. Our posts
come from blog_posts; community posts are pinned link or video cards
(reusing featured cards and Fetch preview), then the newest items
from a list of RSS or Atom feeds. Feeds are read through the link
preview's guarded fetch and cached for 30 minutes, and a failing feed
keeps its last good items.

content_blocks and featured_cards are rebuilt to accept the new owner
kinds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-27 16:27:08 -05:00

254 lines
11 KiB
SQL

-- ═══════════════════════════════════════════════════════════════
-- 031 BLOG
--
-- Posts of our own, at /blog/<id>, and the Community page's blog
-- section, which shows two subsections:
--
-- Our posts pinned blog_posts first, then the newest
-- published, listed ones, up to internal_slots
-- Community posts pinned featured_cards (owner 'community_page')
-- first, then the newest items from the RSS or
-- Atom feeds in community_page_feeds, up to
-- external_slots
--
-- blog_posts
-- A draft until published (is_published defaults to 0, as for
-- people): a half-written post shouldn't go live by accident.
-- is_listed works as it does everywhere (027): unticked, the post
-- is left out of "newest" but its page works and it can be pinned.
-- The body is content_blocks with owner_kind 'blog_post', slot
-- 'body'. Authors are people (blog_post_authors), or a guest named
-- on the post itself, or both. tags is a comma-separated list, eg.
-- 'Reflection, Event recap'; it is only displayed, never joined on.
--
-- content_blocks gains 'blog_post' as an owner. A CHECK can't be
-- altered in place, so the table is rebuilt; the *_cleanup triggers
-- on other tables name it, hence legacy_alter_table around the
-- rename. Its own index and owner trigger are recreated below.
--
-- featured_cards gains 'community_page' as an owner, the same way.
-- Nothing names it but its own triggers, recreated below.
--
-- community_page is a singleton like front_page: one row, made
-- here, which the admin edits and can't delete.
--
-- PRAGMA user_version; -- reads 30 before this file
-- ═══════════════════════════════════════════════════════════════
CREATE TABLE blog_posts (
id TEXT PRIMARY KEY, -- slug: 'finding-unity-at-camp'
title TEXT NOT NULL,
excerpt TEXT, -- the card's text; else the body's start
cover_image TEXT, -- '/uploads/…', a filename, or a URL
published_on TEXT, -- 'YYYY-MM-DD'; newest first
tags TEXT, -- 'Reflection, Event recap'
guest_author TEXT, -- someone with no people row
guest_photo TEXT,
is_published INTEGER NOT NULL DEFAULT 0 CHECK (is_published IN (0, 1)),
is_listed INTEGER NOT NULL DEFAULT 1 CHECK (is_listed IN (0, 1)),
created_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
) STRICT;
CREATE INDEX blog_posts_newest_idx ON blog_posts (is_published, is_listed, published_on DESC);
CREATE TABLE blog_post_authors (
post_id TEXT NOT NULL REFERENCES blog_posts (id) ON DELETE CASCADE,
person_id TEXT NOT NULL REFERENCES people (id) ON DELETE CASCADE,
sort_order INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (post_id, person_id)
) STRICT;
CREATE INDEX blog_post_authors_person_idx ON blog_post_authors (person_id);
CREATE TABLE community_page (
id TEXT PRIMARY KEY CHECK (id = 'community'),
blog_title TEXT, -- null → 'Community Blog'
blog_blurb TEXT,
internal_title TEXT, -- null → 'From NGU'
internal_slots INTEGER NOT NULL DEFAULT 3 CHECK (internal_slots BETWEEN 0 AND 12),
external_title TEXT, -- null → 'From our community'
external_slots INTEGER NOT NULL DEFAULT 3 CHECK (external_slots BETWEEN 0 AND 12),
external_url TEXT, -- the Substack home, for its button
external_label TEXT, -- null → 'Read more on Substack'
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
) STRICT;
INSERT INTO community_page (id) VALUES ('community');
CREATE TABLE community_page_pinned_posts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
page_id TEXT NOT NULL REFERENCES community_page (id) ON DELETE CASCADE,
sort_order INTEGER NOT NULL DEFAULT 0,
post_id TEXT NOT NULL REFERENCES blog_posts (id) ON DELETE CASCADE,
UNIQUE (page_id, post_id)
) STRICT;
CREATE TABLE community_page_feeds (
id INTEGER PRIMARY KEY AUTOINCREMENT,
page_id TEXT NOT NULL REFERENCES community_page (id) ON DELETE CASCADE,
sort_order INTEGER NOT NULL DEFAULT 0,
url TEXT NOT NULL, -- 'https://ngu.substack.com/feed'
label TEXT -- shown on its items; else the feed's own title
) STRICT;
-- ── content_blocks: add 'blog_post' ───────────────────────────
CREATE TABLE content_blocks_new (
id INTEGER PRIMARY KEY AUTOINCREMENT,
owner_kind TEXT NOT NULL
CHECK (owner_kind IN ('organization', 'event', 'person', 'team', 'blog_post')),
owner_id TEXT NOT NULL,
slot TEXT NOT NULL DEFAULT 'body' CHECK (slot IN ('card', 'body')),
sort_order INTEGER NOT NULL DEFAULT 0,
type TEXT NOT NULL
CHECK (type IN ('heading', 'subheading', 'paragraph',
'list', 'links', 'quote', 'image', 'divider')),
text TEXT,
media TEXT,
href TEXT
) STRICT;
INSERT INTO content_blocks_new (id, owner_kind, owner_id, slot, sort_order, type, text, media, href)
SELECT id, owner_kind, owner_id, slot, sort_order, type, text, media, href
FROM content_blocks;
PRAGMA legacy_alter_table = ON;
DROP TABLE content_blocks;
ALTER TABLE content_blocks_new RENAME TO content_blocks;
PRAGMA legacy_alter_table = OFF;
CREATE INDEX content_blocks_owner_idx
ON content_blocks (owner_kind, owner_id, slot, sort_order);
-- ── featured_cards: add 'community_page' ──────────────────────
CREATE TABLE featured_cards_new (
id INTEGER PRIMARY KEY AUTOINCREMENT,
owner_kind TEXT NOT NULL CHECK (owner_kind IN ('front_page', 'community_page')),
owner_id TEXT NOT NULL,
sort_order INTEGER NOT NULL DEFAULT 0,
kind TEXT NOT NULL DEFAULT 'link'
CHECK (kind IN ('record', 'link', 'video')),
ref_kind TEXT CHECK (ref_kind IN ('event', 'organization', 'person', 'team', 'award')),
ref_id TEXT,
url TEXT,
eyebrow TEXT,
title TEXT,
blurb TEXT,
image TEXT,
CHECK ((ref_kind IS NULL) = (ref_id IS NULL)),
CONSTRAINT "A record card needs a record to point at."
CHECK (kind != 'record' OR ref_kind IS NOT NULL),
CONSTRAINT "A link or video card needs a URL."
CHECK (kind = 'record' OR url IS NOT NULL)
) STRICT;
INSERT INTO featured_cards_new
(id, owner_kind, owner_id, sort_order, kind, ref_kind, ref_id, url, eyebrow, title, blurb, image)
SELECT id, owner_kind, owner_id, sort_order, kind, ref_kind, ref_id, url, eyebrow, title, blurb, image
FROM featured_cards;
PRAGMA legacy_alter_table = ON;
DROP TABLE featured_cards;
ALTER TABLE featured_cards_new RENAME TO featured_cards;
PRAGMA legacy_alter_table = OFF;
CREATE INDEX featured_cards_owner_idx ON featured_cards (owner_kind, owner_id, sort_order);
-- ── Triggers last (see CLAUDE.md, Migrations) ──
CREATE TRIGGER content_blocks_owner_exists
BEFORE INSERT ON content_blocks
BEGIN
SELECT CASE
WHEN new.owner_kind = 'event'
AND NOT EXISTS (SELECT 1 FROM events WHERE id = new.owner_id)
THEN RAISE(ABORT, 'content_blocks: no such event')
WHEN new.owner_kind = 'organization'
AND NOT EXISTS (SELECT 1 FROM organizations WHERE id = new.owner_id)
THEN RAISE(ABORT, 'content_blocks: no such organization')
WHEN new.owner_kind = 'person'
AND NOT EXISTS (SELECT 1 FROM people WHERE id = new.owner_id)
THEN RAISE(ABORT, 'content_blocks: no such person')
WHEN new.owner_kind = 'team'
AND NOT EXISTS (SELECT 1 FROM teams WHERE id = new.owner_id)
THEN RAISE(ABORT, 'content_blocks: no such team')
WHEN new.owner_kind = 'blog_post'
AND NOT EXISTS (SELECT 1 FROM blog_posts WHERE id = new.owner_id)
THEN RAISE(ABORT, 'content_blocks: no such blog post')
END;
END;
CREATE TRIGGER featured_cards_ref_exists
BEFORE INSERT ON featured_cards
BEGIN
SELECT CASE
WHEN new.ref_kind = 'event'
AND NOT EXISTS (SELECT 1 FROM events WHERE id = new.ref_id)
THEN RAISE(ABORT, 'featured_cards: no such event')
WHEN new.ref_kind = 'organization'
AND NOT EXISTS (SELECT 1 FROM organizations WHERE id = new.ref_id)
THEN RAISE(ABORT, 'featured_cards: no such organization')
WHEN new.ref_kind = 'person'
AND NOT EXISTS (SELECT 1 FROM people WHERE id = new.ref_id)
THEN RAISE(ABORT, 'featured_cards: no such person')
WHEN new.ref_kind = 'team'
AND NOT EXISTS (SELECT 1 FROM teams WHERE id = new.ref_id)
THEN RAISE(ABORT, 'featured_cards: no such team')
WHEN new.ref_kind = 'award'
AND NOT EXISTS (SELECT 1 FROM awards WHERE id = new.ref_id)
THEN RAISE(ABORT, 'featured_cards: no such award')
END;
END;
CREATE TRIGGER featured_cards_ref_exists_update
BEFORE UPDATE OF ref_kind, ref_id ON featured_cards
BEGIN
SELECT CASE
WHEN new.ref_kind = 'event'
AND NOT EXISTS (SELECT 1 FROM events WHERE id = new.ref_id)
THEN RAISE(ABORT, 'featured_cards: no such event')
WHEN new.ref_kind = 'organization'
AND NOT EXISTS (SELECT 1 FROM organizations WHERE id = new.ref_id)
THEN RAISE(ABORT, 'featured_cards: no such organization')
WHEN new.ref_kind = 'person'
AND NOT EXISTS (SELECT 1 FROM people WHERE id = new.ref_id)
THEN RAISE(ABORT, 'featured_cards: no such person')
WHEN new.ref_kind = 'team'
AND NOT EXISTS (SELECT 1 FROM teams WHERE id = new.ref_id)
THEN RAISE(ABORT, 'featured_cards: no such team')
WHEN new.ref_kind = 'award'
AND NOT EXISTS (SELECT 1 FROM awards WHERE id = new.ref_id)
THEN RAISE(ABORT, 'featured_cards: no such award')
END;
END;
CREATE TRIGGER blog_posts_cleanup
AFTER DELETE ON blog_posts
BEGIN
DELETE FROM content_blocks WHERE owner_kind = 'blog_post' AND owner_id = old.id;
END;
CREATE TRIGGER blog_posts_touch
AFTER UPDATE ON blog_posts
FOR EACH ROW WHEN new.updated_at = old.updated_at
BEGIN
UPDATE blog_posts SET updated_at = datetime('now') WHERE id = new.id;
END;
CREATE TRIGGER community_page_touch
AFTER UPDATE ON community_page
FOR EACH ROW WHEN new.updated_at = old.updated_at
BEGIN
UPDATE community_page SET updated_at = datetime('now') WHERE id = new.id;
END;