Skip to content

Commit 5921d74

Browse files
committed
Add prisma migration workaround for views
1 parent 01e9319 commit 5921d74

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

.editorconfig

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
indent_size = 2
2+
indent_style = space

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"db:ui": "prisma studio",
1515
"db:pull": "prisma db pull",
1616
"db:push": "prisma db push",
17-
"db:migrate": "prisma migrate dev",
17+
"db:migrate": "node scripts/prisma-migrate-with-views-workaround.mjs",
1818
"db:generate": "prisma generate"
1919
},
2020
"prisma": {

prisma/schema.prisma

+2
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ model KeyValue {
270270
value Json
271271
}
272272

273+
/// @view
273274
model ModelRank {
274275
model Model @relation(fields: [modelId], references: [id], onDelete: NoAction)
275276
modelId Int @id
@@ -290,6 +291,7 @@ model ModelRank {
290291
ratingAllTime Int
291292
}
292293

294+
/// @view
293295
model ModelVersionRank {
294296
modelVersion ModelVersion @relation(fields: [modelVersionId], references: [id], onDelete: NoAction)
295297
modelVersionId Int @id
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const SCHEMA = 'prisma/schema.prisma';
2+
const BACKUP = 'prisma/schema.prisma.backup';
3+
const ANNOTATION = '/// @view';
4+
5+
import { readFile, writeFile, copyFile, rename, unlink } from 'fs/promises';
6+
import { argv } from 'process';
7+
import { spawnSync } from 'child_process';
8+
const spawnOptions = { stdio: 'inherit', shell: true };
9+
10+
// Backup the schema
11+
await copyFile(SCHEMA, BACKUP);
12+
console.log('Backed up schema to', BACKUP);
13+
14+
// Remove all views
15+
console.log('Removing views from schema...');
16+
const schema = await readFile(SCHEMA, 'utf-8');
17+
const viewRegex = new RegExp(`\n?${ANNOTATION}\nmodel ([a-zA-Z]+) {[^}]+}\n?`, 'g');
18+
const modelNames = [...schema.matchAll(viewRegex)].map(([_, name]) => name);
19+
let modifiedSchema = schema.replace(viewRegex, '');
20+
for (const modelName of modelNames) {
21+
const modelRegex = new RegExp(`^.+${modelName}.*\n?`, 'gm');
22+
modifiedSchema = modifiedSchema.replace(modelRegex, '');
23+
}
24+
await writeFile(SCHEMA, modifiedSchema, 'utf-8');
25+
console.log('Removing views from schema... Done');
26+
27+
try {
28+
// Run the migration
29+
console.log('Running `prisma migrate dev`... ');
30+
let { error } = spawnSync('prisma migrate dev', argv.slice(2), spawnOptions);
31+
if (error) throw error;
32+
console.log('Running `prisma migrate dev`... Done');
33+
34+
// Restore the schema
35+
console.log('Restoring backup and running `npx prisma generate`... ');
36+
await unlink(SCHEMA);
37+
await rename(BACKUP, SCHEMA);
38+
({ error } = spawnSync('prisma generate', spawnOptions));
39+
if (error) throw error;
40+
console.log('Restoring backup and running `npx prisma generate`... Done');
41+
} catch (error) {
42+
// Restore the schema
43+
console.log('Restoring backup... ');
44+
await unlink(SCHEMA);
45+
await rename(BACKUP, SCHEMA);
46+
console.log('Restoring backup... Done');
47+
48+
// Rethrow the error
49+
throw error;
50+
}

0 commit comments

Comments
 (0)