-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.ts
68 lines (51 loc) · 2.27 KB
/
db.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { drizzle, MySql2Driver } from "drizzle-orm/mysql2";
import { migrate } from "drizzle-orm/mysql2/migrator";
import mysql from "mysql2/promise";
import * as dotenv from "dotenv";
dotenv.config({ path: '.env.development' });
const dbInfo = {
host: process.env.MYSQL_HOST,
port: process.env.MYSQL_PORT,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
};
console.log("=====================================")
console.log("dbInfo:", dbInfo)
console.log("=====================================")
console.log("=====================================")
console.log("Start connecting to the database..")
const connection = mysql.createPool(dbInfo);
console.log("completed connection to the database.")
// console.log("connection result:", connection)
console.log("=====================================")
console.log("=====================================")
console.log("Start drizzle..")
export const db = drizzle(connection);
console.log("completed drizzle.")
// console.log("db:", db)
// マイグレーションファイルを実行
// すぐ実行するとエラーが発生することがあるため、最大30回までリトライする
for (let attempt = 1; attempt <= 30; attempt++) {
try {
await new Promise(resolve => setTimeout(resolve, 1000)); // 1秒待機
console.log("=====================================")
console.log(`マイグレーションファイルを実行.. 試行回数: ${attempt}`);
const migrationResult = await migrate(db, { migrationsFolder: "drizzle/migrations" });
console.log("completed exec migration.")
console.log("migrationResult:", migrationResult);
console.log("=====================================")
break; // 成功したらループを抜ける
} catch (error) {
console.error("=====================================")
console.error(`DB Init error on attempt ${attempt}:`, error)
console.error("=====================================")
if (attempt === 30) {
console.error("最大試行回数に達しました。マイグレーションに失敗しました。")
break;
}
}
}