2018-04-03 09:25:20 +08:00
|
|
|
const log = require('./log');
|
|
|
|
const dataDir = require('./data_dir');
|
|
|
|
const fs = require('fs');
|
|
|
|
const sqlite = require('sqlite');
|
|
|
|
const resourceDir = require('./resource_dir');
|
|
|
|
const appInfo = require('./app_info');
|
|
|
|
const sql = require('./sql');
|
2018-04-03 10:33:54 +08:00
|
|
|
const cls = require('./cls');
|
2018-04-03 09:25:20 +08:00
|
|
|
|
|
|
|
async function createConnection() {
|
|
|
|
return await sqlite.open(dataDir.DOCUMENT_PATH, {Promise});
|
|
|
|
}
|
|
|
|
|
|
|
|
let dbReadyResolve = null;
|
2018-07-24 03:15:32 +08:00
|
|
|
const dbReady = new Promise(async (resolve, reject) => {
|
2018-07-23 01:56:20 +08:00
|
|
|
dbReadyResolve = resolve;
|
2018-04-03 09:25:20 +08:00
|
|
|
|
2018-07-24 03:15:32 +08:00
|
|
|
// no need to create new connection now since DB stays the same all the time
|
|
|
|
const db = await createConnection();
|
|
|
|
sql.setDbConnection(db);
|
|
|
|
|
2018-07-23 01:56:20 +08:00
|
|
|
initDbConnection();
|
|
|
|
});
|
2018-04-03 09:25:20 +08:00
|
|
|
|
2018-07-24 14:12:36 +08:00
|
|
|
async function schemaExists() {
|
|
|
|
const tableResults = await sql.getRows("SELECT name FROM sqlite_master WHERE type='table' AND name='options'");
|
2018-04-03 09:25:20 +08:00
|
|
|
|
2018-07-23 01:56:20 +08:00
|
|
|
return tableResults.length === 1;
|
|
|
|
}
|
2018-07-21 14:55:24 +08:00
|
|
|
|
2018-07-24 14:12:36 +08:00
|
|
|
async function isDbInitialized() {
|
|
|
|
if (!await schemaExists()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const initialized = await sql.getValue("SELECT value FROM options WHERE name = 'initialized'");
|
|
|
|
|
|
|
|
return initialized === 'true';
|
|
|
|
}
|
|
|
|
|
2018-07-23 01:56:20 +08:00
|
|
|
async function initDbConnection() {
|
|
|
|
await cls.init(async () => {
|
|
|
|
await sql.execute("PRAGMA foreign_keys = ON");
|
2018-04-03 09:25:20 +08:00
|
|
|
|
2018-07-23 04:21:16 +08:00
|
|
|
if (!await isDbInitialized()) {
|
|
|
|
log.info("DB not initialized, please visit setup page to initialize Trilium.");
|
2018-04-03 09:25:20 +08:00
|
|
|
|
2018-04-04 10:15:28 +08:00
|
|
|
return;
|
2018-04-03 09:25:20 +08:00
|
|
|
}
|
2018-04-04 10:15:28 +08:00
|
|
|
|
|
|
|
if (!await isDbUpToDate()) {
|
2018-06-11 03:49:22 +08:00
|
|
|
// avoiding circular dependency
|
|
|
|
const migrationService = require('./migration');
|
|
|
|
|
|
|
|
await migrationService.migrate();
|
2018-04-04 10:15:28 +08:00
|
|
|
}
|
|
|
|
|
2018-07-23 01:56:20 +08:00
|
|
|
log.info("DB ready.");
|
2018-07-24 03:15:32 +08:00
|
|
|
dbReadyResolve();
|
2018-04-03 09:25:20 +08:00
|
|
|
});
|
2018-07-23 01:56:20 +08:00
|
|
|
}
|
2018-04-03 09:25:20 +08:00
|
|
|
|
2018-07-21 14:55:24 +08:00
|
|
|
async function createInitialDatabase(username, password) {
|
2018-07-24 03:15:32 +08:00
|
|
|
log.info("Creating initial database ...");
|
2018-04-03 10:33:54 +08:00
|
|
|
|
2018-07-24 14:12:36 +08:00
|
|
|
if (await isDbInitialized()) {
|
|
|
|
throw new Error("DB is already initialized");
|
|
|
|
}
|
|
|
|
|
2018-04-03 10:33:54 +08:00
|
|
|
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');
|
|
|
|
const imagesSql = fs.readFileSync(resourceDir.DB_INIT_DIR + '/main_images.sql', 'UTF-8');
|
|
|
|
const notesImageSql = fs.readFileSync(resourceDir.DB_INIT_DIR + '/main_note_images.sql', 'UTF-8');
|
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
await sql.transactional(async () => {
|
2018-04-03 10:33:54 +08:00
|
|
|
await sql.executeScript(schema);
|
|
|
|
await sql.executeScript(notesSql);
|
|
|
|
await sql.executeScript(notesTreeSql);
|
|
|
|
await sql.executeScript(imagesSql);
|
|
|
|
await sql.executeScript(notesImageSql);
|
|
|
|
|
|
|
|
const startNoteId = await sql.getValue("SELECT noteId FROM branches WHERE parentNoteId = 'root' AND isDeleted = 0 ORDER BY notePosition");
|
|
|
|
|
2018-07-24 03:15:32 +08:00
|
|
|
const optionsInitService = require('./options_init');
|
|
|
|
|
|
|
|
await optionsInitService.initDocumentOptions();
|
|
|
|
await optionsInitService.initSyncedOptions(username, password);
|
2018-07-24 14:12:36 +08:00
|
|
|
await optionsInitService.initNotSyncedOptions(true, startNoteId);
|
2018-07-24 03:15:32 +08:00
|
|
|
|
2018-04-03 10:33:54 +08:00
|
|
|
await require('./sync_table').fillAllSyncRows();
|
|
|
|
});
|
|
|
|
|
2018-07-24 03:15:32 +08:00
|
|
|
log.info("Schema and initial content generated.");
|
2018-04-03 10:33:54 +08:00
|
|
|
|
2018-07-23 01:56:20 +08:00
|
|
|
await initDbConnection();
|
2018-04-03 09:25:20 +08:00
|
|
|
}
|
|
|
|
|
2018-07-24 03:15:32 +08:00
|
|
|
async function createDatabaseForSync(syncServerHost) {
|
|
|
|
log.info("Creating database for sync with server ...");
|
|
|
|
|
|
|
|
const schema = fs.readFileSync(resourceDir.DB_INIT_DIR + '/schema.sql', 'UTF-8');
|
|
|
|
|
|
|
|
await sql.transactional(async () => {
|
|
|
|
await sql.executeScript(schema);
|
|
|
|
|
2018-07-24 14:12:36 +08:00
|
|
|
await require('./options_init').initNotSyncedOptions(false, '', syncServerHost);
|
2018-07-24 03:15:32 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
log.info("Schema and not synced options generated.");
|
|
|
|
}
|
|
|
|
|
2018-04-03 09:25:20 +08:00
|
|
|
async function isDbUpToDate() {
|
2018-04-03 09:47:46 +08:00
|
|
|
const dbVersion = parseInt(await sql.getValue("SELECT value FROM options WHERE name = 'dbVersion'"));
|
2018-04-03 09:25:20 +08:00
|
|
|
|
2018-04-03 09:47:46 +08:00
|
|
|
const upToDate = dbVersion >= appInfo.dbVersion;
|
2018-04-03 09:25:20 +08:00
|
|
|
|
|
|
|
if (!upToDate) {
|
2018-04-03 09:47:46 +08:00
|
|
|
log.info("App db version is " + appInfo.dbVersion + ", while db version is " + dbVersion + ". Migration needed.");
|
2018-04-03 09:25:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return upToDate;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
dbReady,
|
2018-07-24 14:12:36 +08:00
|
|
|
schemaExists,
|
2018-07-23 01:56:20 +08:00
|
|
|
isDbInitialized,
|
|
|
|
initDbConnection,
|
2018-07-21 14:55:24 +08:00
|
|
|
isDbUpToDate,
|
2018-07-24 03:15:32 +08:00
|
|
|
createInitialDatabase,
|
|
|
|
createDatabaseForSync
|
2018-04-03 09:25:20 +08:00
|
|
|
};
|