diff --git a/db/schema.sql b/db/schema.sql index 969927a84..7a1c3ba3e 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -1,8 +1,3 @@ -CREATE TABLE IF NOT EXISTS "options" ( - `name` TEXT NOT NULL PRIMARY KEY, - `value` TEXT, - `dateModified` INT, - isSynced INTEGER NOT NULL DEFAULT 0); CREATE TABLE IF NOT EXISTS "sync" ( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `entityName` TEXT NOT NULL, @@ -29,7 +24,7 @@ CREATE TABLE IF NOT EXISTS "note_revisions" ( `isProtected` INT NOT NULL DEFAULT 0, `dateModifiedFrom` TEXT NOT NULL, `dateModifiedTo` TEXT NOT NULL -, type TEXT DEFAULT '' NOT NULL, mime TEXT DEFAULT '' NOT NULL); +, type TEXT DEFAULT '' NOT NULL, mime TEXT DEFAULT '' NOT NULL, hash TEXT DEFAULT "" NOT NULL); CREATE INDEX `IDX_note_revisions_noteId` ON `note_revisions` ( `noteId` ); @@ -49,7 +44,7 @@ CREATE TABLE IF NOT EXISTS "images" isDeleted INT NOT NULL DEFAULT 0, dateModified TEXT NOT NULL, dateCreated TEXT NOT NULL -); +, hash TEXT DEFAULT "" NOT NULL); CREATE TABLE note_images ( noteImageId TEXT PRIMARY KEY NOT NULL, @@ -58,7 +53,7 @@ CREATE TABLE note_images isDeleted INT NOT NULL DEFAULT 0, dateModified TEXT NOT NULL, dateCreated TEXT NOT NULL -); +, hash TEXT DEFAULT "" NOT NULL); CREATE INDEX IDX_note_images_noteId ON note_images (noteId); CREATE INDEX IDX_note_images_imageId ON note_images (imageId); CREATE INDEX IDX_note_images_noteId_imageId ON note_images (noteId, imageId); @@ -68,7 +63,7 @@ CREATE TABLE IF NOT EXISTS "api_tokens" token TEXT NOT NULL, dateCreated TEXT NOT NULL, isDeleted INT NOT NULL DEFAULT 0 -); +, hash TEXT DEFAULT "" NOT NULL); CREATE TABLE IF NOT EXISTS "branches" ( `branchId` TEXT NOT NULL, `noteId` TEXT NOT NULL, @@ -77,7 +72,7 @@ CREATE TABLE IF NOT EXISTS "branches" ( `prefix` TEXT, `isExpanded` BOOLEAN, `isDeleted` INTEGER NOT NULL DEFAULT 0, - `dateModified` TEXT NOT NULL, + `dateModified` TEXT NOT NULL, hash TEXT DEFAULT "" NOT NULL, dateCreated TEXT NOT NULL DEFAULT '1970-01-01T00:00:00.000Z', PRIMARY KEY(`branchId`) ); CREATE INDEX `IDX_branches_noteId` ON `branches` ( @@ -87,12 +82,6 @@ CREATE INDEX `IDX_branches_noteId_parentNoteId` ON `branches` ( `noteId`, `parentNoteId` ); -CREATE TABLE IF NOT EXISTS "recent_notes" ( - `branchId` TEXT NOT NULL PRIMARY KEY, - `notePath` TEXT NOT NULL, - `dateAccessed` TEXT NOT NULL, - isDeleted INT -); CREATE TABLE labels ( labelId TEXT not null primary key, @@ -103,18 +92,11 @@ CREATE TABLE labels dateCreated TEXT not null, dateModified TEXT not null, isDeleted INT not null -); +, hash TEXT DEFAULT "" NOT NULL); CREATE INDEX IDX_labels_name_value on labels (name, value); CREATE INDEX IDX_labels_noteId on labels (noteId); -CREATE TABLE IF NOT EXISTS "event_log" -( - id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, - noteId TEXT, - comment TEXT, - dateAdded TEXT NOT NULL -); CREATE TABLE IF NOT EXISTS "notes" ( `noteId` TEXT NOT NULL, `title` TEXT NOT NULL DEFAULT "unnamed", @@ -124,9 +106,31 @@ CREATE TABLE IF NOT EXISTS "notes" ( `dateCreated` TEXT NOT NULL, `dateModified` TEXT NOT NULL, type TEXT NOT NULL DEFAULT 'text', - mime TEXT NOT NULL DEFAULT 'text/html', + mime TEXT NOT NULL DEFAULT 'text/html', hash TEXT DEFAULT "" NOT NULL, PRIMARY KEY(`noteId`) ); -CREATE INDEX `IDX_notes_isDeleted` ON `notes` ( - `isDeleted` +CREATE INDEX IDX_branches_parentNoteId ON branches (parentNoteId); +CREATE INDEX IDX_notes_type + on notes (type); +CREATE TABLE IF NOT EXISTS "recent_notes" ( + `branchId` TEXT NOT NULL PRIMARY KEY, + `notePath` TEXT NOT NULL, + `dateCreated` TEXT NOT NULL, + isDeleted INT +, hash TEXT DEFAULT "" NOT NULL); +CREATE TABLE IF NOT EXISTS "event_log" ( + `eventId` TEXT NOT NULL PRIMARY KEY, + `noteId` TEXT, + `comment` TEXT, + `dateCreated` TEXT NOT NULL +); +CREATE TABLE IF NOT EXISTS "options" +( + optionId TEXT NOT NULL PRIMARY KEY, + name TEXT not null, + value TEXT, + dateModified INT, + isSynced INTEGER default 0 not null, + hash TEXT default "" not null, + dateCreated TEXT default '1970-01-01T00:00:00.000Z' not null ); diff --git a/src/entities/option.js b/src/entities/option.js index 9bd3ebf06..2d175fa8c 100644 --- a/src/entities/option.js +++ b/src/entities/option.js @@ -5,8 +5,8 @@ const dateUtils = require('../services/date_utils'); class Option extends Entity { static get tableName() { return "options"; } - static get primaryKeyName() { return "name"; } - static get hashedProperties() { return ["name", "value"]; } + static get primaryKeyName() { return "optionId"; } + static get hashedProperties() { return ["optionId", "name", "value"]; } beforeSaving() { super.beforeSaving(); diff --git a/src/services/sql_init.js b/src/services/sql_init.js index 0d52ce540..119db7415 100644 --- a/src/services/sql_init.js +++ b/src/services/sql_init.js @@ -94,6 +94,12 @@ async function isDbUpToDate() { } async function isUserInitialized() { + const optionsTable = await sql.getRows("SELECT name FROM sqlite_master WHERE type='table' AND name='options'"); + + if (optionsTable.length !== 1) { + return false; + } + const username = await sql.getValue("SELECT value FROM options WHERE name = 'username'"); return !!username; diff --git a/src/www b/src/www index a8aac14c5..146acfed6 100755 --- a/src/www +++ b/src/www @@ -18,6 +18,7 @@ const log = require('./services/log'); const appInfo = require('./services/app_info'); const messagingService = require('./services/messaging'); const utils = require('./services/utils'); +const sqlInit = require('./services/sql_init.js'); const port = normalizePort(config['Network']['port'] || '3000'); app.set('port', port); @@ -54,7 +55,7 @@ httpServer.listen(port); httpServer.on('error', onError); httpServer.on('listening', onListening); -messagingService.init(httpServer, sessionParser); +sqlInit.dbReady.then(() => messagingService.init(httpServer, sessionParser)); if (utils.isElectron()) { const electronRouting = require('./routes/electron');