Fuck with migrations

This commit is contained in:
jaina heartles 2022-12-05 03:21:43 -08:00
parent 3c8ac8d7d4
commit dba9ec516d
1 changed files with 44 additions and 47 deletions

View File

@ -70,8 +70,9 @@ const create_migration_table =
\\);
;
// NOTE: Until the first public release, i may collapse multiple
// migrations into a single one. this will require db recreation
// NOTE: I might fuck with these until the v0.1 release. After that, I'll guarantee that you
// can upgrade to any v0.x release by just running unapplied migrations in order. You might
// need extra work to upgrade to v1.0 but you shouldn't have to recreate the db.
const migrations: []const Migration = &.{
.{
.name = "accounts and actors",
@ -212,7 +213,7 @@ const migrations: []const Migration = &.{
\\CREATE TABLE file_upload(
\\ id UUID NOT NULL PRIMARY KEY,
\\
\\ created_by UUID REFERENCES account(id),
\\ owner_id UUID REFERENCES actor(id),
\\ size INTEGER NOT NULL,
\\
\\ filename TEXT NOT NULL,
@ -222,15 +223,14 @@ const migrations: []const Migration = &.{
\\
\\ is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
\\
\\ created_at TIMESTAMPTZ NOT NULL,
\\ updated_at TIMESTAMPTZ NOT NULL
\\ created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
\\ updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
\\);
\\
\\CREATE TABLE drive_entry(
\\ id UUID NOT NULL PRIMARY KEY,
\\
\\ account_owner_id UUID REFERENCES account(id),
\\ community_owner_id UUID REFERENCES community(id),
\\ owner_id UUID REFERENCES actor(id),
\\
\\ name TEXT,
\\ parent_directory_id UUID REFERENCES drive_entry(id),
@ -238,10 +238,6 @@ const migrations: []const Migration = &.{
\\ 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)
\\ )
@ -250,7 +246,7 @@ const migrations: []const Migration = &.{
\\ON drive_entry(
\\ name,
\\ COALESCE(parent_directory_id, ''),
\\ COALESCE(account_owner_id, community_owner_id)
\\ owner_id
\\);
,
.down =
@ -265,44 +261,39 @@ const migrations: []const Migration = &.{
\\CREATE VIEW drive_entry_path(
\\ id,
\\ path,
\\ account_owner_id,
\\ community_owner_id,
\\ owner_id,
\\ name,
\\ file_id,
\\ kind
\\) AS WITH RECURSIVE full_path(
\\ id,
\\ path,
\\ account_owner_id,
\\ community_owner_id,
\\ kind
\\ owner_id,
\\) AS (
\\ SELECT
\\ id,
\\ '' AS path,
\\ account_owner_id,
\\ community_owner_id,
\\ 'dir' AS kind
\\ owner_id,
\\ 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
\\ base.owner_id,
\\ 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
\\ AND base.owner_id = dir.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;
\\ full_path.id,
\\ (CASE WHEN LENGTH(full_path.path) = 0 '/' ELSE full_path.path END) AS path,
\\ full_path.owner_id,
\\ drive_entry.name,
\\ drive_entry.file_id,
\\ (CASE WHEN drive_entry.file_id IS NULL 'file' ELSE 'dir' END) as kind
\\FROM full_path JOIN drive_entry ON full_path.id = drive_entry.id;
,
.down =
\\DROP VIEW drive_entry_path;
@ -313,34 +304,40 @@ const migrations: []const Migration = &.{
.up =
\\INSERT INTO drive_entry(
\\ id,
\\ account_owner_id,
\\ community_owner_id,
\\ owner_id,
\\ parent_directory_id,
\\ name,
\\ file_id
\\) SELECT
\\ id,
\\ id AS account_owner_id,
\\ NULL AS community_owner_id,
\\ id AS owner_id,
\\ NULL AS parent_directory_id,
\\ NULL AS name,
\\ NULL AS file_id
\\FROM account;
\\INSERT INTO drive_entry(
\\FROM actor;
,
.down = "",
},
.{
.name = "community actors",
.up = "ALTER TABLE community ADD COLUMN community_actor_id UUID REFERENCES actor(id)",
.down = "ALTER COLUMN community DROP COLUMN community_actor_id",
},
.{
.name = "create community actors",
.up =
\\INSERT INTO actor(
\\ id,
\\ account_owner_id,
\\ community_owner_id,
\\ parent_directory_id,
\\ name,
\\ file_id
\\ username,
\\ community_id,
\\ created_at
\\) SELECT
\\ id,
\\ NULL AS account_owner_id,
\\ id AS community_owner_id,
\\ NULL AS parent_directory_id,
\\ NULL AS name,
\\ NULL AS file_id
\\ host AS username,
\\ id AS community_id,
\\ CURRENT_TIMESTAMP AS created_at
\\FROM community;
\\UPDATE community SET community_actor_id = id;
,
.down = "",
},