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

This commit is contained in:
Book Pauk
2019-03-08 18:26:03 +07:00
parent 5122cda6db
commit c287ca9ea8
4 changed files with 25 additions and 11 deletions

View File

@@ -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();
}
}
}