Skip to content

Commit 08f3105

Browse files
committed
use a transaction for stability
1 parent 6fe3d25 commit 08f3105

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

src/mysql/runMigrations.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,39 +43,39 @@ export async function runMigrations(
4343

4444
// Execute the migrations sql
4545
const path = `${migrationsPath}/${migration}/migration.sql`;
46+
let statements = ""; // Used to create the checksum
47+
// Execute the migrations line by line
48+
await prisma.$transaction(async (tx) => {
49+
const fileStream = fs.createReadStream(path);
50+
const rl = readline.createInterface({
51+
input: fileStream,
52+
crlfDelay: Infinity,
53+
});
4654

47-
// @TODO: Make this safer/ better
48-
// Read file line by line
49-
const fileStream = fs.createReadStream(path);
50-
const rl = readline.createInterface({
51-
input: fileStream,
52-
crlfDelay: Infinity,
53-
});
54-
55-
let statements = "";
56-
let statement = "";
55+
let statement = "";
5756

58-
for await (const line of rl) {
59-
// Skip comments and empty lines
60-
if (
61-
line.startsWith("--") ||
62-
line.startsWith("/*") ||
63-
line.trim() === ""
64-
) {
65-
continue;
66-
}
57+
for await (const line of rl) {
58+
// Skip comments and empty lines
59+
if (
60+
line.startsWith("--") ||
61+
line.startsWith("/*") ||
62+
line.trim() === ""
63+
) {
64+
continue;
65+
}
6766

68-
statement += line + " ";
67+
statement += line + " ";
6968

70-
// If the line ends with a semicolon, execute the statement
71-
if (line.trim().endsWith(";")) {
72-
await prisma.$executeRawUnsafe(statement);
73-
statements += statement;
69+
// If the line ends with a semicolon, execute the statement
70+
if (line.trim().endsWith(";")) {
71+
await tx.$executeRawUnsafe(statement);
72+
statements += statement;
7473

75-
// Reset statement
76-
statement = "";
74+
// Reset statement
75+
statement = "";
76+
}
7777
}
78-
}
78+
});
7979

8080
// Update the migration table
8181
const getFinishedDateTime = new Date();

src/types/PrismaClient/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { PrismaClientConfig } from "./prismaClientConfig";
22

3+
// @TODO: Get the real type
34
export type PrismaClient = {
45
_engine: {
56
config: PrismaClientConfig;
@@ -14,4 +15,5 @@ export type PrismaClient = {
1415
) => Promise<unknown>;
1516
$queryRawUnsafe: (query: string, ...values: any[]) => Promise<unknown>;
1617
$executeRawUnsafe: (query: string, ...values: any[]) => Promise<unknown>;
18+
$transaction: (queries: (fn: PrismaClient) => Promise<void>) => Promise<void>;
1719
};

0 commit comments

Comments
 (0)