From 115eb5defb986afdb48ddcd094b22d39745a8fc4 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Mon, 12 Feb 2024 15:02:52 +0000 Subject: [PATCH] Desktop, Cli, Mobile: Migrate profile in preparation for trash feature --- .eslintignore | 1 + .gitignore | 1 + .../lib/services/database/migrations/46.ts | 26 +++++++ .../lib/services/database/migrations/index.ts | 2 + packages/lib/services/database/types.ts | 74 ++++--------------- .../lib/services/plugins/api/noteListType.ts | 2 +- .../server/src/utils/testing/testUtils.ts | 1 + 7 files changed, 45 insertions(+), 62 deletions(-) create mode 100644 packages/lib/services/database/migrations/46.ts diff --git a/.eslintignore b/.eslintignore index 0d372308e5..6448ee750d 100644 --- a/.eslintignore +++ b/.eslintignore @@ -801,6 +801,7 @@ packages/lib/services/database/migrations/42.js packages/lib/services/database/migrations/43.js packages/lib/services/database/migrations/44.js packages/lib/services/database/migrations/45.js +packages/lib/services/database/migrations/46.js packages/lib/services/database/migrations/index.js packages/lib/services/database/sqlStringToLines.js packages/lib/services/database/types.js diff --git a/.gitignore b/.gitignore index 0e2d5603cc..a44e69d251 100644 --- a/.gitignore +++ b/.gitignore @@ -781,6 +781,7 @@ packages/lib/services/database/migrations/42.js packages/lib/services/database/migrations/43.js packages/lib/services/database/migrations/44.js packages/lib/services/database/migrations/45.js +packages/lib/services/database/migrations/46.js packages/lib/services/database/migrations/index.js packages/lib/services/database/sqlStringToLines.js packages/lib/services/database/types.js diff --git a/packages/lib/services/database/migrations/46.ts b/packages/lib/services/database/migrations/46.ts new file mode 100644 index 0000000000..153c98a5e7 --- /dev/null +++ b/packages/lib/services/database/migrations/46.ts @@ -0,0 +1,26 @@ +import { SqlQuery } from '../types'; + +export default (): (SqlQuery|string)[] => { + return [ + 'ALTER TABLE `notes` ADD COLUMN `deleted_time` INT NOT NULL DEFAULT 0', + 'ALTER TABLE `folders` ADD COLUMN `deleted_time` INT NOT NULL DEFAULT 0', + 'DROP VIEW tags_with_note_count', + ` + CREATE VIEW tags_with_note_count AS + SELECT + tags.id as id, + tags.title as title, + tags.created_time as created_time, + tags.updated_time as updated_time, + COUNT(notes.id) as note_count, + SUM(CASE WHEN notes.todo_completed > 0 THEN 1 ELSE 0 END) AS todo_completed_count + FROM tags + LEFT JOIN note_tags nt on nt.tag_id = tags.id + LEFT JOIN notes on notes.id = nt.note_id + WHERE + notes.id IS NOT NULL + AND notes.deleted_time = 0 + GROUP BY tags.id + `, + ]; +}; diff --git a/packages/lib/services/database/migrations/index.ts b/packages/lib/services/database/migrations/index.ts index f165cc050b..6ffb51e4b9 100644 --- a/packages/lib/services/database/migrations/index.ts +++ b/packages/lib/services/database/migrations/index.ts @@ -3,6 +3,7 @@ import migration42 from './42'; import migration43 from './43'; import migration44 from './44'; import migration45 from './45'; +import migration46 from './46'; import { Migration } from '../types'; @@ -11,6 +12,7 @@ const index: Migration[] = [ migration43, migration44, migration45, + migration46, ]; export default index; diff --git a/packages/lib/services/database/types.ts b/packages/lib/services/database/types.ts index b2bffb9d2e..368fcf339a 100644 --- a/packages/lib/services/database/types.ts +++ b/packages/lib/services/database/types.ts @@ -76,58 +76,6 @@ interface DatabaseTables { [key: string]: DatabaseTable; } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // AUTO-GENERATED BY packages/tools/generate-database-types.js /* @@ -150,6 +98,7 @@ export interface DeletedItemEntity { } export interface FolderEntity { 'created_time'?: number; + 'deleted_time'?: number; 'encryption_applied'?: number; 'encryption_cipher_text'?: string; 'icon'?: string; @@ -272,6 +221,7 @@ export interface NoteEntity { 'body'?: string; 'conflict_original_id'?: string; 'created_time'?: number; + 'deleted_time'?: number; 'encryption_applied'?: number; 'encryption_cipher_text'?: string; 'id'?: string | null; @@ -425,6 +375,7 @@ export interface VersionEntity { export const databaseSchema: DatabaseTables = { folders: { created_time: { type: 'number' }, + deleted_time: { type: 'number' }, encryption_applied: { type: 'number' }, encryption_cipher_text: { type: 'string' }, icon: { type: 'string' }, @@ -605,6 +556,7 @@ export const databaseSchema: DatabaseTables = { body: { type: 'string' }, conflict_original_id: { type: 'string' }, created_time: { type: 'number' }, + deleted_time: { type: 'number' }, encryption_applied: { type: 'number' }, encryption_cipher_text: { type: 'string' }, id: { type: 'string' }, @@ -646,15 +598,6 @@ export const databaseSchema: DatabaseTables = { user_updated_time: { type: 'number' }, type_: { type: 'number' }, }, - tags_with_note_count: { - created_time: { type: 'number' }, - id: { type: 'string' }, - note_count: { type: 'any' }, - title: { type: 'string' }, - todo_completed_count: { type: 'any' }, - updated_time: { type: 'number' }, - type_: { type: 'number' }, - }, items_normalized: { body: { type: 'string' }, id: { type: 'number' }, @@ -709,4 +652,13 @@ export const databaseSchema: DatabaseTables = { value: { type: 'any' }, type_: { type: 'number' }, }, + tags_with_note_count: { + created_time: { type: 'number' }, + id: { type: 'string' }, + note_count: { type: 'any' }, + title: { type: 'string' }, + todo_completed_count: { type: 'any' }, + updated_time: { type: 'number' }, + type_: { type: 'number' }, + }, }; \ No newline at end of file diff --git a/packages/lib/services/plugins/api/noteListType.ts b/packages/lib/services/plugins/api/noteListType.ts index 9e16f75772..36124a9fd9 100644 --- a/packages/lib/services/plugins/api/noteListType.ts +++ b/packages/lib/services/plugins/api/noteListType.ts @@ -3,7 +3,7 @@ import { Size } from './types'; // AUTO-GENERATED by generate-database-type -type ListRendererDatabaseDependency = 'folder.created_time' | 'folder.encryption_applied' | 'folder.encryption_cipher_text' | 'folder.icon' | 'folder.id' | 'folder.is_shared' | 'folder.master_key_id' | 'folder.parent_id' | 'folder.share_id' | 'folder.title' | 'folder.updated_time' | 'folder.user_created_time' | 'folder.user_data' | 'folder.user_updated_time' | 'folder.type_' | 'note.altitude' | 'note.application_data' | 'note.author' | 'note.body' | 'note.conflict_original_id' | 'note.created_time' | 'note.encryption_applied' | 'note.encryption_cipher_text' | 'note.id' | 'note.is_conflict' | 'note.is_shared' | 'note.is_todo' | 'note.latitude' | 'note.longitude' | 'note.markup_language' | 'note.master_key_id' | 'note.order' | 'note.parent_id' | 'note.share_id' | 'note.source' | 'note.source_application' | 'note.source_url' | 'note.title' | 'note.todo_completed' | 'note.todo_due' | 'note.updated_time' | 'note.user_created_time' | 'note.user_data' | 'note.user_updated_time' | 'note.type_'; +type ListRendererDatabaseDependency = 'folder.created_time' | 'folder.deleted_time' | 'folder.encryption_applied' | 'folder.encryption_cipher_text' | 'folder.icon' | 'folder.id' | 'folder.is_shared' | 'folder.master_key_id' | 'folder.parent_id' | 'folder.share_id' | 'folder.title' | 'folder.updated_time' | 'folder.user_created_time' | 'folder.user_data' | 'folder.user_updated_time' | 'folder.type_' | 'note.altitude' | 'note.application_data' | 'note.author' | 'note.body' | 'note.conflict_original_id' | 'note.created_time' | 'note.deleted_time' | 'note.encryption_applied' | 'note.encryption_cipher_text' | 'note.id' | 'note.is_conflict' | 'note.is_shared' | 'note.is_todo' | 'note.latitude' | 'note.longitude' | 'note.markup_language' | 'note.master_key_id' | 'note.order' | 'note.parent_id' | 'note.share_id' | 'note.source' | 'note.source_application' | 'note.source_url' | 'note.title' | 'note.todo_completed' | 'note.todo_due' | 'note.updated_time' | 'note.user_created_time' | 'note.user_data' | 'note.user_updated_time' | 'note.type_'; // AUTO-GENERATED by generate-database-type export enum ItemFlow { diff --git a/packages/server/src/utils/testing/testUtils.ts b/packages/server/src/utils/testing/testUtils.ts index c50360182b..51ed23a6e0 100644 --- a/packages/server/src/utils/testing/testUtils.ts +++ b/packages/server/src/utils/testing/testUtils.ts @@ -554,6 +554,7 @@ share_id: ${note.share_id || ''} conflict_original_id: master_key_id: user_data: +deleted_time: 0 type_: 1`; }