🎉 Add scheduled (cron based) tasks subsystem.

This commit is contained in:
Andrey Antukh 2020-01-25 17:23:21 +01:00
parent 9bcb91ceae
commit b005c3905f
12 changed files with 400 additions and 139 deletions

View file

@ -3,7 +3,7 @@ CREATE EXTENSION IF NOT EXISTS "pgcrypto";
-- Modified At
CREATE OR REPLACE FUNCTION update_modified_at()
CREATE FUNCTION update_modified_at()
RETURNS TRIGGER AS $updt$
BEGIN
NEW.modified_at := clock_timestamp();

View file

@ -29,7 +29,7 @@ CREATE INDEX users__is_demo
AND is_demo IS true;
--- Table used for register all used emails by the user
CREATE TABLE IF NOT EXISTS user_emails (
CREATE TABLE user_emails (
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
@ -46,7 +46,7 @@ CREATE INDEX user_emails__user_id__idx
--- Table for user key value attributes
CREATE TABLE IF NOT EXISTS user_attrs (
CREATE TABLE user_attrs (
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
@ -60,7 +60,7 @@ CREATE TABLE IF NOT EXISTS user_attrs (
--- Table for store verification tokens
CREATE TABLE IF NOT EXISTS tokens (
CREATE TABLE tokens (
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
token text NOT NULL,
@ -72,7 +72,7 @@ CREATE TABLE IF NOT EXISTS tokens (
--- Table for store user sessions.
CREATE TABLE IF NOT EXISTS sessions (
CREATE TABLE sessions (
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),

View file

@ -1,6 +1,6 @@
-- Tables
CREATE TABLE IF NOT EXISTS projects (
CREATE TABLE projects (
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
@ -11,7 +11,10 @@ CREATE TABLE IF NOT EXISTS projects (
name text NOT NULL
);
CREATE TABLE IF NOT EXISTS project_users (
CREATE INDEX projects__user_id__idx
ON projects(user_id);
CREATE TABLE project_users (
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
project_id uuid NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
@ -23,7 +26,13 @@ CREATE TABLE IF NOT EXISTS project_users (
PRIMARY KEY (user_id, project_id)
);
CREATE TABLE IF NOT EXISTS project_files (
CREATE INDEX project_users__user_id__idx
ON project_users(user_id);
CREATE INDEX project_users__project_id__idx
ON project_users(project_id);
CREATE TABLE project_files (
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
project_id uuid NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
@ -35,7 +44,26 @@ CREATE TABLE IF NOT EXISTS project_files (
deleted_at timestamptz DEFAULT NULL
);
CREATE TABLE IF NOT EXISTS project_file_users (
CREATE INDEX project_files__user_id__idx
ON project_files(user_id);
CREATE INDEX project_files__project_id__idx
ON project_files(project_id);
CREATE TABLE project_file_media (
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
file_id uuid NOT NULL REFERENCES project_files(id) ON DELETE CASCADE,
type text NOT NULL,
path text NOT NULL,
metadata bytea NULL DEFAULT NULL
);
CREATE INDEX project_file_media__file_id__idx
ON project_file_media(file_id);
CREATE TABLE project_file_users (
file_id uuid NOT NULL REFERENCES project_files(id) ON DELETE CASCADE,
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
@ -47,7 +75,13 @@ CREATE TABLE IF NOT EXISTS project_file_users (
PRIMARY KEY (user_id, file_id)
);
CREATE TABLE IF NOT EXISTS project_pages (
CREATE INDEX project_file_users__user_id__idx
ON project_file_users(user_id);
CREATE INDEX project_file_users__file_id__idx
ON project_file_users(file_id);
CREATE TABLE project_pages (
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
@ -64,7 +98,13 @@ CREATE TABLE IF NOT EXISTS project_pages (
data bytea NOT NULL
);
CREATE TABLE IF NOT EXISTS project_page_snapshots (
CREATE INDEX project_pages__user_id__idx
ON project_pages(user_id);
CREATE INDEX project_pages__file_id__idx
ON project_pages(file_id);
CREATE TABLE project_page_snapshots (
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id uuid NULL REFERENCES users(id) ON DELETE SET NULL,
@ -81,18 +121,11 @@ CREATE TABLE IF NOT EXISTS project_page_snapshots (
changes bytea NULL DEFAULT NULL
);
-- Indexes
CREATE INDEX project_page_snapshots__user_id__idx
ON project_page_snapshots(user_id);
CREATE INDEX projects__user_id__idx ON projects(user_id);
CREATE INDEX project_files__user_id__idx ON project_files(user_id);
CREATE INDEX project_files__project_id__idx ON project_files(project_id);
CREATE INDEX project_pages__user_id__idx ON project_pages(user_id);
CREATE INDEX project_pages__file_id__idx ON project_pages(file_id);
CREATE INDEX project_page_snapshots__page_id__idx ON project_page_snapshots(page_id);
CREATE INDEX project_page_snapshots__user_id__idx ON project_page_snapshots(user_id);
CREATE INDEX project_page_snapshots__page_id_id__idx
ON project_page_snapshots(page_id);
-- Triggers

View file

@ -1,4 +1,6 @@
CREATE TABLE IF NOT EXISTS tasks (
--- Tables
CREATE TABLE tasks (
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
@ -20,3 +22,20 @@ CREATE TABLE IF NOT EXISTS tasks (
CREATE INDEX tasks__scheduled_at__queue__idx
ON tasks (scheduled_at, queue);
CREATE TABLE scheduled_tasks (
id text PRIMARY KEY,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
modified_at timestamptz NOT NULL DEFAULT clock_timestamp(),
executed_at timestamptz NULL DEFAULT NULL,
cron_expr text NOT NULL
);
--- Triggers
CREATE TRIGGER scheduled_tasks__modified_at__tgr
BEFORE UPDATE ON scheduled_tasks
FOR EACH ROW EXECUTE PROCEDURE update_modified_at();

View file

@ -1,6 +1,4 @@
-- Tables
CREATE TABLE IF NOT EXISTS image_collections (
CREATE TABLE image_collections (
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
@ -11,9 +9,13 @@ CREATE TABLE IF NOT EXISTS image_collections (
name text NOT NULL
);
CREATE TABLE IF NOT EXISTS images (
CREATE INDEX image_collections__user_id__idx
ON image_collections(user_id);
CREATE TABLE images (
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
collection_id uuid REFERENCES image_collections(id) ON DELETE CASCADE,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
modified_at timestamptz NOT NULL DEFAULT clock_timestamp(),
@ -22,20 +24,16 @@ CREATE TABLE IF NOT EXISTS images (
width int NOT NULL,
height int NOT NULL,
mimetype text NOT NULL,
collection_id uuid REFERENCES image_collections(id)
ON DELETE SET NULL
DEFAULT NULL,
name text NOT NULL,
path text NOT NULL
);
-- Indexes
CREATE INDEX images__user_id__idx
ON images(user_id);
CREATE INDEX image_collections__user_id__idx ON image_collections (user_id);
CREATE INDEX images__collection_id__idx ON images (collection_id);
CREATE INDEX images__user_id__idx ON images (user_id);
-- Triggers
CREATE INDEX images__collection_id__idx
ON images(collection_id);
CREATE TRIGGER image_collections__modified_at__tgr
BEFORE UPDATE ON image_collections

View file

@ -1,6 +1,6 @@
-- Tables
CREATE TABLE IF NOT EXISTS icon_collections (
CREATE TABLE icon_collections (
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
@ -11,7 +11,7 @@ CREATE TABLE IF NOT EXISTS icon_collections (
name text NOT NULL
);
CREATE TABLE IF NOT EXISTS icons (
CREATE TABLE icons (
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,