-
-
Notifications
You must be signed in to change notification settings - Fork 765
/
Copy pathdb-migrate.mjs
executable file
·83 lines (66 loc) · 2.12 KB
/
db-migrate.mjs
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env zx
import { parseDsn as parse } from '@httpx/dsn-parser';
const env = $.env;
let isCi = ['true', '1'].includes(env?.CI ?? '');
const buildVersion = env.BUILD_VERSION;
const databaseUrl = env.PRISMA_DATABASE_URL;
const parseDsn = (dsn) => {
const parsedDsn = parse(dsn);
if (!parsedDsn.success) {
throw new Error(`DATABASE_URL ${parsedDsn.reason}`);
}
if (!parsedDsn.value.port) {
throw new Error(`DATABASE_URL must provide a port`);
}
return parsedDsn.value;
};
const pgMigrate = async () => {
cd('postgres_migrate');
return await $`prisma migrate deploy`;
};
const killMe = async () => {
await $`exit 0`;
};
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const retryOperation = async (operation, maxRetries = 5, delay = 3000) => {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
await operation();
return;
} catch (error) {
if (attempt === maxRetries) {
throw error;
}
console.log(`Attempt ${attempt} failed. Retrying in ${delay/1000} seconds...`);
await sleep(delay);
}
}
};
console.log(`DB Migrate Version: ${buildVersion}`);
await $`prisma -v`;
const { driver, host, port } = parseDsn(databaseUrl);
const adapters = {
postgresql: pgMigrate,
postgres: pgMigrate,
};
if (!driver || !adapters[driver]) {
throw new Error(`Adapter ${driver} is not allowed`);
}
console.log(`wait-for ${host}:${port} 【${driver}】deploying.`);
try {
await retryOperation(async () => {
const result =
await $`scripts/wait-for ${host}:${port} --timeout=15 -- echo 'database driver:【${driver}】started successfully.'`;
if (result.exitCode !== 0) {
console.error(`database driver:【${driver}】, startup exception is about to exit.`);
throw new Error(result.stderr);
}
console.log(`database driver:【${driver}】, ready to start migration.`);
await adapters[driver]();
console.log(`database driver:【${driver}】, migration success.`);
});
} catch (p) {
console.error(`Exit code: ${p.exitCode}`);
console.error(`Migrate Deploy Error: ${p.stderr}`);
await killMe();
}