Skip to content

Commit 07feac7

Browse files
authored
feat(cli): add --migrations option to migration commands (#89)
* feat(cli): add --migrations option to migration commands * update * update
1 parent 23ce49b commit 07feac7

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

packages/cli/src/actions/action-utils.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,13 @@ export function handleSubProcessError(err: unknown) {
5353
}
5454
}
5555

56-
export async function generateTempPrismaSchema(zmodelPath: string) {
56+
export async function generateTempPrismaSchema(zmodelPath: string, folder?: string) {
5757
const model = await loadSchemaDocument(zmodelPath);
5858
const prismaSchema = await new PrismaSchemaGenerator(model).generate();
59-
const prismaSchemaFile = path.resolve(path.dirname(zmodelPath), '~schema.prisma');
59+
if (!folder) {
60+
folder = path.dirname(zmodelPath);
61+
}
62+
const prismaSchemaFile = path.resolve(folder, '~schema.prisma');
6063
fs.writeFileSync(prismaSchemaFile, prismaSchema);
6164
return prismaSchemaFile;
6265
}

packages/cli/src/actions/migrate.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import fs from 'node:fs';
2+
import path from 'node:path';
23
import { execPackage } from '../utils/exec-utils';
34
import { generateTempPrismaSchema, getSchemaFile } from './action-utils';
45

56
type CommonOptions = {
67
schema?: string;
8+
migrations?: string;
79
};
810

911
type DevOptions = CommonOptions & {
@@ -24,7 +26,8 @@ type StatusOptions = CommonOptions;
2426
*/
2527
export async function run(command: string, options: CommonOptions) {
2628
const schemaFile = getSchemaFile(options.schema);
27-
const prismaSchemaFile = await generateTempPrismaSchema(schemaFile);
29+
const prismaSchemaDir = options.migrations ? path.dirname(options.migrations) : undefined;
30+
const prismaSchemaFile = await generateTempPrismaSchema(schemaFile, prismaSchemaDir);
2831

2932
try {
3033
switch (command) {

packages/cli/src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,31 +60,36 @@ export function createProgram() {
6060
.action(generateAction);
6161

6262
const migrateCommand = program.command('migrate').description('Update the database schema with migrations.');
63+
const migrationsOption = new Option('--migrations <path>', 'path for migrations');
6364

6465
migrateCommand
6566
.command('dev')
6667
.addOption(schemaOption)
6768
.addOption(new Option('-n, --name <name>', 'migration name'))
6869
.addOption(new Option('--create-only', 'only create migration, do not apply'))
70+
.addOption(migrationsOption)
6971
.description('Create a migration from changes in schema and apply it to the database.')
7072
.action((options) => migrateAction('dev', options));
7173

7274
migrateCommand
7375
.command('reset')
7476
.addOption(schemaOption)
7577
.addOption(new Option('--force', 'skip the confirmation prompt'))
78+
.addOption(migrationsOption)
7679
.description('Reset your database and apply all migrations, all data will be lost.')
7780
.action((options) => migrateAction('reset', options));
7881

7982
migrateCommand
8083
.command('deploy')
8184
.addOption(schemaOption)
85+
.addOption(migrationsOption)
8286
.description('Deploy your pending migrations to your production/staging database.')
8387
.action((options) => migrateAction('deploy', options));
8488

8589
migrateCommand
8690
.command('status')
8791
.addOption(schemaOption)
92+
.addOption(migrationsOption)
8893
.description('check the status of your database migrations.')
8994
.action((options) => migrateAction('status', options));
9095

0 commit comments

Comments
 (0)