-- ═══════════════════════════════════════════════════════════════ -- 079 TAB BLOCKS -- -- A blog post can be split into tabs: a 'tab' block (its text the -- tab's title) starts a tab holding the blocks after it, up to the -- next one; the blocks before the first are the intro above the -- tabs. The post's contents follow the open tab (BlogPost.tsx). -- content_blocks.type's CHECK can't change in place, so the table is -- rebuilt, then its index and owner trigger are made again. -- ═══════════════════════════════════════════════════════════════ 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', 'page_section', 'newsletter')), owner_id TEXT NOT NULL, slot TEXT NOT NULL DEFAULT 'body' CHECK (slot IN ('card', 'body', 'both')), sort_order INTEGER NOT NULL DEFAULT 0, type TEXT NOT NULL CHECK (type IN ('heading', 'subheading', 'paragraph', 'list', 'links', 'quote', 'image', 'divider', 'video', 'button', 'callout', 'schedule', 'table', 'cards', 'people', 'tab')), text TEXT, media TEXT, href TEXT, alt TEXT, cite TEXT, team_id TEXT REFERENCES teams (id) ON DELETE SET NULL, captions TEXT ) STRICT; INSERT INTO content_blocks_new (id, owner_kind, owner_id, slot, sort_order, type, text, media, href, alt, cite, team_id, captions) SELECT id, owner_kind, owner_id, slot, sort_order, type, text, media, href, alt, cite, team_id, captions FROM content_blocks; -- The block items and the owners' cleanup triggers name -- content_blocks; legacy mode leaves them alone through the rename. 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); 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') WHEN new.owner_kind = 'page_section' AND NOT EXISTS (SELECT 1 FROM page_sections WHERE CAST(id AS TEXT) = new.owner_id) THEN RAISE(ABORT, 'content_blocks: no such page section') WHEN new.owner_kind = 'newsletter' AND NOT EXISTS (SELECT 1 FROM newsletters WHERE CAST(id AS TEXT) = new.owner_id) THEN RAISE(ABORT, 'content_blocks: no such newsletter') END; END;