mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 01:48:32 +02:00
Checking for tables at startup
This commit is contained in:
parent
5e627cb420
commit
3806c35bd3
@ -15,9 +15,24 @@ const dbReady = utils.deferred();
|
||||
|
||||
cls.init(initDbConnection);
|
||||
|
||||
function schemaExists() {
|
||||
function checkTableExistsInDb(TableName) {
|
||||
return !!sql.getValue(`SELECT name FROM sqlite_master
|
||||
WHERE type = 'table' AND name = 'options'`);
|
||||
WHERE type = 'table' AND name = '${TableName}'`);
|
||||
}
|
||||
|
||||
function schemaExists() {
|
||||
return checkTableExistsInDb('options');
|
||||
}
|
||||
|
||||
function readTableNamesFromSchema(schema) {
|
||||
return schema.split(";").reduce((acc, str) => str.includes("CREATE TABLE IF NOT EXISTS ") ?
|
||||
[...acc, str.replaceAll("\n", '').replace(/^.*CREATE TABLE IF NOT EXISTS[ \t]+"([^"]*)".*/, "$1")] : acc, []);
|
||||
}
|
||||
|
||||
function getTableInitializingQuery(schema, tblName) {
|
||||
return schema.split(";").filter((str) => {
|
||||
return str.includes(tblName);
|
||||
});
|
||||
}
|
||||
|
||||
function isDbInitialized() {
|
||||
@ -38,6 +53,18 @@ async function initDbConnection() {
|
||||
return;
|
||||
}
|
||||
|
||||
const schema = fs.readFileSync(`${resourceDir.DB_INIT_DIR}/schema.sql`, 'UTF-8');
|
||||
|
||||
readTableNamesFromSchema(schema).forEach((tblName) => {
|
||||
if (!checkTableExistsInDb(tblName)) {
|
||||
log.info(`Table ${tblName} not found in DB, creating ...`);
|
||||
getTableInitializingQuery(schema, tblName).forEach((sQuery) => {
|
||||
sql.execute(sQuery);
|
||||
log.info(sQuery);
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
await migrationService.migrateIfNecessary();
|
||||
|
||||
sql.execute('CREATE TEMP TABLE "param_list" (`paramId` TEXT NOT NULL PRIMARY KEY)');
|
||||
@ -131,7 +158,7 @@ function createDatabaseForSync(options, syncServerHost = '', syncProxy = '') {
|
||||
sql.transactional(() => {
|
||||
sql.executeScript(schema);
|
||||
|
||||
require('./options_init.js').initNotSyncedOptions(false, { syncServerHost, syncProxy });
|
||||
require('./options_init.js').initNotSyncedOptions(false, { syncServerHost, syncProxy });
|
||||
|
||||
// document options required for sync to kick off
|
||||
for (const opt of options) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user