Добавляем миграции в БД sqlite
This commit is contained in:
43
server/db/connManager.js
Normal file
43
server/db/connManager.js
Normal file
@@ -0,0 +1,43 @@
|
||||
const SqliteConnectionPool = require('./SqliteConnectionPool');
|
||||
const log = require('../core/getLogger').getLog();
|
||||
|
||||
const migrations = {
|
||||
'app': require('./migrations/app'),
|
||||
'readerStorage': require('./migrations/readerStorage'),
|
||||
};
|
||||
|
||||
class ConnManager {
|
||||
constructor() {
|
||||
this._pool = {};
|
||||
}
|
||||
|
||||
async init(config) {
|
||||
this.config = config;
|
||||
|
||||
const force = (config.branch == 'development' ? 'last' : null);
|
||||
|
||||
for (const poolConfig of this.config.db) {
|
||||
const dbFileName = this.config.dataDir + '/' + poolConfig.fileName;
|
||||
const connPool = new SqliteConnectionPool();
|
||||
await connPool.open(poolConfig.connCount, dbFileName);
|
||||
|
||||
log(`Opened database "${poolConfig.poolName}"`);
|
||||
//миграции
|
||||
const migs = migrations[poolConfig.poolName];
|
||||
if (migs && migs.data.length) {
|
||||
const applied = await connPool.migrate(migs.data, migs.table, force);
|
||||
log(`Applied ${applied} migrations to "${poolConfig.poolName}"`);
|
||||
}
|
||||
|
||||
this._pool[poolConfig.poolName] = connPool;
|
||||
}
|
||||
}
|
||||
|
||||
get pool() {
|
||||
return this._pool;
|
||||
}
|
||||
}
|
||||
|
||||
const connManager = new ConnManager();
|
||||
|
||||
module.exports = connManager;
|
||||
Reference in New Issue
Block a user