-
-
Notifications
You must be signed in to change notification settings - Fork 12
merge dev to main #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,4 +78,3 @@ jobs: | |
|
|
||
| ${{ steps.changelog.outputs.changelog }} | ||
| draft: true | ||
| prerelease: true | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| { | ||
| "name": "zenstack-v3", | ||
| "version": "3.0.0-alpha.6", | ||
| "version": "3.0.0-alpha.7", | ||
| "description": "ZenStack", | ||
| "packageManager": "[email protected]", | ||
| "scripts": { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,43 +1,44 @@ | ||
| import path from 'node:path'; | ||
| import fs from 'node:fs'; | ||
| import { execPackage } from '../utils/exec-utils'; | ||
| import { getSchemaFile, handleSubProcessError } from './action-utils'; | ||
| import { run as runGenerate } from './generate'; | ||
| import { generateTempPrismaSchema, getSchemaFile, handleSubProcessError } from './action-utils'; | ||
|
|
||
| type CommonOptions = { | ||
| type Options = { | ||
| schema?: string; | ||
| name?: string; | ||
| acceptDataLoss?: boolean; | ||
| forceReset?: boolean; | ||
| }; | ||
|
|
||
| /** | ||
| * CLI action for db related commands | ||
| */ | ||
| export async function run(command: string, options: CommonOptions) { | ||
| const schemaFile = getSchemaFile(options.schema); | ||
|
|
||
| // run generate first | ||
| await runGenerate({ | ||
| schema: schemaFile, | ||
| silent: true, | ||
| }); | ||
|
|
||
| const prismaSchemaFile = path.join(path.dirname(schemaFile), 'schema.prisma'); | ||
|
|
||
| export async function run(command: string, options: Options) { | ||
| switch (command) { | ||
| case 'push': | ||
| await runPush(prismaSchemaFile, options); | ||
| await runPush(options); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| async function runPush(prismaSchemaFile: string, options: any) { | ||
| const cmd = `prisma db push --schema "${prismaSchemaFile}"${ | ||
| options.acceptDataLoss ? ' --accept-data-loss' : '' | ||
| }${options.forceReset ? ' --force-reset' : ''} --skip-generate`; | ||
| async function runPush(options: Options) { | ||
| // generate a temp prisma schema file | ||
| const schemaFile = getSchemaFile(options.schema); | ||
| const prismaSchemaFile = await generateTempPrismaSchema(schemaFile); | ||
|
|
||
| try { | ||
| await execPackage(cmd, { | ||
| stdio: 'inherit', | ||
| }); | ||
| } catch (err) { | ||
| handleSubProcessError(err); | ||
| // run prisma db push | ||
| const cmd = `prisma db push --schema "${prismaSchemaFile}"${ | ||
| options.acceptDataLoss ? ' --accept-data-loss' : '' | ||
| }${options.forceReset ? ' --force-reset' : ''} --skip-generate`; | ||
| try { | ||
| await execPackage(cmd, { | ||
| stdio: 'inherit', | ||
| }); | ||
| } catch (err) { | ||
| handleSubProcessError(err); | ||
| } | ||
| } finally { | ||
| if (fs.existsSync(prismaSchemaFile)) { | ||
| fs.unlinkSync(prismaSchemaFile); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { createProject, runCli } from './utils'; | ||
|
|
||
| const model = ` | ||
| model User { | ||
| id String @id @default(cuid()) | ||
| } | ||
| `; | ||
|
|
||
| describe('CLI db commands test', () => { | ||
| it('should generate a database with db push', () => { | ||
| const workDir = createProject(model); | ||
| runCli('db push', workDir); | ||
| expect(fs.existsSync(path.join(workDir, 'zenstack/dev.db'))).toBe(true); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { createProject, runCli } from './utils'; | ||
|
|
||
| const model = ` | ||
| model User { | ||
| id String @id @default(cuid()) | ||
| } | ||
| `; | ||
|
|
||
| describe('CLI generate command test', () => { | ||
| it('should generate a TypeScript schema', () => { | ||
| const workDir = createProject(model); | ||
| runCli('generate', workDir); | ||
| expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true); | ||
| expect(fs.existsSync(path.join(workDir, 'zenstack/schema.prisma'))).toBe(false); | ||
| }); | ||
|
|
||
| it('should respect custom output directory', () => { | ||
| const workDir = createProject(model); | ||
| runCli('generate --output ./zen', workDir); | ||
| expect(fs.existsSync(path.join(workDir, 'zen/schema.ts'))).toBe(true); | ||
| }); | ||
|
|
||
| it('should respect custom schema location', () => { | ||
| const workDir = createProject(model); | ||
| fs.renameSync(path.join(workDir, 'zenstack/schema.zmodel'), path.join(workDir, 'zenstack/foo.zmodel')); | ||
| runCli('generate --schema ./zenstack/foo.zmodel', workDir); | ||
| expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true); | ||
| }); | ||
|
|
||
| it('should respect save prisma schema option', () => { | ||
| const workDir = createProject(model); | ||
| runCli('generate --save-prisma-schema', workDir); | ||
| expect(fs.existsSync(path.join(workDir, 'zenstack/schema.prisma'))).toBe(true); | ||
| }); | ||
|
|
||
| it('should respect save prisma schema custom path option', () => { | ||
| const workDir = createProject(model); | ||
| runCli('generate --save-prisma-schema "../prisma/schema.prisma"', workDir); | ||
| expect(fs.existsSync(path.join(workDir, 'prisma/schema.prisma'))).toBe(true); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import tmp from 'tmp'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { runCli } from './utils'; | ||
|
|
||
| describe('Cli init command tests', () => { | ||
| it('should create a new project', () => { | ||
| const { name: workDir } = tmp.dirSync({ unsafeCleanup: true }); | ||
| runCli('init', workDir); | ||
| expect(fs.existsSync(path.join(workDir, 'zenstack/schema.zmodel'))).toBe(true); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Address potential race conditions and improve error handling.
The current implementation has several concerns:
~schema.prismacould cause conflicts if multiple processes run simultaneously in the same directory.fs.writeFileSyncin an async function context is inconsistent and could block the event loop.Consider this improved implementation:
export async function generateTempPrismaSchema(zmodelPath: string) { const model = await loadSchemaDocument(zmodelPath); const prismaSchema = await new PrismaSchemaGenerator(model).generate(); - const prismaSchemaFile = path.resolve(path.dirname(zmodelPath), '~schema.prisma'); - fs.writeFileSync(prismaSchemaFile, prismaSchema); + const prismaSchemaFile = path.resolve(path.dirname(zmodelPath), `~schema-${Date.now()}-${Math.random().toString(36).substr(2, 9)}.prisma`); + await fs.promises.writeFile(prismaSchemaFile, prismaSchema); return prismaSchemaFile; }This addresses the race condition with a unique filename and uses async file operations for consistency.
📝 Committable suggestion
🤖 Prompt for AI Agents