Added first draft of versions to schema

This commit is contained in:
Jef Roosens 2021-12-23 16:28:43 +01:00
parent b83ea9796b
commit fb38a1c1bf
Signed by: Jef Roosens
GPG key ID: 955C0660072F691F
4 changed files with 107 additions and 27 deletions

View file

@ -1,7 +1,8 @@
-- This file should undo anything in `up.sql`
drop trigger insert_enforce_post_titles on posts;
drop trigger update_enforce_post_titles on posts;
drop function enforce_post_titles;
drop trigger insert_enforce_version_titles on versions;
drop trigger update_enforce_version_titles on versions;
drop function enforce_version_titles;
drop table versions cascade;
drop table posts cascade;
drop table sections cascade;

View file

@ -1,4 +1,3 @@
-- Your SQL goes here
create table sections (
id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
@ -11,48 +10,80 @@ create table sections (
-- Wether to show the section in the default list on the homepage
is_default boolean NOT NULL DEFAULT false,
-- Wether the posts should contain titles or not
has_titles boolean NOT NULL DEFAULT true
has_titles boolean NOT NULL DEFAULT true,
-- Wether posts in this section should be shown publicly
is_private boolean NOT NULL DEFAULT false
);
create table posts (
id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
section_id uuid NOT NULL REFERENCES sections(id) ON DELETE CASCADE,
-- Title of the post
-- Wether this is NULL or not is enforced using the enforce_post_titles trigger
title varchar(255),
-- Post date, defaults to today
publish_date date NOT NULL DEFAULT now(),
-- Content of the post
content text NOT NULL
-- Posts shouldn't get deleted when we delete a section, as they're the
-- most valuable part of a blog
section_id uuid NOT NULL REFERENCES sections(id) ON DELETE SET NULL,
-- Wether a post should be private
is_private boolean NOT NULL DEFAULT false,
-- Wether the post is archived
is_archived boolean NOT NULL DEFAULT false
);
create function enforce_post_titles() returns trigger as $enforce_post_titles$
create table versions (
id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
-- A version should be deleted when its referenced post is deleted
post_id uuid NOT NULL REFERENCES posts(id) ON DELETE CASCADE,
-- Title of the post. Wether this is NULL or not is enforced using the
-- enforce_post_titles trigger.
title varchar(255),
-- Publish date
publish_date date,
-- Content of the post, in Markdown
content text NOT NULL DEFAULT '',
-- Wether the version is still a draft
is_draft boolean NOT NULL default true,
-- This check allows draft posts to be created without having to enter a
-- publish date, but forces them to have one if they're not a draft.
CHECK (is_draft OR publish_date IS NOT NULL)
);
create function enforce_version_titles() returns trigger as $$
begin
-- Draft versions shouldn't be evaluated.
if new.is_draft then
return new;
end if;
-- Check for a wrongfully null title
if new.title is null and exists (
select 1 from sections where id = new.section_id and has_titles
select 1 from posts
inner join sections on posts.section_id = sections.id
where posts.id = new.post_id and sections.has_titles
) then
raise exception 'Expected a post title, but got null.';
end if;
if new.title is not null and exists (
select 1 from sections where id = new.section_id and not has_titles
select 1 from posts
inner join sections on posts.section_id = sections.id
where posts.id = new.post_id and not sections.has_titles
) then
raise exception 'Expected an empty post title, but got a value.';
end if;
return new;
end;
$enforce_post_titles$ language plpgsql;
$$ language plpgsql;
create trigger insert_enforce_post_titles
before insert on posts
create trigger insert_enforce_version_titles
before insert on versions
for each row
execute function enforce_post_titles();
execute function enforce_version_titles();
create trigger update_enforce_post_titles
before update of title on posts
create trigger update_enforce_version_titles
before update of title on versions
for each row
when (old.title is distinct from new.title)
execute function enforce_post_titles();
execute function enforce_version_titles();