Open-source and local-first Slack and Notion alternative that puts you in control of your data
TypeScript chatchat-applicationcrdteditorelectronknowledge-baselocal-firstnotionnotion-alternativerealtime-collaborationself-hostedslacksqliteteam-collaborationwikiyjs
Fix desktop database migrations
Hakan Shehu663a5de7df78
diff --git a/desktop/src/main/data/workspace/migrations.ts b/desktop/src/main/data/workspace/migrations.tsindex 8611d906..a51ab42c 100644--- a/desktop/src/main/data/workspace/migrations.ts+++ b/desktop/src/main/data/workspace/migrations.ts@@ -84,11 +84,58 @@ const createChangesTable: Migration = {},};+const createDownloadsTable: 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();+ },+ down: async (db) => {+ await db.schema.dropTable('downloads').execute();+ },+};++const createUploadsTable: Migration = {+ up: async (db) => {+ await db.schema+ .createTable('uploads')+ .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 db.schema.dropTable('uploads').execute();+ },+};+const createNodeNamesTable: Migration = {up: async (db) => {await sql`CREATE VIRTUAL TABLE node_names USING fts5(id UNINDEXED, name);+ `.execute(db);+ },+ down: async (db) => {+ await sql`+ DROP TABLE IF EXISTS node_names;+ `.execute(db);+ },+};+const createNodeInsertNameTrigger: Migration = {+ up: async (db) => {+ await sql`CREATE TRIGGER node_insert_nameAFTER INSERT ON nodesWHEN json_type(NEW.attributes, '$.name') = 'text'@@ -99,7 +146,18 @@ const createNodeNamesTable: Migration = {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_nameAFTER UPDATE ON nodesWHEN json_extract(OLD.attributes, '$.name') IS NOT json_extract(NEW.attributes, '$.name')@@ -112,57 +170,29 @@ const createNodeNamesTable: Migration = {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);},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;+ DROP TRIGGER node_update_name;`.execute(db);},};-const createUploadsTable: Migration = {+const createNodeDeleteNameTrigger: Migration = {up: async (db) => {- await db.schema- .createTable('uploads')- .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 db.schema.dropTable('uploads').execute();- },-};--const createDownloadsTable: 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 TRIGGER node_delete_name+ AFTER DELETE ON nodes+ BEGIN+ DELETE FROM node_names WHERE id = OLD.id;+ END;+ `.execute(db);},down: async (db) => {- await db.schema.dropTable('downloads').execute();+ await sql`+ DROP TRIGGER node_delete_name;+ `.execute(db);},};@@ -170,7 +200,10 @@ export const workspaceDatabaseMigrations: Record<string, Migration> = {'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,};