1
- import path from 'node:path ' ;
1
+ import fs from 'node:fs ' ;
2
2
import { execPackage } from '../utils/exec-utils' ;
3
- import { getSchemaFile } from './action-utils' ;
4
- import { run as runGenerate } from './generate' ;
3
+ import { generateTempPrismaSchema , getSchemaFile } from './action-utils' ;
5
4
6
5
type CommonOptions = {
7
6
schema ?: string ;
7
+ } ;
8
+
9
+ type DevOptions = CommonOptions & {
8
10
name ?: string ;
11
+ createOnly ?: boolean ;
12
+ } ;
13
+
14
+ type ResetOptions = CommonOptions & {
15
+ force ?: boolean ;
9
16
} ;
10
17
18
+ type DeployOptions = CommonOptions ;
19
+
20
+ type StatusOptions = CommonOptions ;
21
+
11
22
/**
12
23
* CLI action for migration-related commands
13
24
*/
14
25
export async function run ( command : string , options : CommonOptions ) {
15
26
const schemaFile = getSchemaFile ( options . schema ) ;
27
+ const prismaSchemaFile = await generateTempPrismaSchema ( schemaFile ) ;
16
28
17
- // run generate first
18
- await runGenerate ( {
19
- schema : schemaFile ,
20
- silent : true ,
21
- } ) ;
22
-
23
- const prismaSchemaFile = path . join ( path . dirname ( schemaFile ) , 'schema.prisma' ) ;
24
-
25
- switch ( command ) {
26
- case 'dev' :
27
- await runDev ( prismaSchemaFile , options ) ;
28
- break ;
29
+ try {
30
+ switch ( command ) {
31
+ case 'dev' :
32
+ await runDev ( prismaSchemaFile , options as DevOptions ) ;
33
+ break ;
29
34
30
- case 'reset' :
31
- await runReset ( prismaSchemaFile , options as any ) ;
32
- break ;
35
+ case 'reset' :
36
+ await runReset ( prismaSchemaFile , options as ResetOptions ) ;
37
+ break ;
33
38
34
- case 'deploy' :
35
- await runDeploy ( prismaSchemaFile , options ) ;
36
- break ;
39
+ case 'deploy' :
40
+ await runDeploy ( prismaSchemaFile , options as DeployOptions ) ;
41
+ break ;
37
42
38
- case 'status' :
39
- await runStatus ( prismaSchemaFile , options ) ;
40
- break ;
43
+ case 'status' :
44
+ await runStatus ( prismaSchemaFile , options as StatusOptions ) ;
45
+ break ;
46
+ }
47
+ } finally {
48
+ if ( fs . existsSync ( prismaSchemaFile ) ) {
49
+ fs . unlinkSync ( prismaSchemaFile ) ;
50
+ }
41
51
}
42
52
}
43
53
44
- async function runDev ( prismaSchemaFile : string , _options : unknown ) {
54
+ async function runDev ( prismaSchemaFile : string , options : DevOptions ) {
45
55
try {
46
- await execPackage ( `prisma migrate dev --schema "${ prismaSchemaFile } " --skip-generate` , {
47
- stdio : 'inherit' ,
48
- } ) ;
56
+ await execPackage (
57
+ `prisma migrate dev --schema "${ prismaSchemaFile } " --skip-generate${ options . name ? ` --name ${ options . name } ` : '' } ${ options . createOnly ? ' --create-only' : '' } ` ,
58
+ {
59
+ stdio : 'inherit' ,
60
+ } ,
61
+ ) ;
49
62
} catch ( err ) {
50
63
handleSubProcessError ( err ) ;
51
64
}
52
65
}
53
66
54
- async function runReset ( prismaSchemaFile : string , options : { force : boolean } ) {
67
+ async function runReset ( prismaSchemaFile : string , options : ResetOptions ) {
55
68
try {
56
69
await execPackage ( `prisma migrate reset --schema "${ prismaSchemaFile } "${ options . force ? ' --force' : '' } ` , {
57
70
stdio : 'inherit' ,
@@ -61,7 +74,7 @@ async function runReset(prismaSchemaFile: string, options: { force: boolean }) {
61
74
}
62
75
}
63
76
64
- async function runDeploy ( prismaSchemaFile : string , _options : unknown ) {
77
+ async function runDeploy ( prismaSchemaFile : string , _options : DeployOptions ) {
65
78
try {
66
79
await execPackage ( `prisma migrate deploy --schema "${ prismaSchemaFile } "` , {
67
80
stdio : 'inherit' ,
@@ -71,7 +84,7 @@ async function runDeploy(prismaSchemaFile: string, _options: unknown) {
71
84
}
72
85
}
73
86
74
- async function runStatus ( prismaSchemaFile : string , _options : unknown ) {
87
+ async function runStatus ( prismaSchemaFile : string , _options : StatusOptions ) {
75
88
try {
76
89
await execPackage ( `prisma migrate status --schema "${ prismaSchemaFile } "` , {
77
90
stdio : 'inherit' ,
0 commit comments