Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(client): multiSchema, test same table name - using manual SQL mi…
…gration Closes #15009 Similar as #15647 but using a SQL migration as Migrate support for multi-schema is not here yet
- Loading branch information
Showing
3 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
packages/client/src/__tests__/integration/errors/multi-schema/schema.prisma
This file contains 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,29 @@ | ||
generator client { | ||
provider = "prisma-client-js" | ||
previewFeatures = ["multiSchema"] | ||
} | ||
|
||
datasource db { | ||
provider = "postgresql" | ||
url = env("TEST_POSTGRES_URI") | ||
schemas = ["base", "transactional"] | ||
} | ||
|
||
model User { | ||
id String @id @default(cuid()) | ||
email String | ||
posts Post[] | ||
@@map("some_table") // same @@map table name but different schema | ||
@@schema("base") | ||
} | ||
|
||
model Post { | ||
id String @id @default(cuid()) | ||
title String | ||
authorId String | ||
author User? @relation(fields: [authorId], references: [id]) | ||
@@map("some_table") // same @@map table name but different schema | ||
@@schema("transactional") | ||
} |
25 changes: 25 additions & 0 deletions
25
packages/client/src/__tests__/integration/errors/multi-schema/setup.sql
This file contains 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,25 @@ | ||
-- CreateSchema | ||
CREATE SCHEMA IF NOT EXISTS "base"; | ||
|
||
-- CreateSchema | ||
CREATE SCHEMA IF NOT EXISTS "transactional"; | ||
|
||
-- CreateTable | ||
CREATE TABLE "base"."some_table" ( | ||
"id" TEXT NOT NULL, | ||
"email" TEXT NOT NULL, | ||
|
||
CONSTRAINT "User_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "transactional"."some_table" ( | ||
"id" TEXT NOT NULL, | ||
"title" TEXT NOT NULL, | ||
"authorId" TEXT NOT NULL, | ||
|
||
CONSTRAINT "Post_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "transactional"."some_table" ADD CONSTRAINT "Post_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "base"."some_table"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
150 changes: 150 additions & 0 deletions
150
packages/client/src/__tests__/integration/errors/multi-schema/test.ts
This file contains 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,150 @@ | ||
import { generateTestClient } from '../../../../utils/getTestClient' | ||
import type { SetupParams } from '../../../../utils/setupPostgres' | ||
import { setupPostgres, tearDownPostgres } from '../../../../utils/setupPostgres' | ||
|
||
import { faker } from '@faker-js/faker' | ||
|
||
// @ts-ignore trick to get typings at dev time | ||
import type { Prisma, PrismaClient } from './node_modules/.prisma/client' | ||
|
||
let prisma: PrismaClient | ||
let PrismaUtil: typeof Prisma | ||
const baseUri = process.env.TEST_POSTGRES_URI | ||
|
||
const email = faker.internet.email() | ||
const title = faker.name.jobTitle() | ||
const newEmail = faker.internet.email() | ||
const newTitle = faker.name.jobTitle() | ||
|
||
describe('multi-schema', () => { | ||
beforeAll(async () => { | ||
process.env.TEST_POSTGRES_URI += '-multi-schema' | ||
|
||
await tearDownPostgres(process.env.TEST_POSTGRES_URI!) | ||
const SetupParams: SetupParams = { | ||
connectionString: process.env.TEST_POSTGRES_URI!, | ||
dirname: __dirname, | ||
} | ||
await setupPostgres(SetupParams).catch((e) => console.error(e)) | ||
|
||
await generateTestClient() | ||
const { PrismaClient, Prisma } = require('./node_modules/.prisma/client') | ||
prisma = new PrismaClient() | ||
PrismaUtil = Prisma | ||
}) | ||
|
||
afterAll(async () => { | ||
await prisma.post.deleteMany() | ||
await prisma.user.deleteMany() | ||
await prisma.$disconnect() | ||
process.env.TEST_POSTGRES_URI = baseUri | ||
}) | ||
|
||
test('create', async () => { | ||
const created = await prisma.user.create({ | ||
data: { | ||
email, | ||
posts: { | ||
create: [{ title }], | ||
}, | ||
}, | ||
select: { | ||
email: true, | ||
posts: true, | ||
}, | ||
}) | ||
|
||
expect(created).toMatchObject({ | ||
email, | ||
posts: [{ title }], | ||
}) | ||
}) | ||
|
||
test('read', async () => { | ||
const [read] = await prisma.user.findMany({ | ||
where: { | ||
email, | ||
posts: { | ||
some: { | ||
title, | ||
}, | ||
}, | ||
}, | ||
select: { | ||
email: true, | ||
posts: true, | ||
}, | ||
}) | ||
|
||
expect(read).toMatchObject({ | ||
email, | ||
posts: [{ title }], | ||
}) | ||
}) | ||
|
||
test('update', async () => { | ||
await prisma.post.updateMany({ | ||
where: { | ||
title, | ||
}, | ||
data: { title: newTitle }, | ||
}) | ||
|
||
await prisma.user.updateMany({ | ||
where: { | ||
email, | ||
}, | ||
data: { email: newEmail }, | ||
}) | ||
|
||
const [read] = await prisma.user.findMany({ | ||
where: { | ||
email: newEmail, | ||
posts: { | ||
some: { | ||
title: newTitle, | ||
}, | ||
}, | ||
}, | ||
select: { | ||
email: true, | ||
posts: true, | ||
}, | ||
}) | ||
|
||
expect(read).toMatchObject({ | ||
email: newEmail, | ||
posts: [{ title: newTitle }], | ||
}) | ||
}) | ||
|
||
test('delete', async () => { | ||
await prisma.post.deleteMany({ | ||
where: { | ||
title: newTitle, | ||
}, | ||
}) | ||
|
||
await prisma.user.deleteMany({ | ||
where: { | ||
email: newEmail, | ||
}, | ||
}) | ||
|
||
expect( | ||
await prisma.post.findMany({ | ||
where: { | ||
title: newTitle, | ||
}, | ||
}), | ||
).toHaveLength(0) | ||
|
||
expect( | ||
await prisma.user.findMany({ | ||
where: { | ||
email: newEmail, | ||
}, | ||
}), | ||
).toHaveLength(0) | ||
}) | ||
}) |