Skip to content

Commit b10258e

Browse files
committed
separate out setup and init steps
1 parent 9421da5 commit b10258e

File tree

4 files changed

+45
-20
lines changed

4 files changed

+45
-20
lines changed

initialize.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* eslint-disable no-console */
2+
import { PrismaClient } from "@prisma/client";
3+
4+
const prisma = new PrismaClient();
5+
6+
/**
7+
* We set the the initializedAt key here, because this script is run when the
8+
* app is first deployed.
9+
**/
10+
async function setInitializedAt() {
11+
// Check if app is already initialized
12+
const initializedAt = await prisma.appSettings.findUnique({
13+
where: {
14+
key: 'initializedAt'
15+
}
16+
});
17+
18+
if (initializedAt) {
19+
console.log('App already initialized. Skipping.');
20+
return;
21+
}
22+
23+
const now = new Date().toISOString();
24+
25+
console.log(`Setting initializedAt to ${now}.`);
26+
27+
await prisma.appSettings.upsert({
28+
where: {
29+
key: 'initializedAt',
30+
},
31+
// No update emulates findOrCreate
32+
update: {},
33+
create: {
34+
key: 'initializedAt',
35+
value: now
36+
}
37+
});
38+
}
39+
40+
// Self executing function
41+
(async () => {
42+
await setInitializedAt();
43+
})();

migrate-and-start.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/sh
22

33
node setup-database.js
4+
node initialize.js
45
node server.js

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"ts-lint": "SKIP_ENV_VALIDATION=true tsc --noEmit --incremental",
1313
"ts-lint:watch": "SKIP_ENV_VALIDATION=true tsc --noEmit --incremental --watch",
1414
"start": "next start",
15-
"vercel-build": "node ./setup-database.js && next build",
15+
"vercel-build": "node ./setup-database.js && node ./initialize.js && next build",
1616
"knip": "knip",
1717
"test": "vitest",
1818
"load-test": "docker run -i grafana/k6 run - <load-test.js"

setup-database.js

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,26 +76,7 @@ async function handleMigrations() {
7676
}
7777
}
7878

79-
/**
80-
* We set the the initializedAt key here, because this script is run when the
81-
* app is first deployed.
82-
**/
83-
async function seedInitialisedAt() {
84-
await prisma.appSettings.upsert({
85-
where: {
86-
key: 'initializedAt',
87-
},
88-
// No update emulates findOrCreate
89-
update: {},
90-
create: {
91-
key: 'initializedAt',
92-
value: new Date().toISOString()
93-
}
94-
});
95-
}
9679

97-
// Self executing function
9880
(async () => {
9981
await handleMigrations();
100-
await seedInitialisedAt();
10182
})();

0 commit comments

Comments
 (0)