diff --git a/backend/utils/database.js b/backend/utils/database.js index 609c3e6e..ca756ec7 100644 --- a/backend/utils/database.js +++ b/backend/utils/database.js @@ -197,19 +197,21 @@ export class DatabaseAdapter { this.dbFilePath = ''; this.userSessions = []; this.dbFilePath = dbDirectoryPath + sep + 'node-' + selNode.index; - const oldFileName = dbDirectoryPath + sep + 'rtldb-node-' + selNode.index + '.json'; - if (selNode.ln_implementation === 'CLN' && fs.existsSync(oldFileName)) { - this.renameOldDB(oldFileName, selNode); + // For backward compatibility Start + const oldFilePath = dbDirectoryPath + sep + 'rtldb-node-' + selNode.index + '.json'; + if (selNode.ln_implementation === 'CLN' && fs.existsSync(oldFilePath)) { + this.renameOldDB(oldFilePath, selNode); } + // For backward compatibility End this.insertSession(id); } - renameOldDB(oldFileName, selNode = null) { - const newFileName = this.dbFilePath + sep + 'rtldb-' + selNode.ln_implementation + '-Offers.json'; + renameOldDB(oldFilePath, selNode = null) { + const newFilePath = this.dbFilePath + sep + 'rtldb-' + selNode.ln_implementation + '-Offers.json'; try { this.common.createDirectory(this.dbFilePath); - const oldOffers = JSON.parse(fs.readFileSync(oldFileName, 'utf-8')); - fs.writeFileSync(oldFileName, JSON.stringify(oldOffers.Offers, null, 2)); - fs.renameSync(oldFileName, newFileName); + const oldOffers = JSON.parse(fs.readFileSync(oldFilePath, 'utf-8')); + fs.writeFileSync(oldFilePath, JSON.stringify(oldOffers.Offers, null, 2)); + fs.renameSync(oldFilePath, newFilePath); } catch (err) { this.logger.log({ selectedNode: selNode, level: 'ERROR', fileName: 'Database', msg: 'Rename Old Database Error', error: err }); @@ -224,17 +226,28 @@ export class DatabaseAdapter { catch (err) { return new Error('Unable to Create Directory Error ' + JSON.stringify(err)); } - const collectionFileName = this.dbFilePath + sep + 'rtldb-' + this.selNode.ln_implementation + '-' + collectionName + '.json'; + const collectionFilePath = this.dbFilePath + sep + 'rtldb-' + this.selNode.ln_implementation + '-' + collectionName + '.json'; try { - if (!fs.existsSync(collectionFileName)) { - fs.writeFileSync(collectionFileName, '[]'); + if (!fs.existsSync(collectionFilePath)) { + fs.writeFileSync(collectionFilePath, '[]'); } } catch (err) { return new Error('Unable to Create Database File Error ' + JSON.stringify(err)); } try { - const dataFromFile = fs.readFileSync(collectionFileName, 'utf-8'); + const otherFiles = fs.readdirSync(this.dbFilePath); + otherFiles.forEach((oFileName) => { + if (oFileName.endsWith('.json') && oFileName !== ('rtldb-' + this.selNode.ln_implementation + '-' + collectionName + '.json')) { + fs.renameSync(this.dbFilePath + sep + oFileName, this.dbFilePath + sep + oFileName + '.tmp'); + } + }); + } + catch (err) { + this.logger.log({ selectedNode: this.selNode, level: 'ERROR', fileName: 'Database', msg: 'Rename Other Implementation DB Error', error: err }); + } + try { + const dataFromFile = fs.readFileSync(collectionFilePath, 'utf-8'); const dataObj = !dataFromFile ? null : JSON.parse(dataFromFile); return dataObj; } @@ -248,10 +261,10 @@ export class DatabaseAdapter { saveData(collectionName, collectionData) { try { if (collectionData) { - const collectionFileName = this.dbFilePath + sep + 'rtldb-' + this.selNode.ln_implementation + '-' + collectionName + '.json'; - const tempFile = collectionFileName + '.tmp'; + const collectionFilePath = this.dbFilePath + sep + 'rtldb-' + this.selNode.ln_implementation + '-' + collectionName + '.json'; + const tempFile = collectionFilePath + '.tmp'; fs.writeFileSync(tempFile, JSON.stringify(collectionData, null, 2)); - fs.renameSync(tempFile, collectionFileName); + fs.renameSync(tempFile, collectionFilePath); } return true; } diff --git a/server/utils/database.ts b/server/utils/database.ts index e14a7a83..6c680a11 100644 --- a/server/utils/database.ts +++ b/server/utils/database.ts @@ -201,18 +201,20 @@ export class DatabaseAdapter { constructor(public dbDirectoryPath: string, private selNode: CommonSelectedNode = null, private id: string = '') { this.dbFilePath = dbDirectoryPath + sep + 'node-' + selNode.index; - const oldFileName = dbDirectoryPath + sep + 'rtldb-node-' + selNode.index + '.json'; - if (selNode.ln_implementation === 'CLN' && fs.existsSync(oldFileName)) { this.renameOldDB(oldFileName, selNode); } + // For backward compatibility Start + const oldFilePath = dbDirectoryPath + sep + 'rtldb-node-' + selNode.index + '.json'; + if (selNode.ln_implementation === 'CLN' && fs.existsSync(oldFilePath)) { this.renameOldDB(oldFilePath, selNode); } + // For backward compatibility End this.insertSession(id); } - renameOldDB(oldFileName: string, selNode: CommonSelectedNode = null) { - const newFileName = this.dbFilePath + sep + 'rtldb-' + selNode.ln_implementation + '-Offers.json'; + renameOldDB(oldFilePath: string, selNode: CommonSelectedNode = null) { + const newFilePath = this.dbFilePath + sep + 'rtldb-' + selNode.ln_implementation + '-Offers.json'; try { this.common.createDirectory(this.dbFilePath); - const oldOffers: any = JSON.parse(fs.readFileSync(oldFileName, 'utf-8')); - fs.writeFileSync(oldFileName, JSON.stringify(oldOffers.Offers, null, 2)); - fs.renameSync(oldFileName, newFileName); + const oldOffers: any = JSON.parse(fs.readFileSync(oldFilePath, 'utf-8')); + fs.writeFileSync(oldFilePath, JSON.stringify(oldOffers.Offers, null, 2)); + fs.renameSync(oldFilePath, newFilePath); } catch (err) { this.logger.log({ selectedNode: selNode, level: 'ERROR', fileName: 'Database', msg: 'Rename Old Database Error', error: err }); } @@ -226,16 +228,26 @@ export class DatabaseAdapter { } catch (err) { return new Error('Unable to Create Directory Error ' + JSON.stringify(err)); } - const collectionFileName = this.dbFilePath + sep + 'rtldb-' + this.selNode.ln_implementation + '-' + collectionName + '.json'; + const collectionFilePath = this.dbFilePath + sep + 'rtldb-' + this.selNode.ln_implementation + '-' + collectionName + '.json'; try { - if (!fs.existsSync(collectionFileName)) { - fs.writeFileSync(collectionFileName, '[]'); + if (!fs.existsSync(collectionFilePath)) { + fs.writeFileSync(collectionFilePath, '[]'); } } catch (err) { return new Error('Unable to Create Database File Error ' + JSON.stringify(err)); } try { - const dataFromFile = fs.readFileSync(collectionFileName, 'utf-8'); + const otherFiles = fs.readdirSync(this.dbFilePath); + otherFiles.forEach((oFileName) => { + if (oFileName.endsWith('.json') && oFileName !== ('rtldb-' + this.selNode.ln_implementation + '-' + collectionName + '.json')) { + fs.renameSync(this.dbFilePath + sep + oFileName, this.dbFilePath + sep + oFileName + '.tmp'); + } + }); + } catch (err) { + this.logger.log({ selectedNode: this.selNode, level: 'ERROR', fileName: 'Database', msg: 'Rename Other Implementation DB Error', error: err }); + } + try { + const dataFromFile = fs.readFileSync(collectionFilePath, 'utf-8'); const dataObj = !dataFromFile ? null : (JSON.parse(dataFromFile)); return dataObj; } catch (err) { @@ -250,10 +262,10 @@ export class DatabaseAdapter { saveData(collectionName: string, collectionData: any) { try { if (collectionData) { - const collectionFileName = this.dbFilePath + sep + 'rtldb-' + this.selNode.ln_implementation + '-' + collectionName + '.json'; - const tempFile = collectionFileName + '.tmp'; + const collectionFilePath = this.dbFilePath + sep + 'rtldb-' + this.selNode.ln_implementation + '-' + collectionName + '.json'; + const tempFile = collectionFilePath + '.tmp'; fs.writeFileSync(tempFile, JSON.stringify(collectionData, null, 2)); - fs.renameSync(tempFile, collectionFileName); + fs.renameSync(tempFile, collectionFilePath); } return true; } catch (err) {