From 663a5de7df7867d98dd163959a425b2c50f1db72 Mon Sep 17 00:00:00 2001 From: Hakan Shehu Date: Mon, 28 Oct 2024 23:44:10 +0100 Subject: [PATCH] Fix desktop database migrations --- desktop/src/main/data/workspace/migrations.ts | 143 +++++++++++------- 1 file changed, 88 insertions(+), 55 deletions(-) diff --git a/desktop/src/main/data/workspace/migrations.ts b/desktop/src/main/data/workspace/migrations.ts index 8611d906..a51ab42c 100644 --- a/desktop/src/main/data/workspace/migrations.ts +++ b/desktop/src/main/data/workspace/migrations.ts @@ -84,49 +84,21 @@ const createChangesTable: Migration = { }, }; -const createNodeNamesTable: Migration = { +const createDownloadsTable: Migration = { up: async (db) => { - await sql` - CREATE VIRTUAL TABLE node_names USING fts5(id UNINDEXED, name); - - CREATE TRIGGER node_insert_name - AFTER INSERT ON nodes - WHEN json_type(NEW.attributes, '$.name') = 'text' - BEGIN - INSERT INTO node_names (id, name) - VALUES ( - NEW.id, - json_extract(NEW.attributes, '$.name') - ); - END; - - CREATE TRIGGER node_update_name - AFTER UPDATE ON nodes - WHEN json_extract(OLD.attributes, '$.name') IS NOT json_extract(NEW.attributes, '$.name') - BEGIN - DELETE FROM node_names WHERE id = OLD.id; - - INSERT INTO node_names (id, name) - SELECT - NEW.id, - json_extract(NEW.attributes, '$.name') - WHERE json_type(NEW.attributes, '$.name') = 'text'; - END; - - CREATE TRIGGER node_delete_name - AFTER DELETE ON nodes - BEGIN - DELETE FROM node_names WHERE id = OLD.id; - END; - `.execute(db); + await db.schema + .createTable('downloads') + .addColumn('node_id', 'text', (col) => + col.notNull().primaryKey().references('nodes.id'), + ) + .addColumn('created_at', 'text', (col) => col.notNull()) + .addColumn('updated_at', 'text') + .addColumn('progress', 'integer', (col) => col.defaultTo(0)) + .addColumn('retry_count', 'integer', (col) => col.defaultTo(0)) + .execute(); }, down: async (db) => { - await sql` - DROP TABLE node_names; - DROP TRIGGER IF EXISTS node_insert_name; - DROP TRIGGER IF EXISTS node_update_name; - DROP TRIGGER IF EXISTS node_delete_name; - `.execute(db); + await db.schema.dropTable('downloads').execute(); }, }; @@ -148,21 +120,79 @@ const createUploadsTable: Migration = { }, }; -const createDownloadsTable: Migration = { +const createNodeNamesTable: Migration = { up: async (db) => { - await db.schema - .createTable('downloads') - .addColumn('node_id', 'text', (col) => - col.notNull().primaryKey().references('nodes.id'), - ) - .addColumn('created_at', 'text', (col) => col.notNull()) - .addColumn('updated_at', 'text') - .addColumn('progress', 'integer', (col) => col.defaultTo(0)) - .addColumn('retry_count', 'integer', (col) => col.defaultTo(0)) - .execute(); + await sql` + CREATE VIRTUAL TABLE node_names USING fts5(id UNINDEXED, name); + `.execute(db); }, down: async (db) => { - await db.schema.dropTable('downloads').execute(); + await sql` + DROP TABLE IF EXISTS node_names; + `.execute(db); + }, +}; + +const createNodeInsertNameTrigger: Migration = { + up: async (db) => { + await sql` + CREATE TRIGGER node_insert_name + AFTER INSERT ON nodes + WHEN json_type(NEW.attributes, '$.name') = 'text' + BEGIN + INSERT INTO node_names (id, name) + VALUES ( + NEW.id, + json_extract(NEW.attributes, '$.name') + ); + END; + `.execute(db); + }, + down: async (db) => { + await sql` + DROP TRIGGER node_insert_name; + `.execute(db); + }, +}; + +const createNodeUpdateNameTrigger: Migration = { + up: async (db) => { + await sql` + CREATE TRIGGER node_update_name + AFTER UPDATE ON nodes + WHEN json_extract(OLD.attributes, '$.name') IS NOT json_extract(NEW.attributes, '$.name') + BEGIN + DELETE FROM node_names WHERE id = OLD.id; + + INSERT INTO node_names (id, name) + SELECT + NEW.id, + json_extract(NEW.attributes, '$.name') + WHERE json_type(NEW.attributes, '$.name') = 'text'; + END; + `.execute(db); + }, + down: async (db) => { + await sql` + DROP TRIGGER node_update_name; + `.execute(db); + }, +}; + +const createNodeDeleteNameTrigger: Migration = { + up: async (db) => { + await sql` + CREATE TRIGGER node_delete_name + AFTER DELETE ON nodes + BEGIN + DELETE FROM node_names WHERE id = OLD.id; + END; + `.execute(db); + }, + down: async (db) => { + await sql` + DROP TRIGGER node_delete_name; + `.execute(db); }, }; @@ -170,7 +200,10 @@ export const workspaceDatabaseMigrations: Record = { '00001_create_nodes_table': createNodesTable, '00002_create_user_nodes_table': createUserNodesTable, '00003_create_changes_table': createChangesTable, - '00004_create_node_names_table': createNodeNamesTable, - '00005_create_uploads_table': createUploadsTable, - '00006_create_downloads_table': createDownloadsTable, + '00004_create_uploads_table': createUploadsTable, + '00005_create_downloads_table': createDownloadsTable, + '00006_create_node_names_table': createNodeNamesTable, + '00007_create_node_insert_name_trigger': createNodeInsertNameTrigger, + '00008_create_node_update_name_trigger': createNodeUpdateNameTrigger, + '00009_create_node_delete_name_trigger': createNodeDeleteNameTrigger, };