fixes for DB initalization and import of demo data

This commit is contained in:
azivner 2017-12-23 13:55:13 -05:00
parent cf8334607e
commit 79a4a6eb01
8 changed files with 47 additions and 42 deletions

View File

@ -42,7 +42,7 @@ router.post('/sync', async (req, res, next) => {
req.session.loggedIn = true;
res.send({
sourceId: await source_id.getCurrentSourceId()
sourceId: source_id.getCurrentSourceId()
});
});

View File

@ -24,10 +24,7 @@ router.post('/now', auth.checkApiAuth, async (req, res, next) => {
router.post('/fill-sync-rows', auth.checkApiAuth, async (req, res, next) => {
await sql.doInTransaction(async () => {
await sync_table.fillSyncRows("notes", "note_id");
await sync_table.fillSyncRows("notes_tree", "note_tree_id");
await sync_table.fillSyncRows("notes_history", "note_history_id");
await sync_table.fillSyncRows("recent_notes", "note_tree_id");
await sync_table.fillAllSyncRows();
});
log.info("Sync rows have been filled.");

View File

@ -3,8 +3,9 @@ const utils = require('./utils');
const sync_table = require('./sync_table');
const app_info = require('./app_info');
const SYNCED_OPTIONS = [ 'username', 'password_verification_hash', 'encrypted_data_key', 'protected_session_timeout',
'history_snapshot_time_interval' ];
const SYNCED_OPTIONS = [ 'username', 'password_verification_hash', 'password_verification_salt',
'password_derived_key_salt', 'encrypted_data_key', 'encrypted_data_key_iv',
'protected_session_timeout', 'history_snapshot_time_interval' ];
async function getOption(optName) {
const row = await sql.getFirstOrNull("SELECT opt_value FROM options WHERE opt_name = ?", [optName]);

View File

@ -2,11 +2,7 @@ const utils = require('./utils');
const log = require('./log');
const sql = require('./sql');
async function generateSourceId() {
const sourceId = utils.randomString(12);
log.info("Generated sourceId=" + sourceId);
async function saveSourceId(sourceId) {
await sql.doInTransaction(async () => {
await sql.insert("source_ids", {
source_id: sourceId,
@ -15,6 +11,19 @@ async function generateSourceId() {
});
await refreshSourceIds();
}
function createSourceId() {
const sourceId = utils.randomString(12);
log.info("Generated sourceId=" + sourceId);
return sourceId;
}
async function generateSourceId() {
const sourceId = createSourceId();
await saveSourceId(sourceId);
return sourceId;
}
@ -25,16 +34,17 @@ async function refreshSourceIds() {
let allSourceIds = [];
sql.dbReady.then(refreshSourceIds);
function isLocalSourceId(srcId) {
return allSourceIds.includes(srcId);
}
const currentSourceIdPromise = generateSourceId();
const currentSourceId = createSourceId();
async function getCurrentSourceId() {
return await currentSourceIdPromise;
// this will also refresh source IDs
sql.dbReady.then(() => saveSourceId(currentSourceId));
function getCurrentSourceId() {
return currentSourceId;
}
module.exports = {

View File

@ -28,36 +28,23 @@ const dbReady = new Promise((resolve, reject) => {
if (tableResults.length !== 1) {
log.info("Connected to db, but schema doesn't exist. Initializing schema ...");
const schema = fs.readFileSync('schema.sql', 'UTF-8');
const schema = fs.readFileSync('db/schema.sql', 'UTF-8');
const notesSql = fs.readFileSync('db/main_notes.sql', 'UTF-8');
const notesTreeSql = fs.readFileSync('db/main_notes_tree.sql', 'UTF-8');
await doInTransaction(async () => {
await executeScript(schema);
await executeScript(notesSql);
await executeScript(notesTreeSql);
const noteId = utils.newNoteId();
const now = utils.nowDate();
await insert('notes_tree', {
note_tree_id: utils.newNoteTreeId(),
note_id: noteId,
parent_note_id: 'root',
note_position: 1,
is_deleted: 0,
date_modified: now
});
await insert('notes', {
note_id: noteId,
note_title: 'Welcome to Trilium!',
note_text: 'Text',
is_protected: 0,
is_deleted: 0,
date_created: now,
date_modified: now
});
const noteId = await getFirstValue("SELECT note_id FROM notes_tree WHERE parent_note_id = 'root' ORDER BY note_position");
await require('./options').initOptions(noteId);
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
}

View File

@ -235,7 +235,7 @@ async function pushEntity(sync, syncContext) {
log.info(`Pushing changes in sync #${sync.id} ${sync.entity_name} ${sync.entity_id}`);
const payload = {
sourceId: await source_id.getCurrentSourceId(),
sourceId: source_id.getCurrentSourceId(),
entity: entity
};

View File

@ -33,7 +33,7 @@ async function addEntitySync(entityName, entityId, sourceId) {
entity_name: entityName,
entity_id: entityId,
sync_date: utils.nowDate(),
source_id: sourceId || await source_id.getCurrentSourceId()
source_id: sourceId || source_id.getCurrentSourceId()
});
if (!sync_setup.isSyncSetup) {
@ -73,6 +73,13 @@ async function fillSyncRows(entityName, entityKey) {
}
}
async function fillAllSyncRows() {
await fillSyncRows("notes", "note_id");
await fillSyncRows("notes_tree", "note_tree_id");
await fillSyncRows("notes_history", "note_history_id");
await fillSyncRows("recent_notes", "note_tree_id");
}
module.exports = {
addNoteSync,
addNoteTreeSync,
@ -81,5 +88,5 @@ module.exports = {
addOptionsSync,
addRecentNoteSync,
cleanupSyncRowsForMissingEntities,
fillSyncRows
fillAllSyncRows
};

View File

@ -31,6 +31,9 @@
<script type="text/javascript">
const baseApiUrl = 'api/';
const glob = {
sourceId: ''
};
</script>
<!-- Required for correct loading of scripts in Electron -->