UNPKG

@egi/smart-db

Version:

Unified Smart DB Access

1 lines 3.56 kB
import*as fs from"fs";import _ from"lodash";import{SmartDbVersionModel}from"./models/smart-db-version-model";import{SmartDbVersionViewModel}from"./models/smart-db-version-view-model";import{SmartErrorLocation}from"./smart-error";export class SmartDbUpgradeManager{constructor(e){this.db=e,this.log=e.getLogger()}hasDatabaseModule(e){return new Promise(((t,r)=>{this.db.exists(SmartDbVersionModel).then((a=>{a?this.db.getFirst(SmartDbVersionModel,{module:e}).then((e=>{t(!!e)})).catch((e=>{r(e)})):t(!1)})).catch((e=>{r(e)}))}))}prepareDatabase(e){return new Promise(((t,r)=>{try{const a=fs.existsSync(e.sqlFilesDirectory)&&fs.statSync(e.sqlFilesDirectory);if(a&&a.isDirectory()){const a=e.sqlFilesDirectory+`/${e.module}-init.sql`;fs.existsSync(a)?this.hasDatabaseModule(e.module).then((o=>{o?(this.log.logInfo(SmartErrorLocation.UpgradeManager,"prepareDatabase",`database module '${e.module}' exists`),this.upgradeDatabase(e).then((r=>{this.log.logInfo(SmartErrorLocation.UpgradeManager,"prepareDatabase",`connected to module '${e.module}' version ${r.versionString}`),t(r)})).catch((e=>{r(e)}))):(this.log.setDbLogging(!1),this.log.logInfo(SmartErrorLocation.UpgradeManager,"prepareDatabase",`database module '${e.module}' doesn't exists - begin creation`),this.db.execScript(a,!0).then((()=>{this.log.setDbLogging(this.db.dbLogging),this.log.logInfo(SmartErrorLocation.UpgradeManager,"prepareDatabase","database module creation complete"),this.hasDatabaseModule(e.module).then((a=>{a?(this.log.logInfo(SmartErrorLocation.UpgradeManager,"prepareDatabase",`successfully initialized database for new module ${e.module}`),this.upgradeDatabase(e).then((r=>{this.log.logInfo(SmartErrorLocation.UpgradeManager,"prepareDatabase",`connected to module '${e.module}' version ${r.versionString}`),t(r)})).catch((e=>{r(e)}))):r("error running database initialization script")})).catch((e=>{r(e)}))})).catch((e=>{r(e)})))})).catch((e=>{r(e)})):r(`missing mandatory '${e.module}-init.sql' file in ${a}`)}else r(`option 'sqlFilesDirectory' (${e.sqlFilesDirectory}) must point to an existing directory (absolute or relative to ${fs.realpathSync(".")})`)}catch(e){r(e)}}))}upgradeDatabase(e){return new Promise(((t,r)=>{this.db.getFirst(SmartDbVersionViewModel,{module:e.module}).then((a=>{a?fs.readdir(e.sqlFilesDirectory,((o,s)=>{if(o)r(`unable to read database files from ${e.sqlFilesDirectory}`);else{let o=[];s.forEach((t=>{const r=new RegExp(`^${e.module}-update.([0-9]{3})\\..*\\.sql$`),s=t.match(r);if(s){parseInt(s[1],10)>a.sequence&&o.push(t)}})),o=_.sortBy(o),this.executeScriptsSequentially(o,e).then((()=>{this.db.getFirst(SmartDbVersionViewModel,{module:e.module},"*","sequence DESC").then((e=>{e?t(SmartDbVersionViewModel.from(e)):r("unable to determine final smart db version")})).catch((e=>{r(e)}))})).catch((e=>{r(e)}))}})):r(`missing version entry for module ${e.module} - add the mandatory insert into smart_db_version statement to your database creation script!`)})).catch((e=>{r(e)}))}))}executeScriptsSequentially(e,t){return new Promise((async(r,a)=>{e&&e.length>0?(this.log.logInfo(SmartErrorLocation.UpgradeManager,"upgradeDatabase",`execute update script '${e[0]}' for module ${t.module}`),await this.db.execScript(`${t.sqlFilesDirectory}/${e[0]}`).then((()=>{this.log.logInfo(SmartErrorLocation.UpgradeManager,"upgradeDatabase",`successfully executed update script '${e[0]}' for module ${t.module}`),1==e.length&&r(!0)})).catch((e=>{a(e)})),e.length>1&&this.executeScriptsSequentially(e.splice(1),t).then((e=>{r(e)})).catch((e=>{a(e)}))):r(!0)}))}}