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; req.session.loggedIn = true;
res.send({ 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) => { router.post('/fill-sync-rows', auth.checkApiAuth, async (req, res, next) => {
await sql.doInTransaction(async () => { await sql.doInTransaction(async () => {
await sync_table.fillSyncRows("notes", "note_id"); await sync_table.fillAllSyncRows();
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");
}); });
log.info("Sync rows have been filled."); log.info("Sync rows have been filled.");

View File

@ -3,8 +3,9 @@ const utils = require('./utils');
const sync_table = require('./sync_table'); const sync_table = require('./sync_table');
const app_info = require('./app_info'); const app_info = require('./app_info');
const SYNCED_OPTIONS = [ 'username', 'password_verification_hash', 'encrypted_data_key', 'protected_session_timeout', const SYNCED_OPTIONS = [ 'username', 'password_verification_hash', 'password_verification_salt',
'history_snapshot_time_interval' ]; 'password_derived_key_salt', 'encrypted_data_key', 'encrypted_data_key_iv',
'protected_session_timeout', 'history_snapshot_time_interval' ];
async function getOption(optName) { async function getOption(optName) {
const row = await sql.getFirstOrNull("SELECT opt_value FROM options WHERE opt_name = ?", [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 log = require('./log');
const sql = require('./sql'); const sql = require('./sql');
async function generateSourceId() { async function saveSourceId(sourceId) {
const sourceId = utils.randomString(12);
log.info("Generated sourceId=" + sourceId);
await sql.doInTransaction(async () => { await sql.doInTransaction(async () => {
await sql.insert("source_ids", { await sql.insert("source_ids", {
source_id: sourceId, source_id: sourceId,
@ -15,6 +11,19 @@ async function generateSourceId() {
}); });
await refreshSourceIds(); 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; return sourceId;
} }
@ -25,16 +34,17 @@ async function refreshSourceIds() {
let allSourceIds = []; let allSourceIds = [];
sql.dbReady.then(refreshSourceIds);
function isLocalSourceId(srcId) { function isLocalSourceId(srcId) {
return allSourceIds.includes(srcId); return allSourceIds.includes(srcId);
} }
const currentSourceIdPromise = generateSourceId(); const currentSourceId = createSourceId();
async function getCurrentSourceId() { // this will also refresh source IDs
return await currentSourceIdPromise; sql.dbReady.then(() => saveSourceId(currentSourceId));
function getCurrentSourceId() {
return currentSourceId;
} }
module.exports = { module.exports = {

View File

@ -28,36 +28,23 @@ const dbReady = new Promise((resolve, reject) => {
if (tableResults.length !== 1) { if (tableResults.length !== 1) {
log.info("Connected to db, but schema doesn't exist. Initializing schema ..."); 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 doInTransaction(async () => {
await executeScript(schema); await executeScript(schema);
await executeScript(notesSql);
await executeScript(notesTreeSql);
const noteId = utils.newNoteId(); const noteId = await getFirstValue("SELECT note_id FROM notes_tree WHERE parent_note_id = 'root' ORDER BY note_position");
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
});
await require('./options').initOptions(noteId); 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 // we don't resolve dbReady promise because user needs to setup the username and password to initialize
// the database // 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}`); log.info(`Pushing changes in sync #${sync.id} ${sync.entity_name} ${sync.entity_id}`);
const payload = { const payload = {
sourceId: await source_id.getCurrentSourceId(), sourceId: source_id.getCurrentSourceId(),
entity: entity entity: entity
}; };

View File

@ -33,7 +33,7 @@ async function addEntitySync(entityName, entityId, sourceId) {
entity_name: entityName, entity_name: entityName,
entity_id: entityId, entity_id: entityId,
sync_date: utils.nowDate(), sync_date: utils.nowDate(),
source_id: sourceId || await source_id.getCurrentSourceId() source_id: sourceId || source_id.getCurrentSourceId()
}); });
if (!sync_setup.isSyncSetup) { 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 = { module.exports = {
addNoteSync, addNoteSync,
addNoteTreeSync, addNoteTreeSync,
@ -81,5 +88,5 @@ module.exports = {
addOptionsSync, addOptionsSync,
addRecentNoteSync, addRecentNoteSync,
cleanupSyncRowsForMissingEntities, cleanupSyncRowsForMissingEntities,
fillSyncRows fillAllSyncRows
}; };

View File

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