separated DB initialization methods into sql_init

This commit is contained in:
azivner 2018-04-02 21:25:20 -04:00
parent 6ab0cea4e3
commit 26e4ad9bf9
11 changed files with 133 additions and 116 deletions

View File

@ -1,7 +1,7 @@
"use strict";
const optionService = require('../../services/options');
const sql = require('../../services/sql');
const sqlInit = require('../../services/sql_init');
const utils = require('../../services/utils');
const myScryptService = require('../../services/my_scrypt');
const passwordEncryptionService = require('../../services/password_encryption');
@ -19,7 +19,7 @@ async function setup(req) {
await passwordEncryptionService.setDataKey(password, utils.randomSecureToken(16));
sql.setDbReadyAsResolved();
sqlInit.setDbReadyAsResolved();
}
module.exports = {

View File

@ -2,16 +2,17 @@
const migrationService = require('./migration');
const sql = require('./sql');
const sqlInit = require('./sql_init');
const utils = require('./utils');
async function checkAuth(req, res, next) {
if (!await sql.isUserInitialized()) {
if (!await sqlInit.isUserInitialized()) {
res.redirect("setup");
}
else if (!req.session.loggedIn && !utils.isElectron()) {
res.redirect("login");
}
else if (!await sql.isDbUpToDate()) {
else if (!await sqlInit.isDbUpToDate()) {
res.redirect("migration");
}
else {
@ -34,7 +35,7 @@ async function checkApiAuthOrElectron(req, res, next) {
if (!req.session.loggedIn && !utils.isElectron()) {
res.status(401).send("Not authorized");
}
else if (await sql.isDbUpToDate()) {
else if (await sqlInit.isDbUpToDate()) {
next();
}
else {
@ -46,7 +47,7 @@ async function checkApiAuth(req, res, next) {
if (!req.session.loggedIn) {
res.status(401).send("Not authorized");
}
else if (await sql.isDbUpToDate()) {
else if (await sqlInit.isDbUpToDate()) {
next();
}
else {
@ -64,7 +65,7 @@ async function checkApiAuthForMigrationPage(req, res, next) {
}
async function checkAppNotInitialized(req, res, next) {
if (await sql.isUserInitialized()) {
if (await sqlInit.isUserInitialized()) {
res.status(400).send("App already initialized.");
}
else {
@ -78,7 +79,7 @@ async function checkSenderToken(req, res, next) {
if (await sql.getValue("SELECT COUNT(*) FROM api_tokens WHERE isDeleted = 0 AND token = ?", [token]) === 0) {
res.status(401).send("Not authorized");
}
else if (await sql.isDbUpToDate()) {
else if (await sqlInit.isDbUpToDate()) {
next();
}
else {

View File

@ -5,7 +5,7 @@ const optionService = require('./options');
const fs = require('fs-extra');
const dataDir = require('./data_dir');
const log = require('./log');
const sql = require('./sql');
const sqlInit = require('./sql_init');
const syncMutexService = require('./sync_mutex');
const cls = require('./cls');
@ -60,7 +60,7 @@ if (!fs.existsSync(dataDir.BACKUP_DIR)) {
fs.mkdirSync(dataDir.BACKUP_DIR, 0o700);
}
sql.dbReady.then(() => {
sqlInit.dbReady.then(() => {
setInterval(cls.wrap(regularBackup), 60 * 60 * 1000);
// kickoff backup immediately

View File

@ -1,6 +1,7 @@
"use strict";
const sql = require('./sql');
const sqlInit = require('./sql_init');
const log = require('./log');
const messagingService = require('./messaging');
const syncMutexService = require('./sync_mutex');
@ -265,7 +266,7 @@ async function runChecks() {
}
}
sql.dbReady.then(() => {
sqlInit.dbReady.then(() => {
setInterval(cls.wrap(runChecks), 60 * 60 * 1000);
// kickoff backup immediately

View File

@ -1,5 +1,6 @@
const backupService = require('./backup');
const sql = require('./sql');
const sqlInit = require('./sql_init');
const optionService = require('./options');
const fs = require('fs-extra');
const log = require('./log');
@ -84,8 +85,8 @@ async function migrate() {
}
}
if (sql.isDbUpToDate()) {
sql.setDbReadyAsResolved();
if (sqlInit.isDbUpToDate()) {
sqlInit.setDbReadyAsResolved();
}
return migrations;

View File

@ -19,8 +19,8 @@ async function runNotesWithLabel(runAttrValue) {
}
}
setTimeout(cls.wrap(() => runNotesWithLabel('backend_startup')), 10 * 1000);
setTimeout(() => cls.wrap(() => runNotesWithLabel('backend_startup')), 10 * 1000);
setInterval(cls.wrap(() => runNotesWithLabel('hourly')), 3600 * 1000);
setInterval(() => cls.wrap(() => runNotesWithLabel('hourly')), 3600 * 1000);
setInterval(cls.wrap(() => runNotesWithLabel('daily'), 24 * 3600 * 1000));
setInterval(() => cls.wrap(() => runNotesWithLabel('daily'), 24 * 3600 * 1000));

View File

@ -2,6 +2,7 @@ const utils = require('./utils');
const dateUtils = require('./date_utils');
const log = require('./log');
const sql = require('./sql');
const sqlInit = require('./sql_init');
const cls = require('./cls');
async function saveSourceId(sourceId) {
@ -41,7 +42,7 @@ function isLocalSourceId(srcId) {
const currentSourceId = createSourceId();
// this will also refresh source IDs
sql.dbReady.then(cls.wrap(() => saveSourceId(currentSourceId)));
sqlInit.dbReady.then(cls.wrap(() => saveSourceId(currentSourceId)));
function getCurrentSourceId() {
return currentSourceId;

View File

@ -1,80 +1,12 @@
"use strict";
const log = require('./log');
const dataDir = require('./data_dir');
const fs = require('fs');
const sqlite = require('sqlite');
const appInfo = require('./app_info');
const resourceDir = require('./resource_dir');
const cls = require('./cls');
async function createConnection() {
return await sqlite.open(dataDir.DOCUMENT_PATH, {Promise});
}
let dbConnection;
const dbConnected = createConnection();
let dbReadyResolve = null;
const dbReady = new Promise((resolve, reject) => {
dbConnected.then(cls.wrap(async db => {
await execute("PRAGMA foreign_keys = ON");
dbReadyResolve = () => {
log.info("DB ready.");
resolve(db);
};
const tableResults = await getRows("SELECT name FROM sqlite_master WHERE type='table' AND name='notes'");
if (tableResults.length !== 1) {
log.info("Connected to db, but schema doesn't exist. Initializing schema ...");
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 doInTransaction(async () => {
await executeScript(schema);
await executeScript(notesSql);
await executeScript(notesTreeSql);
await executeScript(imagesSql);
await executeScript(notesImageSql);
const startNoteId = await getValue("SELECT noteId FROM branches WHERE parentNoteId = 'root' AND isDeleted = 0 ORDER BY notePosition");
await require('./options').initOptions(startNoteId);
await require('./sync_table').fillAllSyncRows();
});
log.info("Schema and initial content generated. Waiting for user to enter username/password to finish setup.");
// we don't resolve dbReady promise because user needs to setup the username and password to initialize
// the database
}
else {
if (!await isUserInitialized()) {
log.info("Login/password not initialized. DB not ready.");
return;
}
if (!await isDbUpToDate()) {
return;
}
resolve(db);
}
}))
.catch(e => {
console.log("Error connecting to DB.", e);
process.exit(1);
});
});
function setDbReadyAsResolved() {
dbReadyResolve();
function setDbConnection(connection) {
dbConnection = connection;
}
async function insert(table_name, rec, replace = false) {
@ -174,10 +106,9 @@ async function executeScript(query) {
async function wrap(func) {
const thisError = new Error();
const db = await dbConnected;
try {
return await func(db);
return await func(dbConnection);
}
catch (e) {
log.error("Error executing query. Inner exception: " + e.stack + thisError.stack);
@ -238,27 +169,8 @@ async function doInTransaction(func) {
return ret;
}
async function isDbUpToDate() {
const dbVersion = parseInt(await getValue("SELECT value FROM options WHERE name = 'db_version'"));
const upToDate = dbVersion >= appInfo.db_version;
if (!upToDate) {
log.info("App db version is " + appInfo.db_version + ", while db version is " + dbVersion + ". Migration needed.");
}
return upToDate;
}
async function isUserInitialized() {
const username = await getValue("SELECT value FROM options WHERE name = 'username'");
return !!username;
}
module.exports = {
dbReady,
isUserInitialized,
setDbConnection,
insert,
replace,
getValue,
@ -269,7 +181,5 @@ module.exports = {
getColumn,
execute,
executeScript,
doInTransaction,
setDbReadyAsResolved,
isDbUpToDate
doInTransaction
};

101
src/services/sql_init.js Normal file
View File

@ -0,0 +1,101 @@
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');
async function createConnection() {
return await sqlite.open(dataDir.DOCUMENT_PATH, {Promise});
}
let dbReadyResolve = null;
const dbReady = new Promise((resolve, reject) => {
createConnection().then(async db => {
sql.setDbConnection(db);
await sql.execute("PRAGMA foreign_keys = ON");
dbReadyResolve = () => {
log.info("DB ready.");
resolve(db);
};
const tableResults = await sql.getRows("SELECT name FROM sqlite_master WHERE type='table' AND name='notes'");
if (tableResults.length !== 1) {
log.info("Connected to db, but schema doesn't exist. Initializing schema ...");
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.doInTransaction(async () => {
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");
await require('./options').initOptions(startNoteId);
await require('./sync_table').fillAllSyncRows();
});
log.info("Schema and initial content generated. Waiting for user to enter username/password to finish setup.");
// we don't resolve dbReady promise because user needs to setup the username and password to initialize
// the database
}
else {
if (!await isUserInitialized()) {
log.info("Login/password not initialized. DB not ready.");
return;
}
if (!await isDbUpToDate()) {
return;
}
resolve(db);
}
})
.catch(e => {
console.log("Error connecting to DB.", e);
process.exit(1);
});
});
function setDbReadyAsResolved() {
dbReadyResolve();
}
async function isDbUpToDate() {
const dbVersion = parseInt(await sql.getValue("SELECT value FROM options WHERE name = 'db_version'"));
const upToDate = dbVersion >= appInfo.db_version;
if (!upToDate) {
log.info("App db version is " + appInfo.db_version + ", while db version is " + dbVersion + ". Migration needed.");
}
return upToDate;
}
async function isUserInitialized() {
const username = await sql.getValue("SELECT value FROM options WHERE name = 'username'");
return !!username;
}
module.exports = {
dbReady,
isUserInitialized,
setDbReadyAsResolved,
isDbUpToDate
};

View File

@ -3,6 +3,7 @@
const log = require('./log');
const rp = require('request-promise');
const sql = require('./sql');
const sqlInit = require('./sql_init');
const optionService = require('./options');
const utils = require('./utils');
const sourceIdService = require('./source_id');
@ -23,7 +24,7 @@ let syncServerCertificate = null;
async function sync() {
try {
await syncMutexService.doExclusively(async () => {
if (!await sql.isDbUpToDate()) {
if (!await sqlInit.isDbUpToDate()) {
return {
success: false,
message: "DB not up to date"
@ -330,7 +331,7 @@ async function syncRequest(syncContext, method, uri, body) {
}
}
sql.dbReady.then(() => {
sqlInit.dbReady.then(() => {
if (syncSetup.isSyncSetup) {
log.info("Setting up sync to " + syncSetup.SYNC_SERVER + " with timeout " + syncSetup.SYNC_TIMEOUT);

View File

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