Skip to content
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

test(client): multiSchema, test same table name - using manual SQL migration #15798

Merged
merged 1 commit into from
Nov 10, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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")
}
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;
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)
})
})