trilium/src/services/sql_init.js

133 lines
4.1 KiB
JavaScript
Raw Normal View History

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');
async function createConnection() {
return await sqlite.open(dataDir.DOCUMENT_PATH, {Promise});
}
let dbReadyResolve = null;
const dbReady = new Promise(async (resolve, reject) => {
dbReadyResolve = resolve;
// no need to create new connection now since DB stays the same all the time
const db = await createConnection();
sql.setDbConnection(db);
initDbConnection();
});
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");
if (!await isDbInitialized()) {
log.info("DB not initialized, please visit setup page to initialize Trilium.");
return;
}
if (!await isDbUpToDate()) {
// avoiding circular dependency
const migrationService = require('./migration');
await migrationService.migrate();
}
log.info("DB ready.");
dbReadyResolve();
});
}
async function createInitialDatabase(username, password) {
log.info("Creating initial database ...");
2018-04-03 10:33:54 +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');
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");
const optionsInitService = require('./options_init');
await optionsInitService.initDocumentOptions();
await optionsInitService.initSyncedOptions(username, password);
await optionsInitService.initNotSyncedOptions(true, startNoteId);
2018-04-03 10:33:54 +08:00
await require('./sync_table').fillAllSyncRows();
});
log.info("Schema and initial content generated.");
2018-04-03 10:33:54 +08:00
await initDbConnection();
}
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);
await require('./options_init').initNotSyncedOptions(false, '', syncServerHost);
});
log.info("Schema and not synced options generated.");
}
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:47:46 +08:00
const upToDate = dbVersion >= appInfo.dbVersion;
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.");
}
return upToDate;
}
module.exports = {
dbReady,
schemaExists,
isDbInitialized,
initDbConnection,
isDbUpToDate,
createInitialDatabase,
createDatabaseForSync
};