1- import path from 'node:path ' ;
1+ import fs from 'node:fs ' ;
22import { 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' ;
54
65type CommonOptions = {
76 schema ?: string ;
7+ } ;
8+
9+ type DevOptions = CommonOptions & {
810 name ?: string ;
11+ createOnly ?: boolean ;
12+ } ;
13+
14+ type ResetOptions = CommonOptions & {
15+ force ?: boolean ;
916} ;
1017
18+ type DeployOptions = CommonOptions ;
19+
20+ type StatusOptions = CommonOptions ;
21+
1122/**
1223 * CLI action for migration-related commands
1324 */
1425export async function run ( command : string , options : CommonOptions ) {
1526 const schemaFile = getSchemaFile ( options . schema ) ;
27+ const prismaSchemaFile = await generateTempPrismaSchema ( schemaFile ) ;
1628
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 ;
2934
30- case 'reset' :
31- await runReset ( prismaSchemaFile , options as any ) ;
32- break ;
35+ case 'reset' :
36+ await runReset ( prismaSchemaFile , options as ResetOptions ) ;
37+ break ;
3338
34- case 'deploy' :
35- await runDeploy ( prismaSchemaFile , options ) ;
36- break ;
39+ case 'deploy' :
40+ await runDeploy ( prismaSchemaFile , options as DeployOptions ) ;
41+ break ;
3742
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+ }
4151 }
4252}
4353
44- async function runDev ( prismaSchemaFile : string , _options : unknown ) {
54+ async function runDev ( prismaSchemaFile : string , options : DevOptions ) {
4555 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+ ) ;
4962 } catch ( err ) {
5063 handleSubProcessError ( err ) ;
5164 }
5265}
5366
54- async function runReset ( prismaSchemaFile : string , options : { force : boolean } ) {
67+ async function runReset ( prismaSchemaFile : string , options : ResetOptions ) {
5568 try {
5669 await execPackage ( `prisma migrate reset --schema "${ prismaSchemaFile } "${ options . force ? ' --force' : '' } ` , {
5770 stdio : 'inherit' ,
@@ -61,7 +74,7 @@ async function runReset(prismaSchemaFile: string, options: { force: boolean }) {
6174 }
6275}
6376
64- async function runDeploy ( prismaSchemaFile : string , _options : unknown ) {
77+ async function runDeploy ( prismaSchemaFile : string , _options : DeployOptions ) {
6578 try {
6679 await execPackage ( `prisma migrate deploy --schema "${ prismaSchemaFile } "` , {
6780 stdio : 'inherit' ,
@@ -71,7 +84,7 @@ async function runDeploy(prismaSchemaFile: string, _options: unknown) {
7184 }
7285}
7386
74- async function runStatus ( prismaSchemaFile : string , _options : unknown ) {
87+ async function runStatus ( prismaSchemaFile : string , _options : StatusOptions ) {
7588 try {
7689 await execPackage ( `prisma migrate status --schema "${ prismaSchemaFile } "` , {
7790 stdio : 'inherit' ,
0 commit comments