Rework fs db schema
This commit is contained in:
parent
6cfd035883
commit
31f676580d
1 changed files with 122 additions and 5 deletions
|
@ -209,22 +209,139 @@ const migrations: []const Migration = &.{
|
|||
.{
|
||||
.name = "files",
|
||||
.up =
|
||||
\\CREATE TABLE drive_file(
|
||||
\\CREATE TABLE file_upload(
|
||||
\\ id UUID NOT NULL PRIMARY KEY,
|
||||
\\
|
||||
\\ filename TEXT NOT NULL,
|
||||
\\ account_owner_id UUID REFERENCES account(id),
|
||||
\\ community_owner_id UUID REFERENCES community(id),
|
||||
\\ created_by UUID REFERENCES account(id),
|
||||
\\ size INTEGER NOT NULL,
|
||||
\\
|
||||
\\ filename TEXT NOT NULL,
|
||||
\\ description TEXT,
|
||||
\\ content_type TEXT,
|
||||
\\ sensitive BOOLEAN NOT NULL,
|
||||
\\
|
||||
\\ is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
\\
|
||||
\\ created_at TIMESTAMPTZ NOT NULL,
|
||||
\\ updated_at TIMESTAMPTZ NOT NULL
|
||||
\\);
|
||||
\\
|
||||
\\CREATE TABLE drive_entry(
|
||||
\\ id UUID NOT NULL PRIMARY KEY,
|
||||
\\
|
||||
\\ account_owner_id UUID REFERENCES account(id),
|
||||
\\ community_owner_id UUID REFERENCES community(id),
|
||||
\\
|
||||
\\ name TEXT,
|
||||
\\ parent_directory_id UUID REFERENCES drive_entry(id),
|
||||
\\
|
||||
\\ file_id UUID REFERENCES file_upload(id),
|
||||
\\
|
||||
\\ CHECK(
|
||||
\\ (account_owner_id IS NULL AND community_owner_id IS NOT NULL)
|
||||
\\ OR (account_owner_id IS NOT NULL AND community_owner_id IS NULL)
|
||||
\\ ),
|
||||
\\ CHECK(
|
||||
\\ (name IS NULL AND parent_directory_id IS NULL AND file_id IS NULL)
|
||||
\\ OR (name IS NOT NULL AND parent_directory_id IS NOT NULL)
|
||||
\\ )
|
||||
\\);
|
||||
\\CREATE UNIQUE INDEX drive_entry_uniqueness
|
||||
\\ON drive_entry(
|
||||
\\ name,
|
||||
\\ COALESCE(parent_directory_id, ''),
|
||||
\\ COALESCE(account_owner_id, community_owner_id)
|
||||
\\);
|
||||
,
|
||||
.down = "DROP TABLE drive_file",
|
||||
.down =
|
||||
\\DROP INDEX drive_entry_uniqueness;
|
||||
\\DROP TABLE drive_entry;
|
||||
\\DROP TABLE file_upload;
|
||||
,
|
||||
},
|
||||
.{
|
||||
.name = "drive_entry_path",
|
||||
.up =
|
||||
\\CREATE VIEW drive_entry_path(
|
||||
\\ id,
|
||||
\\ path,
|
||||
\\ account_owner_id,
|
||||
\\ community_owner_id,
|
||||
\\ kind
|
||||
\\) AS WITH RECURSIVE full_path(
|
||||
\\ id,
|
||||
\\ path,
|
||||
\\ account_owner_id,
|
||||
\\ community_owner_id,
|
||||
\\ kind
|
||||
\\) AS (
|
||||
\\ SELECT
|
||||
\\ id,
|
||||
\\ '' AS path,
|
||||
\\ account_owner_id,
|
||||
\\ community_owner_id,
|
||||
\\ 'dir' AS kind
|
||||
\\ FROM drive_entry
|
||||
\\ WHERE parent_directory_id IS NULL
|
||||
\\ UNION ALL
|
||||
\\ SELECT
|
||||
\\ base.id,
|
||||
\\ (dir.path || '/' || base.name) AS path,
|
||||
\\ base.account_owner_id,
|
||||
\\ base.community_owner_id,
|
||||
\\ (CASE WHEN base.file_id IS NULL THEN 'dir' ELSE 'file' END) as kind
|
||||
\\ FROM drive_entry AS base
|
||||
\\ JOIN full_path AS dir ON
|
||||
\\ base.parent_directory_id = dir.id
|
||||
\\ AND base.account_owner_id IS NOT DISTINCT FROM dir.account_owner_id
|
||||
\\ AND base.community_owner_id IS NOT DISTINCT FROM dir.community_owner_id
|
||||
\\)
|
||||
\\SELECT
|
||||
\\ id,
|
||||
\\ (CASE WHEN kind = 'dir' THEN path || '/' ELSE path END) AS path,
|
||||
\\ account_owner_id,
|
||||
\\ community_owner_id,
|
||||
\\ kind
|
||||
\\FROM full_path;
|
||||
,
|
||||
.down =
|
||||
\\DROP VIEW drive_entry_path;
|
||||
,
|
||||
},
|
||||
.{
|
||||
.name = "create drive root directories",
|
||||
.up =
|
||||
\\INSERT INTO drive_entry(
|
||||
\\ id,
|
||||
\\ account_owner_id,
|
||||
\\ community_owner_id,
|
||||
\\ parent_directory_id,
|
||||
\\ name,
|
||||
\\ file_id
|
||||
\\) SELECT
|
||||
\\ id,
|
||||
\\ id AS account_owner_id,
|
||||
\\ NULL AS community_owner_id,
|
||||
\\ NULL AS parent_directory_id,
|
||||
\\ NULL AS name,
|
||||
\\ NULL AS file_id
|
||||
\\FROM account;
|
||||
\\INSERT INTO drive_entry(
|
||||
\\ id,
|
||||
\\ account_owner_id,
|
||||
\\ community_owner_id,
|
||||
\\ parent_directory_id,
|
||||
\\ name,
|
||||
\\ file_id
|
||||
\\) SELECT
|
||||
\\ id,
|
||||
\\ NULL AS account_owner_id,
|
||||
\\ id AS community_owner_id,
|
||||
\\ NULL AS parent_directory_id,
|
||||
\\ NULL AS name,
|
||||
\\ NULL AS file_id
|
||||
\\FROM community;
|
||||
,
|
||||
.down = "",
|
||||
},
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue