♻️ Refactor services (for add the project-file concept.

And fix many tests.
This commit is contained in:
Andrey Antukh 2019-12-09 16:27:01 +01:00
parent af62d949d8
commit 183f0a5400
40 changed files with 1279 additions and 1006 deletions

View file

@ -10,10 +10,11 @@ CREATE TABLE users (
email text NOT NULL,
photo text NOT NULL,
password text NOT NULL,
metadata bytea NOT NULL
metadata bytea NULL DEFAULT NULL
);
CREATE TABLE IF NOT EXISTS user_storage (
CREATE TABLE IF NOT EXISTS user_attrs (
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
@ -25,7 +26,7 @@ CREATE TABLE IF NOT EXISTS user_storage (
PRIMARY KEY (key, user_id)
);
CREATE TABLE user_tokens (
CREATE TABLE IF NOT EXISTS tokens (
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
token text NOT NULL,
@ -35,7 +36,7 @@ CREATE TABLE user_tokens (
PRIMARY KEY (token, user_id)
);
CREATE TABLE sessions (
CREATE TABLE IF NOT EXISTS sessions (
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
@ -56,17 +57,19 @@ VALUES ('00000000-0000-0000-0000-000000000000'::uuid,
'!',
'{}');
CREATE UNIQUE INDEX users_username_idx
ON users USING btree (username)
CREATE UNIQUE INDEX users__username__idx
ON users (username)
WHERE deleted_at is null;
CREATE UNIQUE INDEX users_email_idx
ON users USING btree (email)
CREATE UNIQUE INDEX users__email__idx
ON users (email)
WHERE deleted_at is null;
CREATE TRIGGER users_modified_at_tgr BEFORE UPDATE ON users
CREATE TRIGGER users__modified_at__tgr
BEFORE UPDATE ON users
FOR EACH ROW EXECUTE PROCEDURE update_modified_at();
CREATE TRIGGER user_storage_modified_at_tgr BEFORE UPDATE ON user_storage
CREATE TRIGGER user_attrs__modified_at__tgr
BEFORE UPDATE ON user_attrs
FOR EACH ROW EXECUTE PROCEDURE update_modified_at();

View file

@ -8,43 +8,150 @@ CREATE TABLE IF NOT EXISTS projects (
modified_at timestamptz NOT NULL DEFAULT clock_timestamp(),
deleted_at timestamptz DEFAULT NULL,
name text NOT NULL
name text NOT NULL,
metadata bytea NULL DEFAULT NULL
);
CREATE TABLE IF NOT EXISTS projects_users (
CREATE TABLE IF NOT EXISTS project_users (
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
project_id uuid NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
modified_at timestamptz NOT NULL DEFAULT clock_timestamp(),
role text NOT NULL,
can_edit boolean DEFAULT false,
PRIMARY KEY (user_id, project_id)
);
CREATE TABLE IF NOT EXISTS 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,
name text NOT NULL,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
modified_at timestamptz NOT NULL DEFAULT clock_timestamp(),
deleted_at timestamptz DEFAULT NULL,
metadata bytea NULL DEFAULT NULL
);
CREATE TABLE IF NOT EXISTS 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,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
modified_at timestamptz NOT NULL DEFAULT clock_timestamp(),
can_edit boolean DEFAULT false,
PRIMARY KEY (user_id, file_id)
);
CREATE TABLE IF NOT EXISTS project_pages (
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
file_id uuid NOT NULL REFERENCES project_files(id) ON DELETE CASCADE,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
modified_at timestamptz NOT NULL DEFAULT clock_timestamp(),
deleted_at timestamptz DEFAULT NULL,
version bigint NOT NULL,
ordering smallint NOT NULL,
name text NOT NULL,
data bytea NOT NULL,
metadata bytea NULL DEFAULT NULL
);
CREATE TABLE IF NOT EXISTS project_page_history (
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id uuid NULL REFERENCES users(id) ON DELETE SET NULL,
page_id uuid NOT NULL REFERENCES project_pages(id) ON DELETE CASCADE,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
modified_at timestamptz NOT NULL DEFAULT clock_timestamp(),
version bigint NOT NULL DEFAULT 0,
pinned bool NOT NULL DEFAULT false,
label text NOT NULL DEFAULT '',
data bytea NOT NULL
);
-- Indexes
CREATE INDEX projects_user_idx ON projects(user_id);
CREATE INDEX projects_users_user_id_idx ON projects_users(project_id);
CREATE INDEX projects_users_project_id_idx ON projects_users(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_history__page_id__idx ON project_page_history(page_id);
CREATE INDEX project_page_history__user_id__idx ON project_page_history(user_id);
-- Triggers
CREATE OR REPLACE FUNCTION handle_project_insert()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO projects_users (user_id, project_id, role)
VALUES (NEW.user_id, NEW.id, 'owner');
INSERT INTO project_users (user_id, project_id, can_edit)
VALUES (NEW.user_id, NEW.id, true);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION handle_page_update()
RETURNS TRIGGER AS $pagechange$
DECLARE
current_dt timestamptz := clock_timestamp();
proj_id uuid;
BEGIN
UPDATE project_files
SET modified_at = current_dt
WHERE id = OLD.file_id
RETURNING project_id
INTO STRICT proj_id;
--- Update projects modified_at attribute when a
--- page of that project is modified.
UPDATE projects
SET modified_at = current_dt
WHERE id = proj_id;
RETURN NEW;
END;
$pagechange$ LANGUAGE plpgsql;
CREATE TRIGGER projects_on_insert_tgr
AFTER INSERT ON projects
FOR EACH ROW EXECUTE PROCEDURE handle_project_insert();
CREATE TRIGGER projects_modified_at_tgr
CREATE TRIGGER pages__on_update__tgr
BEFORE UPDATE ON project_pages
FOR EACH ROW EXECUTE PROCEDURE handle_page_update();
CREATE TRIGGER projects__modified_at__tgr
BEFORE UPDATE ON projects
FOR EACH ROW EXECUTE PROCEDURE update_modified_at();
CREATE TRIGGER project_files__modified_at__tgr
BEFORE UPDATE ON project_files
FOR EACH ROW EXECUTE PROCEDURE update_modified_at();
CREATE TRIGGER project_pages__modified_at__tgr
BEFORE UPDATE ON project_pages
FOR EACH ROW EXECUTE PROCEDURE update_modified_at();
CREATE TRIGGER project_page_history__modified_at__tgr
BEFORE UPDATE ON project_page_history
FOR EACH ROW EXECUTE PROCEDURE update_modified_at();

View file

@ -1,65 +0,0 @@
-- Tables
CREATE TABLE IF NOT EXISTS pages (
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,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
modified_at timestamptz NOT NULL DEFAULT clock_timestamp(),
deleted_at timestamptz DEFAULT NULL,
version bigint NOT NULL,
ordering smallint NOT NULL,
name text NOT NULL,
data bytea NOT NULL,
metadata bytea NOT NULL
);
CREATE TABLE IF NOT EXISTS pages_history (
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
page_id uuid NOT NULL REFERENCES pages(id) ON DELETE CASCADE,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
modified_at timestamptz NOT NULL DEFAULT clock_timestamp(),
version bigint NOT NULL DEFAULT 0,
pinned bool NOT NULL DEFAULT false,
label text NOT NULL DEFAULT '',
data bytea NOT NULL,
metadata bytea NOT NULL
);
-- Indexes
CREATE INDEX pages_project_idx ON pages(project_id);
CREATE INDEX pages_user_idx ON pages(user_id);
CREATE INDEX pages_history_page_idx ON pages_history(page_id);
CREATE INDEX pages_history_user_idx ON pages_history(user_id);
-- Triggers
CREATE OR REPLACE FUNCTION handle_page_update()
RETURNS TRIGGER AS $pagechange$
BEGIN
--- Update projects modified_at attribute when a
--- page of that project is modified.
UPDATE projects SET modified_at = clock_timestamp()
WHERE id = OLD.project_id;
RETURN NEW;
END;
$pagechange$ LANGUAGE plpgsql;
CREATE TRIGGER page_on_update_tgr BEFORE UPDATE ON pages
FOR EACH ROW EXECUTE PROCEDURE handle_page_update();
CREATE TRIGGER pages_modified_at_tgr BEFORE UPDATE ON pages
FOR EACH ROW EXECUTE PROCEDURE update_modified_at();
CREATE TRIGGER pages_history_modified_at_tgr BEFORE UPDATE ON pages
FOR EACH ROW EXECUTE PROCEDURE update_modified_at();

View file

@ -1,6 +1,6 @@
-- Tables
CREATE TABLE IF NOT EXISTS images_collections (
CREATE TABLE IF NOT EXISTS image_collections (
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
@ -22,7 +22,7 @@ CREATE TABLE IF NOT EXISTS images (
width int NOT NULL,
height int NOT NULL,
mimetype text NOT NULL,
collection_id uuid REFERENCES images_collections(id)
collection_id uuid REFERENCES image_collections(id)
ON DELETE SET NULL
DEFAULT NULL,
name text NOT NULL,
@ -31,20 +31,17 @@ CREATE TABLE IF NOT EXISTS images (
-- Indexes
CREATE INDEX images_collections_user_idx
ON images_collections (user_id);
CREATE INDEX images_collection_idx
ON images (collection_id);
CREATE INDEX images_user_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 TRIGGER images_collections_modified_at_tgr BEFORE UPDATE ON images_collections
CREATE TRIGGER image_collections__modified_at__tgr
BEFORE UPDATE ON image_collections
FOR EACH ROW EXECUTE PROCEDURE update_modified_at();
CREATE TRIGGER images_modified_at_tgr BEFORE UPDATE ON images
FOR EACH ROW EXECUTE PROCEDURE update_modified_at();
CREATE TRIGGER images__modified_at__tgr
BEFORE UPDATE ON images
FOR EACH ROW EXECUTE PROCEDURE update_modified_at();

View file

@ -1,6 +1,6 @@
-- Tables
CREATE TABLE IF NOT EXISTS icons_collections (
CREATE TABLE IF NOT EXISTS icon_collections (
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
@ -23,27 +23,23 @@ CREATE TABLE IF NOT EXISTS icons (
content text NOT NULL,
metadata bytea NOT NULL,
collection_id uuid REFERENCES icons_collections(id)
collection_id uuid REFERENCES icon_collections(id)
ON DELETE SET NULL
DEFAULT NULL
);
-- Indexes
CREATE INDEX icon_colections_user_idx
ON icons_collections (user_id);
CREATE INDEX icons_user_idx
ON icons (user_id);
CREATE INDEX icons_collection_idx
ON icons (collection_id);
CREATE INDEX icon_colections__user_id__idx ON icon_collections (user_id);
CREATE INDEX icons__user_id__idx ON icons(user_id);
CREATE INDEX icons__collection_id__idx ON icons(collection_id);
-- Triggers
CREATE TRIGGER icons_collections_modified_at_tgr BEFORE UPDATE ON icons_collections
FOR EACH ROW EXECUTE PROCEDURE update_modified_at();
CREATE TRIGGER icons_modified_at_tgr BEFORE UPDATE ON icons
FOR EACH ROW EXECUTE PROCEDURE update_modified_at();
CREATE TRIGGER icon_collections__modified_at__tgr
BEFORE UPDATE ON icon_collections
FOR EACH ROW EXECUTE PROCEDURE update_modified_at();
CREATE TRIGGER icons__modified_at__tgr
BEFORE UPDATE ON icons
FOR EACH ROW EXECUTE PROCEDURE update_modified_at();