@ovotech/pg-sql-migrate
Advanced tools
Comparing version
@@ -16,2 +16,3 @@ /// <reference types="node" /> | ||
}) => Promise<string[]>; | ||
export declare const isTransactionDisabled: (migration: string) => boolean; | ||
export declare const executeMigrations: ({ db, table, logger, migrations, }: { | ||
@@ -18,0 +19,0 @@ db: MigrationClient; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.migrate = exports.readAndExecuteMigrations = exports.loadConfig = exports.readNewMigrations = exports.executeMigrations = exports.readExecutedIds = exports.readMigrations = exports.filenameParts = void 0; | ||
exports.migrate = exports.readAndExecuteMigrations = exports.loadConfig = exports.readNewMigrations = exports.executeMigrations = exports.isTransactionDisabled = exports.readExecutedIds = exports.readMigrations = exports.filenameParts = void 0; | ||
const config_file_1 = require("@ovotech/config-file"); | ||
@@ -28,2 +28,3 @@ const pg_1 = require("pg"); | ||
}; | ||
exports.isTransactionDisabled = (migration) => migration.startsWith('-- pg-sql-migrate: DISABLE TRANSACTION'); | ||
exports.executeMigrations = async ({ db, table, logger, migrations, }) => { | ||
@@ -33,10 +34,17 @@ for (const migration of migrations) { | ||
logger.info(`Executing [${migration.id}] ${migration.name}`); | ||
await db.query('BEGIN'); | ||
await db.query(migration.content); | ||
await db.query(`INSERT INTO ${db.escapeIdentifier(table)} VALUES ($1);`, [migration.id]); | ||
await db.query('COMMIT'); | ||
if (exports.isTransactionDisabled(migration.content)) { | ||
await db.query(migration.content); | ||
} | ||
else { | ||
await db.query('BEGIN'); | ||
await db.query(migration.content); | ||
await db.query(`INSERT INTO ${db.escapeIdentifier(table)} VALUES ($1);`, [migration.id]); | ||
await db.query('COMMIT'); | ||
} | ||
} | ||
catch (error) { | ||
try { | ||
await db.query('ROLLBACK'); | ||
if (!exports.isTransactionDisabled(migration.content)) { | ||
await db.query('ROLLBACK'); | ||
} | ||
} | ||
@@ -43,0 +51,0 @@ finally { |
@@ -5,3 +5,3 @@ { | ||
"repository": "git@github.com:ovotech/pg-sql-migrate.git", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"main": "dist/index.js", | ||
@@ -8,0 +8,0 @@ "types": "dist/index.d.ts", |
@@ -107,2 +107,11 @@ # Postgres migration tool with plain sql | ||
## Distabling transactions for some migrations | ||
By default migrations are wrapped in transactions, but there are some PG operations that cannot be performed inside a transaction. You can disable this for a specific transaction by adding a prefix | ||
```sql | ||
-- pg-sql-migrate: DISABLE TRANSACTION | ||
ALTER TYPE my_type ADD VALUE 'VAL2' AFTER 'VAL1'; | ||
``` | ||
## Running the tests | ||
@@ -109,0 +118,0 @@ |
@@ -49,2 +49,5 @@ import { loadConfigFile } from '@ovotech/config-file'; | ||
export const isTransactionDisabled = (migration: string): boolean => | ||
migration.startsWith('-- pg-sql-migrate: DISABLE TRANSACTION'); | ||
export const executeMigrations = async ({ | ||
@@ -64,9 +67,15 @@ db, | ||
logger.info(`Executing [${migration.id}] ${migration.name}`); | ||
await db.query('BEGIN'); | ||
await db.query(migration.content); | ||
await db.query(`INSERT INTO ${db.escapeIdentifier(table)} VALUES ($1);`, [migration.id]); | ||
await db.query('COMMIT'); | ||
if (isTransactionDisabled(migration.content)) { | ||
await db.query(migration.content); | ||
} else { | ||
await db.query('BEGIN'); | ||
await db.query(migration.content); | ||
await db.query(`INSERT INTO ${db.escapeIdentifier(table)} VALUES ($1);`, [migration.id]); | ||
await db.query('COMMIT'); | ||
} | ||
} catch (error) { | ||
try { | ||
await db.query('ROLLBACK'); | ||
if (!isTransactionDisabled(migration.content)) { | ||
await db.query('ROLLBACK'); | ||
} | ||
} finally { | ||
@@ -73,0 +82,0 @@ throw new MigrationError(error.message, migration); |
Sorry, the diff of this file is not supported yet
35924
4.2%592
2.96%145
6.62%