From 14ca2daa39109dbc5094a18bcdf23234569ae729 Mon Sep 17 00:00:00 2001 From: Book Pauk Date: Wed, 1 Dec 2021 22:09:48 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C=D1=88?= =?UTF-8?q?=D0=BE=D0=B9=20=D1=80=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=B8=D0=BD=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/db/JembaConnManager.js | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/server/db/JembaConnManager.js b/server/db/JembaConnManager.js index 0ed3423a..50cc61d3 100644 --- a/server/db/JembaConnManager.js +++ b/server/db/JembaConnManager.js @@ -14,7 +14,6 @@ class JembaConnManager { constructor() { if (!instance) { this.inited = false; - this.closed = false; instance = this; } @@ -22,15 +21,13 @@ class JembaConnManager { return instance; } - async init(config, migs = jembaMigrations) { + async init(config, migs = jembaMigrations, undoLastMigration = false) { if (this.inited) throw new Error('JembaConnManager initialized already'); this.config = config; this._db = {}; - const force = null;//(config.branch == 'development' ? 'last' : null); - for (const dbConfig of this.config.db) { const dbPath = `${this.config.dataDir}/db/${dbConfig.dbName}`; @@ -49,7 +46,7 @@ class JembaConnManager { } log(`Open "${dbConfig.dbName}" start`); - await dbConn.openDb({dbPath, cacheSize: dbConfig.cacheSize, compressed: dbConfig.compressed, forceFileClosing: true}); + await dbConn.openDb({dbPath, cacheSize: dbConfig.cacheSize, compressed: dbConfig.compressed, forceFileClosing: dbConfig.forceFileClosing}); if (dbConfig.openAll) { try { @@ -76,7 +73,7 @@ class JembaConnManager { //миграции const mig = migs[dbConfig.dbName]; if (mig && mig.data) { - const applied = await this.migrate(dbConn, mig.data, mig.table, force); + const applied = await this.migrate(dbConn, mig.data, mig.table, undoLastMigration); if (applied.length) log(`${applied.length} migrations applied to "${dbConfig.dbName}"`); } @@ -91,20 +88,17 @@ class JembaConnManager { async close() { if (!this.inited) - throw new Error('JembaConnManager not inited'); - - if (this.closed) - throw new Error('JembaConnManager closed'); + return; for (const dbConfig of this.config.db) { await this._db[dbConfig.dbName].closeDb(); } this._db = {}; - this.closed = true; + this.inited = false; } - async migrate(db, migs, table, force) { + async migrate(db, migs, table, undoLastMigration) { const migrations = _.cloneDeep(migs).sort((a, b) => a.id - b.id); if (!migrations.length) { @@ -144,11 +138,11 @@ class JembaConnManager { }; // Undo migrations that exist only in the database but not in migs, - // also undo the last migration if the `force` option was set to `last`. + // also undo the last migration if the undoLastMigration const lastMigration = migrations[migrations.length - 1]; for (const migration of dbMigrations.slice().sort((a, b) => b.id - a.id)) { if (!migrations.some(x => x.id === migration.id) || - (force === 'last' && migration.id === lastMigration.id)) { + (undoLastMigration && migration.id === lastMigration.id)) { await execUpDown(migration.down); await db.delete({ table, @@ -181,9 +175,6 @@ class JembaConnManager { if (!this.inited) throw new Error('JembaConnManager not inited'); - if (this.closed) - throw new Error('JembaConnManager closed'); - return this._db; } }