mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +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);
|
cls.init(initDbConnection);
|
||||||
|
|
||||||
function schemaExists() {
|
function checkTableExistsInDb(TableName) {
|
||||||
return !!sql.getValue(`SELECT name FROM sqlite_master
|
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() {
|
function isDbInitialized() {
|
||||||
@ -38,6 +53,18 @@ async function initDbConnection() {
|
|||||||
return;
|
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();
|
await migrationService.migrateIfNecessary();
|
||||||
|
|
||||||
sql.execute('CREATE TEMP TABLE "param_list" (`paramId` TEXT NOT NULL PRIMARY KEY)');
|
sql.execute('CREATE TEMP TABLE "param_list" (`paramId` TEXT NOT NULL PRIMARY KEY)');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user