Пробные миграции
This commit is contained in:
@@ -140,15 +140,18 @@ class SqliteConnectionPool {
|
|||||||
for (const migration of dbMigrations.slice().sort((a, b) => Math.sign(b.id - a.id))) {
|
for (const migration of dbMigrations.slice().sort((a, b) => Math.sign(b.id - a.id))) {
|
||||||
if (!migrations.some(x => x.id === migration.id) ||
|
if (!migrations.some(x => x.id === migration.id) ||
|
||||||
(force === 'last' && migration.id === lastMigration.id)) {
|
(force === 'last' && migration.id === lastMigration.id)) {
|
||||||
await this.run('BEGIN');
|
const dbh = await this.get();
|
||||||
|
await dbh.run('BEGIN');
|
||||||
try {
|
try {
|
||||||
await this.exec(migration.down);
|
await dbh.exec(migration.down);
|
||||||
await this.run(SQL`DELETE FROM "`.append(table).append(SQL`" WHERE id = ${migration.id}`));
|
await dbh.run(SQL`DELETE FROM "`.append(table).append(SQL`" WHERE id = ${migration.id}`));
|
||||||
await this.run('COMMIT');
|
await dbh.run('COMMIT');
|
||||||
dbMigrations = dbMigrations.filter(x => x.id !== migration.id);
|
dbMigrations = dbMigrations.filter(x => x.id !== migration.id);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await this.run('ROLLBACK');
|
await dbh.run('ROLLBACK');
|
||||||
throw err;
|
throw err;
|
||||||
|
} finally {
|
||||||
|
dbh.ret();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
@@ -160,17 +163,20 @@ class SqliteConnectionPool {
|
|||||||
const lastMigrationId = dbMigrations.length ? dbMigrations[dbMigrations.length - 1].id : 0;
|
const lastMigrationId = dbMigrations.length ? dbMigrations[dbMigrations.length - 1].id : 0;
|
||||||
for (const migration of migrations) {
|
for (const migration of migrations) {
|
||||||
if (migration.id > lastMigrationId) {
|
if (migration.id > lastMigrationId) {
|
||||||
await this.run('BEGIN');
|
const dbh = await this.get();
|
||||||
|
await dbh.run('BEGIN');
|
||||||
try {
|
try {
|
||||||
await this.exec(migration.up);
|
await dbh.exec(migration.up);
|
||||||
await this.run(SQL`INSERT INTO "`.append(table).append(
|
await dbh.run(SQL`INSERT INTO "`.append(table).append(
|
||||||
SQL`" (id, name, up, down) VALUES (${migration.id}, ${migration.name}, ${migration.up}, ${migration.down})`)
|
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);
|
applied.push(migration.id);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await this.run('ROLLBACK');
|
await dbh.run('ROLLBACK');
|
||||||
throw err;
|
throw err;
|
||||||
|
} finally {
|
||||||
|
dbh.ret();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ class ConnManager {
|
|||||||
const migs = migrations[poolConfig.poolName];
|
const migs = migrations[poolConfig.poolName];
|
||||||
if (migs && migs.data.length) {
|
if (migs && migs.data.length) {
|
||||||
const applied = await connPool.migrate(migs.data, migs.table, force);
|
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;
|
this._pool[poolConfig.poolName] = connPool;
|
||||||
|
|||||||
7
server/db/migrations/readerStorage/001-create.js
Normal file
7
server/db/migrations/readerStorage/001-create.js
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
module.exports = `
|
||||||
|
-- Up
|
||||||
|
CREATE TABLE storage (id2 INTEGER PRIMARY KEY, name TEXT);
|
||||||
|
|
||||||
|
-- Down
|
||||||
|
DROP TABLE storage;
|
||||||
|
`;
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
table: 'migration1',
|
table: 'migration1',
|
||||||
data: [
|
data: [
|
||||||
|
{id: 1, name: 'create', data: require('./001-create')}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user