Fuck with migrations

This commit is contained in:
jaina heartles 2022-12-05 03:21:43 -08:00
parent 3c8ac8d7d4
commit dba9ec516d

View file

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