Skip to content

Commit

Permalink
fix(delegate): don't generate both "@@Schema" attributes from base an…
Browse files Browse the repository at this point in the history
…d sub models
  • Loading branch information
ymc9 committed Jan 6, 2025
1 parent 3ee50d3 commit 2017e62
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
17 changes: 16 additions & 1 deletion packages/schema/src/utils/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ import {
ModelImport,
TypeDef,
} from '@zenstackhq/language/ast';
import { getAttribute, getInheritanceChain, getRecursiveBases, isDelegateModel, isFromStdlib } from '@zenstackhq/sdk';
import {
getAttribute,
getInheritanceChain,
getRecursiveBases,
hasAttribute,
isDelegateModel,
isFromStdlib,
} from '@zenstackhq/sdk';
import {
AstNode,
copyAstNode,
Expand Down Expand Up @@ -96,6 +103,9 @@ function filterBaseAttribute(forModel: DataModel, base: DataModel, attr: DataMod
// uninheritable attributes for delegate inheritance (they reference fields from the base)
const uninheritableFromDelegateAttributes = ['@@unique', '@@index', '@@fulltext'];

// attributes that are inherited but can be overridden
const overrideAttributes = ['@@schema'];

if (uninheritableAttributes.includes(attr.decl.$refText)) {
return false;
}
Expand All @@ -109,6 +119,11 @@ function filterBaseAttribute(forModel: DataModel, base: DataModel, attr: DataMod
return false;
}

if (hasAttribute(forModel, attr.decl.$refText) && overrideAttributes.includes(attr.decl.$refText)) {
// don't inherit an attribute if it's overridden in the sub model
return false;
}

return true;
}

Expand Down
69 changes: 69 additions & 0 deletions tests/regression/tests/issue-1647.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { loadSchema } from '@zenstackhq/testtools';
import fs from 'fs';

describe('issue 1647', () => {
it('inherits @@schema by default', async () => {
const { projectDir } = await loadSchema(
`
datasource db {
provider = 'postgresql'
url = env('DATABASE_URL')
schemas = ['public', 'post']
}
generator client {
provider = 'prisma-client-js'
previewFeatures = ['multiSchema']
}
model Asset {
id Int @id
type String
@@delegate(type)
@@schema('public')
}
model Post extends Asset {
title String
}
`,
{ addPrelude: false, pushDb: false, getPrismaOnly: true }
);

const prismaSchema = fs.readFileSync(`${projectDir}/prisma/schema.prisma`, 'utf-8');
expect(prismaSchema.split('\n').filter((l) => l.includes('@@schema("public")'))).toHaveLength(2);
});
it('respects sub model @@schema overrides', async () => {
const { projectDir } = await loadSchema(
`
datasource db {
provider = 'postgresql'
url = env('DATABASE_URL')
schemas = ['public', 'post']
}
generator client {
provider = 'prisma-client-js'
previewFeatures = ['multiSchema']
}
model Asset {
id Int @id
type String
@@delegate(type)
@@schema('public')
}
model Post extends Asset {
title String
@@schema('post')
}
`,
{ addPrelude: false, pushDb: false, getPrismaOnly: true }
);

const prismaSchema = fs.readFileSync(`${projectDir}/prisma/schema.prisma`, 'utf-8');
expect(prismaSchema.split('\n').filter((l) => l.includes('@@schema("public")'))).toHaveLength(1);
expect(prismaSchema.split('\n').filter((l) => l.includes('@@schema("post")'))).toHaveLength(1);
});
});

0 comments on commit 2017e62

Please sign in to comment.