| 
									
										
										
										
											2018-04-02 21:25:20 -04: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-02 22:33:54 -04:00
										 |  |  | const cls = require('./cls'); | 
					
						
							| 
									
										
										
										
											2018-04-02 21:25:20 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | async function createConnection() { | 
					
						
							|  |  |  |     return await sqlite.open(dataDir.DOCUMENT_PATH, {Promise}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-03 22:15:28 -04:00
										 |  |  | let schemaReadyResolve = null; | 
					
						
							|  |  |  | const schemaReady = new Promise((resolve, reject) => schemaReadyResolve = resolve); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-02 21:25:20 -04:00
										 |  |  | let dbReadyResolve = null; | 
					
						
							|  |  |  | const dbReady = new Promise((resolve, reject) => { | 
					
						
							| 
									
										
										
										
											2018-04-02 22:33:54 -04:00
										 |  |  |     cls.init(async () => { | 
					
						
							|  |  |  |         const db = await createConnection(); | 
					
						
							| 
									
										
										
										
											2018-04-02 21:25:20 -04:00
										 |  |  |         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) { | 
					
						
							| 
									
										
										
										
											2018-04-02 22:33:54 -04:00
										 |  |  |             await createInitialDatabase(); | 
					
						
							| 
									
										
										
										
											2018-04-02 21:25:20 -04:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-03 22:15:28 -04:00
										 |  |  |         schemaReadyResolve(); | 
					
						
							| 
									
										
										
										
											2018-04-02 21:25:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-03 22:15:28 -04:00
										 |  |  |         if (!await isUserInitialized()) { | 
					
						
							|  |  |  |             log.info("Login/password not initialized. DB not ready."); | 
					
						
							| 
									
										
										
										
											2018-04-02 21:25:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-03 22:15:28 -04:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2018-04-02 21:25:20 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-04-03 22:15:28 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (!await isDbUpToDate()) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         resolve(db); | 
					
						
							| 
									
										
										
										
											2018-04-02 21:25:20 -04:00
										 |  |  |     }); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-02 22:33:54 -04:00
										 |  |  | async function createInitialDatabase() { | 
					
						
							|  |  |  |     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'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  |     await sql.transactional(async () => { | 
					
						
							| 
									
										
										
										
											2018-04-02 22:33:54 -04: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"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         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
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-02 21:25:20 -04:00
										 |  |  | function setDbReadyAsResolved() { | 
					
						
							|  |  |  |     dbReadyResolve(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function isDbUpToDate() { | 
					
						
							| 
									
										
										
										
											2018-04-02 21:47:46 -04:00
										 |  |  |     const dbVersion = parseInt(await sql.getValue("SELECT value FROM options WHERE name = 'dbVersion'")); | 
					
						
							| 
									
										
										
										
											2018-04-02 21:25:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-02 21:47:46 -04:00
										 |  |  |     const upToDate = dbVersion >= appInfo.dbVersion; | 
					
						
							| 
									
										
										
										
											2018-04-02 21:25:20 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!upToDate) { | 
					
						
							| 
									
										
										
										
											2018-04-02 21:47:46 -04:00
										 |  |  |         log.info("App db version is " + appInfo.dbVersion + ", while db version is " + dbVersion + ". Migration needed."); | 
					
						
							| 
									
										
										
										
											2018-04-02 21:25:20 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return upToDate; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function isUserInitialized() { | 
					
						
							|  |  |  |     const username = await sql.getValue("SELECT value FROM options WHERE name = 'username'"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return !!username; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |     dbReady, | 
					
						
							| 
									
										
										
										
											2018-04-03 22:15:28 -04:00
										 |  |  |     schemaReady, | 
					
						
							| 
									
										
										
										
											2018-04-02 21:25:20 -04:00
										 |  |  |     isUserInitialized, | 
					
						
							|  |  |  |     setDbReadyAsResolved, | 
					
						
							|  |  |  |     isDbUpToDate | 
					
						
							|  |  |  | }; |