Skip to content

Commit dc5cb75

Browse files
authored
fix: improve prisma scripts (#563)
1 parent 8930121 commit dc5cb75

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

packages/wb/src/commands/prisma.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ const resetCommand: CommandModule<unknown, InferredOptionTypes<typeof builder>>
137137
for (const project of prepareForRunningCommand('prisma reset', allProjects)) {
138138
await runWithSpawn(prismaScripts.reset(project, unknownOptions), project, argv);
139139
}
140+
// Force to reset test database
141+
if (process.env.WB_ENV !== 'test') {
142+
process.env.WB_ENV = 'test';
143+
for (const project of prepareForRunningCommand('WB_ENV=test prisma reset', await findPrismaProjects(argv))) {
144+
await runWithSpawn(prismaScripts.reset(project, unknownOptions), project, argv);
145+
}
146+
}
140147
},
141148
};
142149

packages/wb/src/scripts/prismaScripts.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ class PrismaScripts {
2323

2424
deployForce(project: Project): string {
2525
const dirPath = getDatabaseDirPath(project);
26+
// `prisma migrate reset` sometimes fails if the existing database schema, so we remove it first.
2627
// Don't skip "migrate deploy" because restored database may be older than the current schema.
27-
return `PRISMA migrate reset --force --skip-seed && rm -Rf ${dirPath}/prod.sqlite3*
28+
return `rm -Rf ${dirPath}/prod.sqlite3*; PRISMA migrate reset --force --skip-seed && rm -Rf ${dirPath}/prod.sqlite3*
2829
&& litestream restore -config litestream.yml -o ${dirPath}/prod.sqlite3 ${dirPath}/prod.sqlite3 && ls -ahl ${dirPath}/prod.sqlite3 && ALLOW_TO_SKIP_SEED=0 PRISMA migrate deploy`;
2930
}
3031

@@ -43,18 +44,16 @@ class PrismaScripts {
4344

4445
reset(project: Project, additionalOptions = ''): string {
4546
// cf. https://www.prisma.io/docs/guides/database/seed-database#integrated-seeding-with-prisma-migrate
46-
const resetOptions = additionalOptions.trim();
47-
const baseReset = `PRISMA migrate reset --force ${resetOptions}`;
48-
const resetCommand = project.packageJson.dependencies?.blitz ? `${baseReset} && ${this.seed(project)}` : baseReset;
49-
const resetCommandForTest = project.packageJson.dependencies?.blitz
50-
? String.raw`find db \( -name "test.db*" -o -name "test.sqlite*" \) -delete`
51-
: String.raw`find prisma \( -name "test.db*" -o -name "test.sqlite*" \) -delete`;
52-
return `${resetCommand} && ${resetCommandForTest}`;
47+
if (project.packageJson.dependencies?.blitz) {
48+
// Blitz does not trigger seed automatically, so we need to run it manually.
49+
return `PRISMA migrate reset --force ${additionalOptions} && ${this.seed(project)}`;
50+
}
51+
return `PRISMA migrate reset --force ${additionalOptions}`;
5352
}
5453

5554
restore(project: Project, outputPath: string): string {
5655
const dirPath = getDatabaseDirPath(project);
57-
return `${this.removeSqliteArtifacts(outputPath)}; litestream restore -config litestream.yml -o ${outputPath} ${dirPath}/prod.sqlite3`;
56+
return `rm -Rf ${outputPath}*; litestream restore -config litestream.yml -o ${outputPath} ${dirPath}/prod.sqlite3`;
5857
}
5958

6059
seed(project: Project, scriptPath?: string): string {
@@ -117,11 +116,6 @@ const prisma = new PrismaClient();
117116
}
118117
return `${prefix}PRISMA studio ${additionalOptions}`;
119118
}
120-
121-
private removeSqliteArtifacts(sqlitePath: string): string {
122-
// Litestream requires removing WAL/SHM and Litestream sidecar files when recreating databases.
123-
return `rm -Rf ${sqlitePath} ${sqlitePath}-shm ${sqlitePath}-wal ${sqlitePath}-litestream`;
124-
}
125119
}
126120

127121
function getDatabaseDirPath(project: Project): string {

0 commit comments

Comments
 (0)