|
1 | 1 | import fs from 'node:fs'; |
2 | 2 | import path from 'node:path'; |
3 | 3 | import { describe, expect, it } from 'vitest'; |
4 | | -import { createProject, runCli } from '../utils'; |
| 4 | +import { createProject, getDefaultPrelude, runCli } from '../utils'; |
| 5 | +import { loadSchemaDocument } from '../../src/actions/action-utils'; |
| 6 | +import { ZModelCodeGenerator } from '@zenstackhq/language'; |
5 | 7 |
|
6 | 8 | const getSchema = (workDir: string) => fs.readFileSync(path.join(workDir, 'zenstack/schema.zmodel')).toString(); |
| 9 | +const generator = new ZModelCodeGenerator({ |
| 10 | + quote: 'double', |
| 11 | + indent: 4, |
| 12 | +}); |
7 | 13 |
|
8 | 14 | describe('DB pull', () => { |
9 | | - it('simple schema', () => { |
| 15 | + it("simple schema - pull shouldn't modify the schema", () => { |
10 | 16 | const workDir = createProject( |
11 | | -`model User { |
| 17 | + `model User { |
12 | 18 | id String @id @default(cuid()) |
13 | 19 | email String @unique @map("email_address") |
14 | 20 | name String? @default("Anonymous") |
@@ -85,12 +91,98 @@ enum Role { |
85 | 91 | USER |
86 | 92 | ADMIN |
87 | 93 | MODERATOR |
88 | | -}`); |
| 94 | +}`, |
| 95 | + ); |
89 | 96 | runCli('format', workDir); |
90 | 97 | runCli('db push', workDir); |
91 | 98 |
|
92 | 99 | const originalSchema = getSchema(workDir); |
93 | 100 | runCli('db pull --indent 4', workDir); |
94 | 101 | expect(getSchema(workDir)).toEqual(originalSchema); |
95 | 102 | }); |
| 103 | + |
| 104 | + it('simple schema - pull shouldn recreate the schema.zmodel', async () => { |
| 105 | + const workDir = createProject( |
| 106 | + `model Post { |
| 107 | + id Int @id @default(autoincrement()) |
| 108 | + authorId String |
| 109 | + title String |
| 110 | + content String? |
| 111 | + published Boolean @default(false) |
| 112 | + createdAt DateTime @default(now()) |
| 113 | + updatedAt DateTime @updatedAt |
| 114 | + slug String |
| 115 | + score Float @default(0.0) |
| 116 | + metadata Json? |
| 117 | + author User @relation(fields: [authorId], references: [id], onDelete: Cascade) |
| 118 | + PostTag PostTag[] |
| 119 | +
|
| 120 | + @@unique([authorId, slug]) |
| 121 | + @@index([authorId, published]) |
| 122 | +} |
| 123 | +model PostTag { |
| 124 | + post Post @relation(fields: [postId], references: [id], onDelete: Cascade) |
| 125 | + postId Int |
| 126 | + tag Tag @relation(fields: [tagId], references: [id], onDelete: Cascade) |
| 127 | + tagId Int |
| 128 | + assignedAt DateTime @default(now()) |
| 129 | + note String? @default("initial") |
| 130 | +
|
| 131 | + @@id([postId, tagId]) |
| 132 | +} |
| 133 | +model User { |
| 134 | + id String @id @default(cuid()) |
| 135 | + email String @unique |
| 136 | + name String? @default("Anonymous") |
| 137 | + role Role @default(USER) |
| 138 | + profile Profile? |
| 139 | + shared_profile Profile? @relation("shared") |
| 140 | + posts Post[] |
| 141 | + createdAt DateTime @default(now()) |
| 142 | + updatedAt DateTime @updatedAt |
| 143 | + jsonData Json? |
| 144 | + balance Decimal @default(0.00) |
| 145 | + isActive Boolean @default(true) |
| 146 | + bigCounter BigInt @default(0) |
| 147 | + bytes Bytes? |
| 148 | +
|
| 149 | + @@index([role]) |
| 150 | +} |
| 151 | +
|
| 152 | +model Profile { |
| 153 | + id Int @id @default(autoincrement()) |
| 154 | + user User @relation(fields: [userId], references: [id], onDelete: Cascade) |
| 155 | + userId String @unique |
| 156 | + user_shared User @relation("shared", fields: [shared_userId], references: [id], onDelete: Cascade) |
| 157 | + shared_userId String @unique |
| 158 | + bio String? |
| 159 | + avatarUrl String? |
| 160 | +} |
| 161 | +
|
| 162 | +model Tag { |
| 163 | + id Int @id @default(autoincrement()) |
| 164 | + name String @unique |
| 165 | + posts PostTag[] |
| 166 | + createdAt DateTime @default(now()) |
| 167 | +
|
| 168 | + @@index([name], name: "tag_name_idx") |
| 169 | +} |
| 170 | +
|
| 171 | +enum Role { |
| 172 | + USER |
| 173 | + ADMIN |
| 174 | + MODERATOR |
| 175 | +}`, |
| 176 | + ); |
| 177 | + console.log(workDir) |
| 178 | + runCli('format', workDir); |
| 179 | + runCli('db push', workDir); |
| 180 | + const schemaFile = path.join(workDir, 'zenstack/schema.zmodel'); |
| 181 | + const { model } = await loadSchemaDocument(schemaFile, { returnServices: true }); |
| 182 | + const originalSchema = generator.generate(model); |
| 183 | + fs.writeFileSync(path.join(workDir, 'zenstack/schema.zmodel'), getDefaultPrelude()); |
| 184 | + |
| 185 | + runCli('db pull --indent 4 --field-casing=camel', workDir); |
| 186 | + expect(getSchema(workDir)).toEqual(originalSchema); |
| 187 | + }); |
96 | 188 | }); |
0 commit comments