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
16 changes: 15 additions & 1 deletion packages/sdk/src/prisma/prisma-schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,24 @@ export class PrismaSchemaGenerator {
this.generateEnumField(_enum, field);
}

for (const attr of decl.attributes.filter((attr) => this.isPrismaAttribute(attr))) {
const allAttributes = decl.attributes.filter((attr) => this.isPrismaAttribute(attr));
for (const attr of allAttributes) {
this.generateContainerAttribute(_enum, attr);
}

if (
this.datasourceHasSchemasSetting(decl.$container) &&
!allAttributes.some((attr) => attr.decl.ref?.name === '@@schema')
) {
// if the datasource declared `schemas` and no @@schema attribute is defined, add a default one
_enum.addAttribute('@@schema', [
new PrismaAttributeArg(
undefined,
new PrismaAttributeArgValue('String', this.getDefaultPostgresSchemaName(decl.$container)),
),
]);
}

// user defined comments pass-through
decl.comments.forEach((c) => _enum.addComment(c));
}
Expand Down
52 changes: 46 additions & 6 deletions tests/e2e/orm/client-api/pg-custom-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@ model Foo {
const foundSchema = { create: false, read: false, update: false, delete: false };
const db = await createTestClient(
`
enum Role {
ADMIN
USER
}

model Foo {
id Int @id
name String
role Role
}
`,
{
Expand All @@ -57,7 +63,9 @@ model Foo {
},
);

await expect(db.$qb.insertInto('Foo').values({ id: 1, name: 'test' }).execute()).toResolveTruthy();
await expect(
db.$qb.insertInto('Foo').values({ id: 1, name: 'test', role: 'ADMIN' }).execute(),
).toResolveTruthy();
await expect(db.$qb.selectFrom('Foo').selectAll().executeTakeFirst()).toResolveTruthy();
await expect(
db.$qb.updateTable('Foo').set({ name: 'updated' }).where('id', '=', 1).execute(),
Expand All @@ -75,17 +83,23 @@ datasource db {
defaultSchema = 'mySchema'
}

enum Role {
ADMIN
USER
}

model Foo {
id Int @id
name String
role Role
}
`,
{
provider: 'postgresql',
},
);

await expect(db.foo.create({ data: { id: 1, name: 'test' } })).rejects.toSatisfy(
await expect(db.foo.create({ data: { id: 1, name: 'test', role: 'ADMIN' } })).rejects.toSatisfy(
(e) => e instanceof ORMError && !!e.dbErrorMessage?.includes('relation "mySchema.Foo" does not exist'),
);

Expand All @@ -98,17 +112,23 @@ datasource db {
defaultSchema = 'public'
}

enum Role {
ADMIN
USER
}

model Foo {
id Int @id
name String
role Role
}
`,
{
provider: 'postgresql',
},
);

await expect(db1.foo.create({ data: { id: 1, name: 'test' } })).toResolveTruthy();
await expect(db1.foo.create({ data: { id: 1, name: 'test', role: 'ADMIN' } })).toResolveTruthy();
});

it('supports custom schemas', async () => {
Expand All @@ -123,15 +143,29 @@ datasource db {
url = '$DB_URL'
}

enum FooRole {
ADMIN
USER
@@schema('mySchema')
}

model Foo {
id Int @id
name String
role FooRole
@@schema('mySchema')
}

enum BarRole {
ADMIN
USER
@@schema('public')
}

model Bar {
id Int @id
name String
role BarRole
@@schema('public')
}
`,
Expand All @@ -150,8 +184,8 @@ model Bar {
},
);

await expect(db.foo.create({ data: { id: 1, name: 'test' } })).toResolveTruthy();
await expect(db.bar.create({ data: { id: 1, name: 'test' } })).toResolveTruthy();
await expect(db.foo.create({ data: { id: 1, name: 'test', role: 'ADMIN' } })).toResolveTruthy();
await expect(db.bar.create({ data: { id: 1, name: 'test', role: 'USER' } })).toResolveTruthy();

expect(fooQueriesVerified).toBe(true);
expect(barQueriesVerified).toBe(true);
Expand Down Expand Up @@ -226,9 +260,15 @@ datasource db {
url = '$DB_URL'
}

enum Role {
ADMIN
USER
}

model Foo {
id Int @id
name String
role Role
@@schema('mySchema')
}

Expand All @@ -252,7 +292,7 @@ model Bar {
},
);

await expect(db.foo.create({ data: { id: 1, name: 'test' } })).toResolveTruthy();
await expect(db.foo.create({ data: { id: 1, name: 'test', role: 'ADMIN' } })).toResolveTruthy();
await expect(db.bar.create({ data: { id: 1, name: 'test' } })).toResolveTruthy();

expect(fooQueriesVerified).toBe(true);
Expand Down
Loading