hugo-cms/sql/schema/1_init.up.sql

116 lines
3.4 KiB
MySQL
Raw Normal View History

2025-01-27 03:23:54 +00:00
CREATE TYPE post_state AS ENUM (
'draft',
'published'
);
2025-02-18 10:26:24 +00:00
CREATE TYPE post_format AS ENUM (
'markdown'
);
2025-01-27 04:45:53 +00:00
CREATE TYPE target_role AS ENUM (
'production'
);
CREATE TYPE target_type AS ENUM (
'netlify'
);
2025-02-16 00:43:22 +00:00
CREATE TYPE page_name_provenance AS ENUM (
'user',
'title',
'date'
);
CREATE TYPE page_role AS ENUM (
'index'
);
2025-01-31 22:42:32 +00:00
CREATE TABLE users (
id BIGSERIAL NOT NULL PRIMARY KEY,
email TEXT NOT NULL UNIQUE,
password TEXT NOT NULL
);
2025-01-27 03:23:54 +00:00
2025-01-31 22:42:32 +00:00
CREATE TABLE sites (
id BIGSERIAL NOT NULL PRIMARY KEY,
owner_user_id BIGINT NOT NULL,
name TEXT NOT NULL UNIQUE,
title TEXT NOT NULL,
theme TEXT NOT NULL,
props JSON NOT NULL,
FOREIGN KEY (owner_user_id) REFERENCES users (id)
);
2025-02-16 00:43:22 +00:00
-- Post role is used to describe a specific kind of post, such as a link.
-- When set, it specifies the layout to use for the page
CREATE TABLE post_types (
2025-02-16 00:43:22 +00:00
id BIGSERIAL NOT NULL PRIMARY KEY,
site_id BIGINT NOT NULL,
layout_name TEXT NOT NULL,
FOREIGN KEY (site_id) REFERENCES sites (id) ON DELETE CASCADE
);
2025-01-31 22:42:32 +00:00
CREATE TABLE posts (
2025-02-18 10:26:24 +00:00
id BIGSERIAL NOT NULL PRIMARY KEY,
site_id BIGINT NOT NULL,
2025-02-16 00:43:22 +00:00
title TEXT,
post_type_id BIGINT,
2025-02-18 10:26:24 +00:00
format post_format NOT NULL DEFAULT 'markdown',
body TEXT NOT NULL,
state post_state NOT NULL,
props JSON NOT NULL,
2025-02-16 00:43:22 +00:00
publish_date TIMESTAMP WITH TIME ZONE,
2025-02-18 10:26:24 +00:00
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL,
2025-02-16 00:43:22 +00:00
FOREIGN KEY (post_type_id) REFERENCES post_types (id) ON DELETE CASCADE,
2025-02-16 00:43:22 +00:00
FOREIGN KEY (site_id) REFERENCES sites (id) ON DELETE CASCADE
);
CREATE TABLE bundles (
id BIGSERIAL NOT NULL PRIMARY KEY,
site_id BIGINT NOT NULL,
name TEXT NOT NULL,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL,
2025-01-27 03:23:54 +00:00
2025-01-31 22:42:32 +00:00
FOREIGN KEY (site_id) REFERENCES sites (id) ON DELETE CASCADE
2025-01-27 04:45:53 +00:00
);
2025-02-16 00:43:22 +00:00
CREATE TABLE pages (
id BIGSERIAL NOT NULL PRIMARY KEY,
site_id BIGINT NOT NULL,
bundle_id BIGINT NOT NULL,
name TEXT NOT NULL,
name_provenance page_name_provenance NOT NULL,
2025-02-18 10:26:24 +00:00
format post_format NOT NULL DEFAULT 'markdown',
2025-02-16 00:43:22 +00:00
title TEXT,
post_type_id BIGINT,
2025-02-16 00:43:22 +00:00
body TEXT NOT NULL,
state post_state NOT NULL,
props JSON NOT NULL,
role page_role,
2025-02-16 00:43:22 +00:00
publish_date TIMESTAMP WITH TIME ZONE,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL,
UNIQUE (bundle_id, name),
FOREIGN KEY (site_id) REFERENCES sites (id) ON DELETE CASCADE,
FOREIGN KEY (post_type_id) REFERENCES post_types (id) ON DELETE CASCADE,
2025-02-16 00:43:22 +00:00
FOREIGN KEY (bundle_id) REFERENCES sites (id) ON DELETE CASCADE
);
CREATE UNIQUE INDEX page_bundle_id_role ON pages (bundle_id, role) WHERE (role is NOT null);
2025-02-16 00:43:22 +00:00
2025-01-31 22:42:32 +00:00
CREATE TABLE publish_targets (
2025-01-27 04:45:53 +00:00
id BIGSERIAL NOT NULL PRIMARY KEY,
site_id BIGINT NOT NULL,
role target_role NOT NULL,
target_type target_type NOT NULL,
url TEXT NOT NULL,
target_ref TEXT NOT NULL,
2025-01-31 23:56:59 +00:00
FOREIGN KEY (site_id) REFERENCES sites (id) ON DELETE CASCADE,
UNIQUE (site_id, role)
2025-01-27 03:23:54 +00:00
);