mirror of
https://github.com/zadam/trilium.git
synced 2025-11-16 18:25:51 +01:00
#98, new option "initialized" which indicates if setup has been finished
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
const build = require('./build');
|
||||
const packageJson = require('../../package');
|
||||
|
||||
const APP_DB_VERSION = 102;
|
||||
const APP_DB_VERSION = 103;
|
||||
const SYNC_VERSION = 1;
|
||||
|
||||
module.exports = {
|
||||
|
||||
@@ -29,7 +29,7 @@ async function initSyncedOptions(username, password) {
|
||||
await passwordEncryptionService.setDataKey(password, utils.randomSecureToken(16));
|
||||
}
|
||||
|
||||
async function initNotSyncedOptions(startNotePath = '', syncServerHost = '') {
|
||||
async function initNotSyncedOptions(initialized, startNotePath = '', syncServerHost = '') {
|
||||
await optionService.createOption('startNotePath', startNotePath, false);
|
||||
await optionService.createOption('lastBackupDate', dateUtils.nowDate(), false);
|
||||
await optionService.createOption('dbVersion', appInfo.dbVersion, false);
|
||||
@@ -43,6 +43,8 @@ async function initNotSyncedOptions(startNotePath = '', syncServerHost = '') {
|
||||
await optionService.createOption('syncServerHost', syncServerHost, false);
|
||||
await optionService.createOption('syncServerTimeout', 5000, false);
|
||||
await optionService.createOption('syncProxy', '', false);
|
||||
|
||||
await optionService.createOption('initialized', initialized ? 'true' : 'false', false);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
66
src/services/setup.js
Normal file
66
src/services/setup.js
Normal file
@@ -0,0 +1,66 @@
|
||||
const sqlInit = require('./sql_init');
|
||||
const sql = require('./sql');
|
||||
const rp = require('request-promise');
|
||||
const Option = require('../entities/option');
|
||||
const syncService = require('./sync');
|
||||
const log = require('./log');
|
||||
const optionService = require('./options');
|
||||
|
||||
function triggerSync() {
|
||||
// it's ok to not wait for it here
|
||||
syncService.sync().then(async () => {
|
||||
await optionService.setOption('initialized', 'true');
|
||||
});
|
||||
}
|
||||
|
||||
async function setupSyncFromSyncServer(serverAddress, username, password) {
|
||||
if (await sqlInit.isDbInitialized()) {
|
||||
return {
|
||||
result: 'failure',
|
||||
error: 'DB is already initialized.'
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
log.info("Getting document options from sync server.");
|
||||
|
||||
// response is expected to contain documentId and documentSecret options
|
||||
const options = await rp.get({
|
||||
uri: serverAddress + '/api/sync/document',
|
||||
auth: {
|
||||
'user': username,
|
||||
'pass': password
|
||||
},
|
||||
json: true
|
||||
});
|
||||
|
||||
log.info("Creating database for sync");
|
||||
|
||||
await sql.transactional(async () => {
|
||||
await sqlInit.createDatabaseForSync(serverAddress);
|
||||
|
||||
for (const opt of options) {
|
||||
await new Option(opt).save();
|
||||
}
|
||||
});
|
||||
|
||||
log.info("Triggering sync.");
|
||||
|
||||
triggerSync();
|
||||
|
||||
return { result: 'success' };
|
||||
}
|
||||
catch (e) {
|
||||
log.error("Sync failed: " + e.message);
|
||||
|
||||
return {
|
||||
result: 'failure',
|
||||
error: e.message
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
setupSyncFromSyncServer,
|
||||
triggerSync
|
||||
};
|
||||
@@ -22,12 +22,22 @@ const dbReady = new Promise(async (resolve, reject) => {
|
||||
initDbConnection();
|
||||
});
|
||||
|
||||
async function isDbInitialized() {
|
||||
const tableResults = await sql.getRows("SELECT name FROM sqlite_master WHERE type='table' AND name='notes'");
|
||||
async function schemaExists() {
|
||||
const tableResults = await sql.getRows("SELECT name FROM sqlite_master WHERE type='table' AND name='options'");
|
||||
|
||||
return tableResults.length === 1;
|
||||
}
|
||||
|
||||
async function isDbInitialized() {
|
||||
if (!await schemaExists()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const initialized = await sql.getValue("SELECT value FROM options WHERE name = 'initialized'");
|
||||
|
||||
return initialized === 'true';
|
||||
}
|
||||
|
||||
async function initDbConnection() {
|
||||
await cls.init(async () => {
|
||||
await sql.execute("PRAGMA foreign_keys = ON");
|
||||
@@ -53,6 +63,10 @@ async function initDbConnection() {
|
||||
async function createInitialDatabase(username, password) {
|
||||
log.info("Creating initial database ...");
|
||||
|
||||
if (await isDbInitialized()) {
|
||||
throw new Error("DB is already initialized");
|
||||
}
|
||||
|
||||
const schema = fs.readFileSync(resourceDir.DB_INIT_DIR + '/schema.sql', 'UTF-8');
|
||||
const notesSql = fs.readFileSync(resourceDir.DB_INIT_DIR + '/main_notes.sql', 'UTF-8');
|
||||
const notesTreeSql = fs.readFileSync(resourceDir.DB_INIT_DIR + '/main_branches.sql', 'UTF-8');
|
||||
@@ -72,7 +86,7 @@ async function createInitialDatabase(username, password) {
|
||||
|
||||
await optionsInitService.initDocumentOptions();
|
||||
await optionsInitService.initSyncedOptions(username, password);
|
||||
await optionsInitService.initNotSyncedOptions(startNoteId);
|
||||
await optionsInitService.initNotSyncedOptions(true, startNoteId);
|
||||
|
||||
await require('./sync_table').fillAllSyncRows();
|
||||
});
|
||||
@@ -90,7 +104,7 @@ async function createDatabaseForSync(syncServerHost) {
|
||||
await sql.transactional(async () => {
|
||||
await sql.executeScript(schema);
|
||||
|
||||
await require('./options_init').initNotSyncedOptions('', syncServerHost);
|
||||
await require('./options_init').initNotSyncedOptions(false, '', syncServerHost);
|
||||
});
|
||||
|
||||
log.info("Schema and not synced options generated.");
|
||||
@@ -110,6 +124,7 @@ async function isDbUpToDate() {
|
||||
|
||||
module.exports = {
|
||||
dbReady,
|
||||
schemaExists,
|
||||
isDbInitialized,
|
||||
initDbConnection,
|
||||
isDbUpToDate,
|
||||
|
||||
Reference in New Issue
Block a user