Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/cli/src/actions/action-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ export function getSchemaFile(file?: string) {
if (!fs.existsSync(pkgJsonConfig.schema)) {
throw new CliError(`Schema file not found: ${pkgJsonConfig.schema}`);
}
return pkgJsonConfig.schema;
if (fs.statSync(pkgJsonConfig.schema).isDirectory()) {
const schemaPath = path.join(pkgJsonConfig.schema, 'schema.zmodel');
if (!fs.existsSync(schemaPath)) {
throw new CliError(`Schema file not found: ${schemaPath}`);
}
return schemaPath;
} else {
return pkgJsonConfig.schema;
}
}

if (fs.existsSync('./zenstack/schema.zmodel')) {
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/actions/db.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'node:fs';
import { execPackage } from '../utils/exec-utils';
import { execPrisma } from '../utils/exec-utils';
import { generateTempPrismaSchema, getSchemaFile, handleSubProcessError } from './action-utils';

type Options = {
Expand Down Expand Up @@ -27,15 +27,15 @@ async function runPush(options: Options) {
try {
// run prisma db push
const cmd = [
'prisma db push',
'db push',
` --schema "${prismaSchemaFile}"`,
options.acceptDataLoss ? ' --accept-data-loss' : '',
options.forceReset ? ' --force-reset' : '',
' --skip-generate',
].join('');

try {
await execPackage(cmd);
execPrisma(cmd);
} catch (err) {
handleSubProcessError(err);
}
Expand Down
40 changes: 18 additions & 22 deletions packages/cli/src/actions/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'node:fs';
import path from 'node:path';
import { CliError } from '../cli-error';
import { execPackage } from '../utils/exec-utils';
import { execPrisma } from '../utils/exec-utils';
import { generateTempPrismaSchema, getSchemaFile } from './action-utils';

type CommonOptions = {
Expand Down Expand Up @@ -64,69 +64,65 @@ export async function run(command: string, options: CommonOptions) {
}
}

async function runDev(prismaSchemaFile: string, options: DevOptions) {
function runDev(prismaSchemaFile: string, options: DevOptions) {
try {
const cmd = [
'prisma migrate dev',
'migrate dev',
` --schema "${prismaSchemaFile}"`,
' --skip-generate',
options.name ? ` --name ${options.name}` : '',
options.name ? ` --name "${options.name}"` : '',
options.createOnly ? ' --create-only' : '',
].join('');

await execPackage(cmd);
execPrisma(cmd);
} catch (err) {
handleSubProcessError(err);
}
}

async function runReset(prismaSchemaFile: string, options: ResetOptions) {
function runReset(prismaSchemaFile: string, options: ResetOptions) {
try {
const cmd = [
'prisma migrate reset',
'migrate reset',
` --schema "${prismaSchemaFile}"`,
' --skip-generate',
options.force ? ' --force' : '',
].join('');

await execPackage(cmd);
execPrisma(cmd);
} catch (err) {
handleSubProcessError(err);
}
}

async function runDeploy(prismaSchemaFile: string, _options: DeployOptions) {
function runDeploy(prismaSchemaFile: string, _options: DeployOptions) {
try {
const cmd = ['prisma migrate deploy', ` --schema "${prismaSchemaFile}"`].join('');

await execPackage(cmd);
const cmd = ['migrate deploy', ` --schema "${prismaSchemaFile}"`].join('');
execPrisma(cmd);
} catch (err) {
handleSubProcessError(err);
}
}

async function runStatus(prismaSchemaFile: string, _options: StatusOptions) {
function runStatus(prismaSchemaFile: string, _options: StatusOptions) {
try {
await execPackage(`prisma migrate status --schema "${prismaSchemaFile}"`);
execPrisma(`migrate status --schema "${prismaSchemaFile}"`);
} catch (err) {
handleSubProcessError(err);
}
}

async function runResolve(prismaSchemaFile: string, options: ResolveOptions) {
function runResolve(prismaSchemaFile: string, options: ResolveOptions) {
if (!options.applied && !options.rolledBack) {
throw new CliError('Either --applied or --rolled-back option must be provided');
}

try {
const cmd = [
'prisma migrate resolve',
'migrate resolve',
` --schema "${prismaSchemaFile}"`,
options.applied ? ` --applied ${options.applied}` : '',
options.rolledBack ? ` --rolled-back ${options.rolledBack}` : '',
options.applied ? ` --applied "${options.applied}"` : '',
options.rolledBack ? ` --rolled-back "${options.rolledBack}"` : '',
].join('');

await execPackage(cmd);
execPrisma(cmd);
} catch (err) {
handleSubProcessError(err);
}
Expand Down
16 changes: 16 additions & 0 deletions packages/cli/src/utils/exec-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { execSync as _exec, type ExecSyncOptions } from 'child_process';
import { fileURLToPath } from 'url';

/**
* Utility for executing command synchronously and prints outputs on current console
Expand All @@ -24,3 +25,18 @@ export function execPackage(
const packageManager = process?.versions?.['bun'] ? 'bunx' : 'npx';
execSync(`${packageManager} ${cmd}`, options);
}

/**
* Utility for running prisma commands
*/
export function execPrisma(args: string, options?: Omit<ExecSyncOptions, 'env'> & { env?: Record<string, string> }) {
let prismaPath: string;
if (typeof import.meta.resolve === 'function') {
// esm
prismaPath = fileURLToPath(import.meta.resolve('prisma/build/index.js'));
} else {
// cjs
prismaPath = require.resolve('prisma/build/index.js');
}
execSync(`node ${prismaPath} ${args}`, options);
}
15 changes: 15 additions & 0 deletions packages/cli/test/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ describe('CLI generate command test', () => {
expect(fs.existsSync(path.join(workDir, 'bar/schema.ts'))).toBe(true);
});

it('should respect package.json schema dir config', () => {
const workDir = createProject(model);
fs.mkdirSync(path.join(workDir, 'foo'));
fs.renameSync(path.join(workDir, 'zenstack/schema.zmodel'), path.join(workDir, 'foo/schema.zmodel'));
fs.rmdirSync(path.join(workDir, 'zenstack'));
const pkgJson = JSON.parse(fs.readFileSync(path.join(workDir, 'package.json'), 'utf8'));
pkgJson.zenstack = {
schema: './foo',
output: './bar',
};
fs.writeFileSync(path.join(workDir, 'package.json'), JSON.stringify(pkgJson, null, 2));
runCli('generate', workDir);
expect(fs.existsSync(path.join(workDir, 'bar/schema.ts'))).toBe(true);
});

it('should respect lite option', () => {
const workDir = createProject(model);
runCli('generate --lite', workDir);
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/test/migrate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ model User {
}
`;

// skip due to timeout in CI
describe.skip('CLI migrate commands test', () => {
describe('CLI migrate commands test', () => {
it('should generate a database with migrate dev', () => {
const workDir = createProject(model);
runCli('migrate dev --name init', workDir);
Expand Down
Loading