-- ═══════════════════════════════════════════════════════════════ -- 030 FEATURED CARDS -- -- A row of cards that point somewhere: the front page's new -- 'featured' band first, and the Community page's blog posts next. -- One table for both, owned polymorphically like links and -- content_blocks, so the card's shape and the code that draws it -- are written once. owner_kind only allows 'front_page' today; -- adding an owner is a rebuild of this table, which nothing -- references, so it's a cheap one. -- -- A card is one of three kinds: -- -- record points at an event, organization, person, team or -- award. Title, image and link come from that record at -- read time, so renaming the event renames the card. -- Any of title, blurb and image filled in here overrides. -- link any URL, on the site or off it. Title and image are -- the card's own (the editor can fetch them from the -- page's preview tags). -- video a YouTube, Vimeo or Facebook link, or a video file. -- Drawn as a thumbnail that swaps in the player when -- pressed. image overrides the thumbnail. -- -- ref_kind + ref_id is checked by the triggers below, the same way -- timeline_entries' is, and a deleted record takes its cards with -- it. An unpublished record's card is left out when read. -- -- Also: 'featured' joins front_page_sections.section. A CHECK can't -- be altered in place, so that table is rebuilt. No view or trigger -- names it, and nothing references it. -- -- PRAGMA user_version; -- reads 29 before this file -- ═══════════════════════════════════════════════════════════════ CREATE TABLE front_page_sections_new ( id INTEGER PRIMARY KEY AUTOINCREMENT, page_id TEXT NOT NULL REFERENCES front_page (id) ON DELETE CASCADE, sort_order INTEGER NOT NULL DEFAULT 0, section TEXT NOT NULL CHECK (section IN ('countdown', 'retreats', 'calendar', 'stats', 'timeline', 'connect', 'featured')), title TEXT, blurb TEXT, is_hidden INTEGER NOT NULL DEFAULT 0 CHECK (is_hidden IN (0, 1)), UNIQUE (page_id, section) ) STRICT; INSERT INTO front_page_sections_new (id, page_id, sort_order, section, title, blurb, is_hidden) SELECT id, page_id, sort_order, section, title, blurb, is_hidden FROM front_page_sections; DROP TABLE front_page_sections; ALTER TABLE front_page_sections_new RENAME TO front_page_sections; CREATE TABLE featured_cards ( id INTEGER PRIMARY KEY AUTOINCREMENT, owner_kind TEXT NOT NULL CHECK (owner_kind IN ('front_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, -- 'Article', 'Watch' title TEXT, blurb TEXT, image TEXT, -- '/uploads/…', a filename, or a URL CHECK ((ref_kind IS NULL) = (ref_id IS NULL)), -- A record card needs its record; the other two need somewhere to -- go. Named as sentences: the admin shows a sentence-shaped -- constraint name as the error (wrapDbErrors in admin-crud.js). 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; CREATE INDEX featured_cards_owner_idx ON featured_cards (owner_kind, owner_id, sort_order); -- ── Triggers last (see CLAUDE.md, Migrations) ── 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; -- A deleted record takes the cards that point at it. Separate -- triggers rather than additions to the existing *_cleanup ones, so -- this file doesn't have to drop and restate those. CREATE TRIGGER events_featured_cleanup AFTER DELETE ON events BEGIN DELETE FROM featured_cards WHERE ref_kind = 'event' AND ref_id = old.id; END; CREATE TRIGGER organizations_featured_cleanup AFTER DELETE ON organizations BEGIN DELETE FROM featured_cards WHERE ref_kind = 'organization' AND ref_id = old.id; END; CREATE TRIGGER people_featured_cleanup AFTER DELETE ON people BEGIN DELETE FROM featured_cards WHERE ref_kind = 'person' AND ref_id = old.id; END; CREATE TRIGGER teams_featured_cleanup AFTER DELETE ON teams BEGIN DELETE FROM featured_cards WHERE ref_kind = 'team' AND ref_id = old.id; END; CREATE TRIGGER awards_featured_cleanup AFTER DELETE ON awards BEGIN DELETE FROM featured_cards WHERE ref_kind = 'award' AND ref_id = old.id; END;