@iobroker/db-states-jsonl
Advanced tools
Comparing version 4.0.0-alpha.45-20220114-88aaebdb to 4.0.0-alpha.46-20220119-9e487e55
@@ -80,3 +80,5 @@ /** | ||
async open() { | ||
await this._db.open(); | ||
if (!(await this._maybeMigrateFileDB())) { | ||
await this._db.open(); | ||
} | ||
@@ -126,2 +128,78 @@ // Create an object-like wrapper around the internal Map | ||
/** | ||
* Checks if an existing file DB should be migrated to JSONL | ||
* @returns {Promise<boolean>} true if the file DB was migrated. false if not. | ||
* If this returns true, the jsonl DB was opened and doesn't need to be opened again. | ||
*/ | ||
async _maybeMigrateFileDB() { | ||
const jsonlFileName = path.join(this.dataDir, this.settings.jsonlDB.fileName); | ||
const jsonFileName = path.join(this.dataDir, this.settings.fileDB.fileName); | ||
const bakFileName = path.join(this.dataDir, this.settings.fileDB.fileName + '.bak'); | ||
// Check the timestamps of each file, defaulting to 0 if they don't exist | ||
let jsonlTimeStamp = 0; | ||
let jsonTimeStamp = 0; | ||
let bakTimeStamp = 0; | ||
try { | ||
const stat = fs.statSync(jsonlFileName); | ||
if (stat.isFile()) { | ||
jsonlTimeStamp = stat.mtime; | ||
} | ||
} catch { | ||
// ignore | ||
} | ||
try { | ||
const stat = fs.statSync(jsonFileName); | ||
if (stat.isFile()) { | ||
jsonTimeStamp = stat.mtime; | ||
} | ||
} catch { | ||
// ignore | ||
} | ||
try { | ||
const stat = fs.statSync(bakFileName); | ||
if (stat.isFile()) { | ||
bakTimeStamp = stat.mtime; | ||
} | ||
} catch { | ||
// ignore | ||
} | ||
// Figure out which file needs to be imported | ||
/** @type {string} */ | ||
let importFilename; | ||
if (jsonTimeStamp > 0 && jsonTimeStamp >= bakTimeStamp && jsonTimeStamp >= jsonlTimeStamp) { | ||
importFilename = jsonFileName; | ||
} else if (bakTimeStamp > 0 && bakTimeStamp >= jsonTimeStamp && bakTimeStamp >= jsonlTimeStamp) { | ||
importFilename = bakFileName; | ||
} else { | ||
// None of the File DB files are newer than the JSONL file | ||
// There is nothing to restore, we're done | ||
return false; | ||
} | ||
await this._db.open(); | ||
this._db.clear(); | ||
await this._db.importJson(importFilename); | ||
// And rename the existing files to avoid redoing the work next time | ||
if (fs.existsSync(jsonFileName)) { | ||
try { | ||
fs.renameSync(jsonFileName, `${jsonFileName}.migrated`); | ||
} catch { | ||
// ignore | ||
} | ||
} | ||
if (fs.existsSync(bakFileName)) { | ||
try { | ||
fs.renameSync(bakFileName, `${bakFileName}.migrated`); | ||
} catch { | ||
// ignore | ||
} | ||
} | ||
// Signal to the caller that the DB is already open | ||
return true; | ||
} | ||
async saveState() { | ||
@@ -134,6 +212,6 @@ // Nothing to do, the DB saves behind the scenes | ||
const now = Date.now(); | ||
const tmpBackupFileName = path.join(os.tmpdir(), this.getTimeStr(now) + '_' + this.settings.jsonlDB.fileName); | ||
const tmpBackupFileName = path.join(os.tmpdir(), `${this.getTimeStr(now)}_${this.settings.jsonlDB.fileName}`); | ||
const backupFileName = path.join( | ||
this.backupDir, | ||
this.getTimeStr(now) + '_' + this.settings.jsonlDB.fileName + '.gz' | ||
`${this.getTimeStr(now)}_${this.settings.jsonlDB.fileName}.gz` | ||
); | ||
@@ -140,0 +218,0 @@ try { |
{ | ||
"name": "@iobroker/db-states-jsonl", | ||
"version": "4.0.0-alpha.45-20220114-88aaebdb", | ||
"version": "4.0.0-alpha.46-20220119-9e487e55", | ||
"engines": { | ||
@@ -9,5 +9,5 @@ "node": ">=12.0.0" | ||
"@alcalzone/jsonl-db": "~2.4.1", | ||
"@iobroker/db-base": "4.0.0-alpha.45-20220114-88aaebdb", | ||
"@iobroker/db-states-file": "4.0.0-alpha.45-20220114-88aaebdb", | ||
"@iobroker/db-states-redis": "4.0.0-alpha.45-20220114-88aaebdb" | ||
"@iobroker/db-base": "4.0.0-alpha.46-20220119-9e487e55", | ||
"@iobroker/db-states-file": "4.0.0-alpha.46-20220119-9e487e55", | ||
"@iobroker/db-states-redis": "4.0.0-alpha.46-20220119-9e487e55" | ||
}, | ||
@@ -39,3 +39,3 @@ "keywords": [ | ||
], | ||
"gitHead": "e2fd3e7af91eb83c30f8ae45f71d6e178edc7b28" | ||
"gitHead": "e081e38bf6d24e767e16a0bb5a8d8202f65a9302" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34634
789
+ Added@iobroker/db-base@4.0.0-alpha.46-20220119-9e487e55(transitive)
+ Added@iobroker/db-states-file@4.0.0-alpha.46-20220119-9e487e55(transitive)
+ Added@iobroker/db-states-redis@4.0.0-alpha.46-20220119-9e487e55(transitive)
+ Added@iobroker/js-controller-common@4.0.0-alpha.46-20220119-9e487e55(transitive)
- Removed@iobroker/db-base@4.0.0-alpha.45-20220114-88aaebdb(transitive)
- Removed@iobroker/db-states-file@4.0.0-alpha.45-20220114-88aaebdb(transitive)
- Removed@iobroker/db-states-redis@4.0.0-alpha.45-20220114-88aaebdb(transitive)
- Removed@iobroker/js-controller-common@4.0.0-alpha.45-20220114-88aaebdb(transitive)
Updated@iobroker/db-states-file@4.0.0-alpha.46-20220119-9e487e55
Updated@iobroker/db-states-redis@4.0.0-alpha.46-20220119-9e487e55