浏览代码

Пробные миграции

Book Pauk 6 年之前
父节点
当前提交
c287ca9ea8

+ 16 - 10
server/db/SqliteConnectionPool.js

@@ -140,15 +140,18 @@ class SqliteConnectionPool {
         for (const migration of dbMigrations.slice().sort((a, b) => Math.sign(b.id - a.id))) {
             if (!migrations.some(x => x.id === migration.id) ||
                 (force === 'last' && migration.id === lastMigration.id)) {
-                await this.run('BEGIN');
+                const dbh = await this.get();
+                await dbh.run('BEGIN');
                 try {
-                    await this.exec(migration.down);
-                    await this.run(SQL`DELETE FROM "`.append(table).append(SQL`" WHERE id = ${migration.id}`));
-                    await this.run('COMMIT');
+                    await dbh.exec(migration.down);
+                    await dbh.run(SQL`DELETE FROM "`.append(table).append(SQL`" WHERE id = ${migration.id}`));
+                    await dbh.run('COMMIT');
                     dbMigrations = dbMigrations.filter(x => x.id !== migration.id);
                 } catch (err) {
-                    await this.run('ROLLBACK');
+                    await dbh.run('ROLLBACK');
                     throw err;
+                } finally {
+                    dbh.ret();
                 }
             } else {
                 break;
@@ -160,17 +163,20 @@ class SqliteConnectionPool {
         const lastMigrationId = dbMigrations.length ? dbMigrations[dbMigrations.length - 1].id : 0;
         for (const migration of migrations) {
             if (migration.id > lastMigrationId) {
-                await this.run('BEGIN');
+                const dbh = await this.get();
+                await dbh.run('BEGIN');
                 try {
-                    await this.exec(migration.up);
-                    await this.run(SQL`INSERT INTO "`.append(table).append(
+                    await dbh.exec(migration.up);
+                    await dbh.run(SQL`INSERT INTO "`.append(table).append(
                         SQL`" (id, name, up, down) VALUES (${migration.id}, ${migration.name}, ${migration.up}, ${migration.down})`)
                     );
-                    await this.run('COMMIT');
+                    await dbh.run('COMMIT');
                     applied.push(migration.id);
                 } catch (err) {
-                    await this.run('ROLLBACK');
+                    await dbh.run('ROLLBACK');
                     throw err;
+                } finally {
+                    dbh.ret();
                 }
             }
         }

+ 1 - 1
server/db/connManager.js

@@ -26,7 +26,7 @@ class ConnManager {
             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}"`);
+                log(`Applied ${applied.length} migrations to "${poolConfig.poolName}"`);
             }
 
             this._pool[poolConfig.poolName] = connPool;

+ 7 - 0
server/db/migrations/readerStorage/001-create.js

@@ -0,0 +1,7 @@
+module.exports = `
+-- Up
+CREATE TABLE storage (id2 INTEGER PRIMARY KEY, name TEXT);
+
+-- Down
+DROP TABLE storage;
+`;

+ 1 - 0
server/db/migrations/readerStorage/index.js

@@ -1,5 +1,6 @@
 module.exports = {
     table: 'migration1',
     data: [
+        {id: 1, name: 'create', data: require('./001-create')}
     ]
 }