From 9a89c1a200322c678e2b0246ed324c847e734fc6 Mon Sep 17 00:00:00 2001 From: wangsijie Date: Fri, 23 Sep 2022 10:22:13 +0800 Subject: [PATCH] feat(core): deploy migration in transaction mode (#1980) --- packages/core/src/migration/index.ts | 4 +++- packages/core/src/migration/types.ts | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/core/src/migration/index.ts b/packages/core/src/migration/index.ts index 21ac88d9b..090e7748c 100644 --- a/packages/core/src/migration/index.ts +++ b/packages/core/src/migration/index.ts @@ -109,7 +109,9 @@ const runMigration = async (pool: DatabasePool, file: string) => { const { up } = await importMigration(file); try { - await up(pool); + await pool.transaction(async (connect) => { + await up(connect); + }); } catch (error: unknown) { if (error instanceof Error) { console.log(`${chalk.red('[migration]')} run ${file} failed: ${error.message}.`); diff --git a/packages/core/src/migration/types.ts b/packages/core/src/migration/types.ts index 71f1f964c..f750275d7 100644 --- a/packages/core/src/migration/types.ts +++ b/packages/core/src/migration/types.ts @@ -1,4 +1,4 @@ -import { DatabasePool } from 'slonik'; +import { DatabaseTransactionConnection } from 'slonik'; import { z } from 'zod'; export const databaseVersionGuard = z.object({ @@ -9,6 +9,6 @@ export const databaseVersionGuard = z.object({ export type DatabaseVersion = z.infer; export type MigrationScript = { - up: (pool: DatabasePool) => Promise; - down: (pool: DatabasePool) => Promise; + up: (connection: DatabaseTransactionConnection) => Promise; + down: (connection: DatabaseTransactionConnection) => Promise; };