Notes/src/services/sql_init.js

160 lines
4.5 KiB
JavaScript
Raw Normal View History

const log = require('./log');
const fs = require('fs');
const resourceDir = require('./resource_dir');
const sql = require('./sql');
const utils = require('./utils');
const optionService = require('./options');
const port = require('./port');
const Option = require('../entities/option');
const TaskContext = require('./task_context.js');
2020-07-02 22:57:17 +02:00
const migrationService = require('./migration');
const cls = require('./cls');
const config = require('./config');
2020-06-20 21:42:41 +02:00
const dbReady = utils.deferred();
cls.init(initDbConnection);
2020-06-20 12:31:38 +02:00
function schemaExists() {
2020-06-20 21:42:41 +02:00
return !!sql.getValue(`SELECT name FROM sqlite_master
WHERE type = 'table' AND name = 'options'`);
}
2020-06-20 12:31:38 +02:00
function isDbInitialized() {
if (!schemaExists()) {
return false;
}
2020-06-20 12:31:38 +02:00
const initialized = sql.getValue("SELECT value FROM options WHERE name = 'initialized'");
2020-06-20 21:42:41 +02:00
return initialized === 'true';
}
2020-07-02 22:57:17 +02:00
async function initDbConnection() {
2020-06-20 21:42:41 +02:00
if (!isDbInitialized()) {
2020-09-06 20:58:26 +02:00
log.info(`DB not initialized, please visit setup page` + (utils.isElectron() ? '' : ` - http://[your-server-host]:${await port} to see instructions on how to initialize Trilium.`));
2020-06-20 21:42:41 +02:00
return;
}
2020-07-02 22:57:17 +02:00
await migrationService.migrateIfNecessary();
2020-06-20 21:42:41 +02:00
require('./options_init').initStartupOptions();
sql.execute('CREATE TEMP TABLE "param_list" (`paramId` TEXT NOT NULL PRIMARY KEY)');
2020-06-20 21:42:41 +02:00
dbReady.resolve();
}
2020-07-02 21:08:18 +02:00
async function createInitialDatabase(username, password, theme) {
log.info("Creating initial database ...");
2018-04-02 22:33:54 -04:00
2020-06-20 12:31:38 +02:00
if (isDbInitialized()) {
throw new Error("DB is already initialized");
}
2018-04-02 22:33:54 -04:00
const schema = fs.readFileSync(resourceDir.DB_INIT_DIR + '/schema.sql', 'UTF-8');
2020-03-26 17:05:17 +01:00
const demoFile = fs.readFileSync(resourceDir.DB_INIT_DIR + '/demo.zip');
2018-04-02 22:33:54 -04:00
2020-07-02 21:08:18 +02:00
let rootNote;
2020-06-20 12:31:38 +02:00
sql.transactional(() => {
sql.executeScript(schema);
const Note = require("../entities/note");
const Branch = require("../entities/branch");
2020-07-02 21:08:18 +02:00
rootNote = new Note({
noteId: 'root',
title: 'root',
type: 'text',
mime: 'text/html'
}).save();
2020-06-20 12:31:38 +02:00
rootNote.setContent('');
2019-02-20 23:07:57 +01:00
2020-06-20 12:31:38 +02:00
new Branch({
branchId: 'root',
noteId: 'root',
parentNoteId: 'none',
isExpanded: true,
2019-10-19 12:36:16 +02:00
notePosition: 10
}).save();
2020-07-02 21:08:18 +02:00
});
const dummyTaskContext = new TaskContext("initial-demo-import", 'import', false);
2020-07-02 21:08:18 +02:00
const zipImportService = require("./import/zip");
await zipImportService.importZip(dummyTaskContext, demoFile, rootNote);
2019-02-24 12:24:28 +01:00
2020-07-02 21:08:18 +02:00
sql.transactional(() => {
2020-06-20 12:31:38 +02:00
const startNoteId = sql.getValue("SELECT noteId FROM branches WHERE parentNoteId = 'root' AND isDeleted = 0 ORDER BY notePosition");
2018-04-02 22:33:54 -04:00
const optionsInitService = require('./options_init');
2020-06-20 12:31:38 +02:00
optionsInitService.initDocumentOptions();
optionsInitService.initSyncedOptions(username, password);
optionsInitService.initNotSyncedOptions(true, startNoteId, { theme });
2018-04-02 22:33:54 -04:00
});
log.info("Schema and initial content generated.");
2018-04-02 22:33:54 -04:00
2020-06-20 12:31:38 +02:00
initDbConnection();
}
2020-06-20 12:31:38 +02:00
function createDatabaseForSync(options, syncServerHost = '', syncProxy = '') {
log.info("Creating database for sync");
2020-06-20 12:31:38 +02:00
if (isDbInitialized()) {
throw new Error("DB is already initialized");
}
const schema = fs.readFileSync(resourceDir.DB_INIT_DIR + '/schema.sql', 'UTF-8');
2020-06-20 12:31:38 +02:00
sql.transactional(() => {
sql.executeScript(schema);
2020-06-20 12:31:38 +02:00
require('./options_init').initNotSyncedOptions(false, 'root', { syncServerHost, syncProxy });
// document options required for sync to kick off
for (const opt of options) {
2020-06-20 12:31:38 +02:00
new Option(opt).save();
}
});
log.info("Schema and not synced options generated.");
}
2020-06-20 21:42:41 +02:00
function setDbAsInitialized() {
2020-06-20 12:31:38 +02:00
if (!isDbInitialized()) {
optionService.setOption('initialized', 'true');
2020-06-20 12:31:38 +02:00
initDbConnection();
2019-11-30 11:36:36 +01:00
}
}
2020-07-02 22:57:17 +02:00
dbReady.then(() => {
if (config.General && config.General.noBackup === true) {
log.info("Disabling scheduled backups.");
return;
}
2020-07-02 22:57:17 +02:00
setInterval(() => require('./backup').regularBackup(), 4 * 60 * 60 * 1000);
// kickoff first backup soon after start up
setTimeout(() => require('./backup').regularBackup(), 5 * 60 * 1000);
});
2020-06-20 12:31:38 +02:00
log.info("DB size: " + sql.getValue("SELECT page_count * page_size / 1000 as size FROM pragma_page_count(), pragma_page_size()") + " KB");
2019-01-15 20:00:24 +01:00
module.exports = {
dbReady,
schemaExists,
isDbInitialized,
initDbConnection,
createInitialDatabase,
createDatabaseForSync,
2020-06-20 21:42:41 +02:00
setDbAsInitialized
2020-06-17 23:03:46 +02:00
};