85 lines
3.6 KiB
SQL
85 lines
3.6 KiB
SQL
-- ═══════════════════════════════════════════════════════════════
|
|
-- 008 v_timeline
|
|
--
|
|
-- 007's tables, indexes and all eight triggers landed; its view did
|
|
-- not. This file creates it, and nothing else.
|
|
--
|
|
-- The definition below is byte-identical to the one at the foot of
|
|
-- 007. That is deliberate: a fresh database built from 007 and an
|
|
-- existing one upgraded through 008 must end up with the same view,
|
|
-- or a restore from backup six months from now produces a subtly
|
|
-- different site. Leave 007 exactly as it is.
|
|
--
|
|
-- No BEGIN...END anywhere in this file — two plain statements and a
|
|
-- pragma — so a runner that splits on semicolons treats it the same
|
|
-- way one that doesn't would. 007's triggers are the only place in
|
|
-- the schema where that distinction bites, and they are already in.
|
|
--
|
|
-- Safe to run twice: DROP VIEW IF EXISTS makes it idempotent, and
|
|
-- dropping a view touches no data.
|
|
--
|
|
-- PRAGMA user_version; -- reads 7 before this file
|
|
-- ═══════════════════════════════════════════════════════════════
|
|
|
|
DROP VIEW IF EXISTS v_timeline;
|
|
|
|
CREATE VIEW v_timeline AS
|
|
SELECT
|
|
t.id,
|
|
t.kind,
|
|
t.ref_kind,
|
|
t.ref_id,
|
|
t.precision,
|
|
t.is_featured,
|
|
t.is_published,
|
|
t.sort_order,
|
|
|
|
COALESCE(t.occurred_on, e.starts_on, pa.awarded_on) AS effective_date,
|
|
|
|
COALESCE(
|
|
t.title,
|
|
e.title,
|
|
o.name,
|
|
aw.name,
|
|
p.display_name,
|
|
tm.name
|
|
) AS effective_title,
|
|
|
|
COALESCE(t.blurb, e.tagline, o.tagline, aw.description, p.tagline, tm.tagline)
|
|
AS effective_blurb,
|
|
t.meta,
|
|
t.link_url,
|
|
|
|
-- Filename only. The directory is the frontend's business.
|
|
COALESCE(e.event_logo, e.org_logo, o.logo, aw.logo, p.photo, tm.logo)
|
|
AS effective_logo,
|
|
|
|
o.kind AS org_kind,
|
|
tm.org_id AS team_org_id,
|
|
tm.name AS team_name,
|
|
|
|
-- Whether the referenced record is itself visible. An entry must not
|
|
-- outlive the thing it points at being unpublished — a draft event
|
|
-- would otherwise leak its title and date onto a public page. Null
|
|
-- for a standalone milestone, which answers to nothing but its own
|
|
-- is_published.
|
|
CASE t.ref_kind
|
|
WHEN 'event' THEN e.is_published
|
|
WHEN 'organization' THEN o.is_published
|
|
WHEN 'person' THEN p.is_published
|
|
WHEN 'team' THEN tm.is_published
|
|
ELSE NULL
|
|
END AS ref_is_published,
|
|
t.occurred_on,
|
|
t.title AS title_override
|
|
FROM timeline_entries t
|
|
LEFT JOIN events e ON t.ref_kind = 'event' AND e.id = t.ref_id
|
|
LEFT JOIN organizations o ON t.ref_kind = 'organization' AND o.id = t.ref_id
|
|
LEFT JOIN awards aw ON t.ref_kind = 'award' AND aw.id = t.ref_id
|
|
LEFT JOIN people p ON t.ref_kind = 'person' AND p.id = t.ref_id
|
|
LEFT JOIN teams tm ON t.ref_kind = 'team' AND tm.id = t.ref_id
|
|
LEFT JOIN person_awards pa ON t.ref_kind = 'award' AND pa.award_id = t.ref_id
|
|
AND pa.id = (SELECT MIN(id) FROM person_awards
|
|
WHERE award_id = t.ref_id);
|
|
|
|
PRAGMA user_version = 8;
|