mirror of
https://github.com/colanode/colanode.git
synced 2025-12-28 16:06:37 +01:00
Fix desktop database migrations
This commit is contained in:
@@ -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<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,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user