initialization and schema fixes, closes #111

This commit is contained in:
azivner 2018-06-01 22:26:37 -04:00
parent c07785be67
commit fc6669d254
4 changed files with 41 additions and 30 deletions

View file

@ -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" ( CREATE TABLE IF NOT EXISTS "sync" (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`entityName` TEXT NOT NULL, `entityName` TEXT NOT NULL,
@ -29,7 +24,7 @@ CREATE TABLE IF NOT EXISTS "note_revisions" (
`isProtected` INT NOT NULL DEFAULT 0, `isProtected` INT NOT NULL DEFAULT 0,
`dateModifiedFrom` TEXT NOT NULL, `dateModifiedFrom` TEXT NOT NULL,
`dateModifiedTo` 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` ( CREATE INDEX `IDX_note_revisions_noteId` ON `note_revisions` (
`noteId` `noteId`
); );
@ -49,7 +44,7 @@ CREATE TABLE IF NOT EXISTS "images"
isDeleted INT NOT NULL DEFAULT 0, isDeleted INT NOT NULL DEFAULT 0,
dateModified TEXT NOT NULL, dateModified TEXT NOT NULL,
dateCreated TEXT NOT NULL dateCreated TEXT NOT NULL
); , hash TEXT DEFAULT "" NOT NULL);
CREATE TABLE note_images CREATE TABLE note_images
( (
noteImageId TEXT PRIMARY KEY NOT NULL, noteImageId TEXT PRIMARY KEY NOT NULL,
@ -58,7 +53,7 @@ CREATE TABLE note_images
isDeleted INT NOT NULL DEFAULT 0, isDeleted INT NOT NULL DEFAULT 0,
dateModified TEXT NOT NULL, dateModified TEXT NOT NULL,
dateCreated 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_noteId ON note_images (noteId);
CREATE INDEX IDX_note_images_imageId ON note_images (imageId); CREATE INDEX IDX_note_images_imageId ON note_images (imageId);
CREATE INDEX IDX_note_images_noteId_imageId ON note_images (noteId, 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, token TEXT NOT NULL,
dateCreated TEXT NOT NULL, dateCreated TEXT NOT NULL,
isDeleted INT NOT NULL DEFAULT 0 isDeleted INT NOT NULL DEFAULT 0
); , hash TEXT DEFAULT "" NOT NULL);
CREATE TABLE IF NOT EXISTS "branches" ( CREATE TABLE IF NOT EXISTS "branches" (
`branchId` TEXT NOT NULL, `branchId` TEXT NOT NULL,
`noteId` TEXT NOT NULL, `noteId` TEXT NOT NULL,
@ -77,7 +72,7 @@ CREATE TABLE IF NOT EXISTS "branches" (
`prefix` TEXT, `prefix` TEXT,
`isExpanded` BOOLEAN, `isExpanded` BOOLEAN,
`isDeleted` INTEGER NOT NULL DEFAULT 0, `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`) PRIMARY KEY(`branchId`)
); );
CREATE INDEX `IDX_branches_noteId` ON `branches` ( CREATE INDEX `IDX_branches_noteId` ON `branches` (
@ -87,12 +82,6 @@ CREATE INDEX `IDX_branches_noteId_parentNoteId` ON `branches` (
`noteId`, `noteId`,
`parentNoteId` `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 CREATE TABLE labels
( (
labelId TEXT not null primary key, labelId TEXT not null primary key,
@ -103,18 +92,11 @@ CREATE TABLE labels
dateCreated TEXT not null, dateCreated TEXT not null,
dateModified TEXT not null, dateModified TEXT not null,
isDeleted INT not null isDeleted INT not null
); , hash TEXT DEFAULT "" NOT NULL);
CREATE INDEX IDX_labels_name_value CREATE INDEX IDX_labels_name_value
on labels (name, value); on labels (name, value);
CREATE INDEX IDX_labels_noteId CREATE INDEX IDX_labels_noteId
on 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" ( CREATE TABLE IF NOT EXISTS "notes" (
`noteId` TEXT NOT NULL, `noteId` TEXT NOT NULL,
`title` TEXT NOT NULL DEFAULT "unnamed", `title` TEXT NOT NULL DEFAULT "unnamed",
@ -124,9 +106,31 @@ CREATE TABLE IF NOT EXISTS "notes" (
`dateCreated` TEXT NOT NULL, `dateCreated` TEXT NOT NULL,
`dateModified` TEXT NOT NULL, `dateModified` TEXT NOT NULL,
type TEXT NOT NULL DEFAULT 'text', 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`) PRIMARY KEY(`noteId`)
); );
CREATE INDEX `IDX_notes_isDeleted` ON `notes` ( CREATE INDEX IDX_branches_parentNoteId ON branches (parentNoteId);
`isDeleted` 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
); );

View file

@ -5,8 +5,8 @@ const dateUtils = require('../services/date_utils');
class Option extends Entity { class Option extends Entity {
static get tableName() { return "options"; } static get tableName() { return "options"; }
static get primaryKeyName() { return "name"; } static get primaryKeyName() { return "optionId"; }
static get hashedProperties() { return ["name", "value"]; } static get hashedProperties() { return ["optionId", "name", "value"]; }
beforeSaving() { beforeSaving() {
super.beforeSaving(); super.beforeSaving();

View file

@ -94,6 +94,12 @@ async function isDbUpToDate() {
} }
async function isUserInitialized() { 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'"); const username = await sql.getValue("SELECT value FROM options WHERE name = 'username'");
return !!username; return !!username;

View file

@ -18,6 +18,7 @@ const log = require('./services/log');
const appInfo = require('./services/app_info'); const appInfo = require('./services/app_info');
const messagingService = require('./services/messaging'); const messagingService = require('./services/messaging');
const utils = require('./services/utils'); const utils = require('./services/utils');
const sqlInit = require('./services/sql_init.js');
const port = normalizePort(config['Network']['port'] || '3000'); const port = normalizePort(config['Network']['port'] || '3000');
app.set('port', port); app.set('port', port);
@ -54,7 +55,7 @@ httpServer.listen(port);
httpServer.on('error', onError); httpServer.on('error', onError);
httpServer.on('listening', onListening); httpServer.on('listening', onListening);
messagingService.init(httpServer, sessionParser); sqlInit.dbReady.then(() => messagingService.init(httpServer, sessionParser));
if (utils.isElectron()) { if (utils.isElectron()) {
const electronRouting = require('./routes/electron'); const electronRouting = require('./routes/electron');