Skip to content

Commit b4be736

Browse files
committed
prismaExtension fixes for #1325 and #1327
1 parent 4e0bc48 commit b4be736

File tree

15 files changed

+208
-15
lines changed

15 files changed

+208
-15
lines changed

.changeset/friendly-brooms-cry.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"trigger.dev": patch
3+
"@trigger.dev/build": patch
4+
---
5+
6+
prismaExtension fixes for #1325 and #1327

.vscode/launch.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@
3636
"cwd": "${workspaceFolder}/references/v3-catalog",
3737
"sourceMaps": true
3838
},
39+
{
40+
"type": "node-terminal",
41+
"request": "launch",
42+
"name": "Debug prisma-catalog deploy CLI",
43+
"command": "pnpm exec trigger deploy --self-hosted --load-image",
44+
"cwd": "${workspaceFolder}/references/prisma-catalog",
45+
"sourceMaps": true
46+
},
3947
{
4048
"type": "node-terminal",
4149
"request": "launch",

docs/config/config-file.mdx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -444,12 +444,9 @@ export default defineConfig({
444444
```
445445

446446
<Note>
447-
The `prismaExtension` will inject the `DATABASE_URL` environment variable into the build process
448-
when running the `deploy` command. This means the CLI needs to have `process.env.DATABASE_URL` set
449-
at the time of calling the `deploy` command. You can do this via a `.env` file and passing the
450-
`--env-file .env` option to the deploy command or via shell environment variables. This goes for direct database URLs as well.
447+
The `prismaExtension` will inject the `DATABASE_URL` environment variable into the build process. Learn more about setting environment variables for deploying in our [Environment Variables](/deploy-environment-variables) guide.
451448

452-
These environment variables are only used during the build process and are not embedded in the final image.
449+
These environment variables are only used during the build process and are not embedded in the final container image.
453450

454451
</Note>
455452

packages/build/src/extensions/prisma.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,9 @@ export class PrismaExtension implements BuildExtension {
127127
if (this.options.typedSql) {
128128
generatorFlags.push(`--sql`);
129129

130-
const schemaDir = dirname(this._resolvedSchemaPath);
131-
const prismaDir = dirname(schemaDir);
130+
const prismaDir = usingSchemaFolder
131+
? dirname(dirname(this._resolvedSchemaPath))
132+
: dirname(this._resolvedSchemaPath);
132133

133134
context.logger.debug(`Using typedSql`);
134135

@@ -226,15 +227,29 @@ export class PrismaExtension implements BuildExtension {
226227
commands.push(
227228
`${binaryForRuntime(manifest.runtime)} node_modules/prisma/build/index.js migrate deploy`
228229
);
230+
}
229231

230-
env.DATABASE_URL = manifest.deploy.env?.DATABASE_URL;
232+
env.DATABASE_URL = manifest.deploy.env?.DATABASE_URL;
231233

232-
if (this.options.directUrlEnvVarName) {
233-
env[this.options.directUrlEnvVarName] =
234-
manifest.deploy.env?.[this.options.directUrlEnvVarName];
235-
} else {
236-
env.DIRECT_URL = manifest.deploy.env?.DIRECT_URL;
234+
if (this.options.directUrlEnvVarName) {
235+
env[this.options.directUrlEnvVarName] =
236+
manifest.deploy.env?.[this.options.directUrlEnvVarName] ??
237+
process.env[this.options.directUrlEnvVarName];
238+
239+
if (!env[this.options.directUrlEnvVarName]) {
240+
context.logger.warn(
241+
`prismaExtension could not resolve the ${this.options.directUrlEnvVarName} environment variable. Make sure you add it to your environment variables or provide it as an environment variable to the deploy CLI command. See our docs for more info: https://trigger.dev/docs/deploy-environment-variables`
242+
);
237243
}
244+
} else {
245+
env.DIRECT_URL = manifest.deploy.env?.DIRECT_URL;
246+
env.DIRECT_DATABASE_URL = manifest.deploy.env?.DIRECT_DATABASE_URL;
247+
}
248+
249+
if (!env.DATABASE_URL) {
250+
context.logger.warn(
251+
`prismaExtension could not resolve the DATABASE_URL environment variable. Make sure you add it to your environment variables. See our docs for more info: https://trigger.dev/docs/deploy-environment-variables`
252+
);
238253
}
239254

240255
context.logger.debug(`Adding the prisma layer with the following commands`, {

packages/cli-v3/src/deploy/buildImage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ COPY --chown=bun:bun . .
492492
493493
${postInstallCommands}
494494
495-
from build as indexer
495+
FROM build AS indexer
496496
497497
USER bun
498498
WORKDIR /app
@@ -601,7 +601,7 @@ COPY --chown=node:node . .
601601
602602
${postInstallCommands}
603603
604-
from build as indexer
604+
FROM build AS indexer
605605
606606
USER node
607607
WORKDIR /app

pnpm-lock.yaml

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "references-prisma-catalog",
3+
"private": true,
4+
"type": "module",
5+
"devDependencies": {
6+
"trigger.dev": "workspace:*",
7+
"@trigger.dev/build": "workspace:*",
8+
"typescript": "^5.5.4",
9+
"prisma": "5.19.0"
10+
},
11+
"dependencies": {
12+
"@trigger.dev/sdk": "workspace:*",
13+
"@prisma/client": "5.19.0"
14+
},
15+
"scripts": {
16+
"generate:prisma": "prisma generate --sql"
17+
}
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- CreateTable
2+
CREATE TABLE "User" (
3+
"id" SERIAL NOT NULL,
4+
"name" TEXT NOT NULL,
5+
6+
CONSTRAINT "User_pkey" PRIMARY KEY ("id")
7+
);
8+
9+
-- CreateTable
10+
CREATE TABLE "Post" (
11+
"id" SERIAL NOT NULL,
12+
"title" TEXT NOT NULL,
13+
"content" TEXT NOT NULL,
14+
"authorId" INTEGER NOT NULL,
15+
16+
CONSTRAINT "Post_pkey" PRIMARY KEY ("id")
17+
);
18+
19+
-- AddForeignKey
20+
ALTER TABLE "Post" ADD CONSTRAINT "Post_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Please do not edit this file manually
2+
# It should be added in your version-control system (i.e. Git)
3+
provider = "postgresql"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
generator client {
2+
provider = "prisma-client-js"
3+
previewFeatures = ["typedSql"]
4+
}
5+
6+
datasource db {
7+
provider = "postgresql"
8+
url = env("DATABASE_URL")
9+
directUrl = env("DIRECT_DATABASE_URL")
10+
}
11+
12+
// user.prisma
13+
model User {
14+
id Int @id @default(autoincrement())
15+
name String
16+
posts Post[]
17+
}
18+
19+
// post.prisma
20+
model Post {
21+
id Int @id @default(autoincrement())
22+
title String
23+
content String
24+
authorId Int
25+
author User @relation(fields: [authorId], references: [id])
26+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
SELECT
2+
u.id,
3+
u.name,
4+
COUNT(p.id) as "postCount"
5+
FROM
6+
"User" u
7+
LEFT JOIN "Post" p ON u.id = p."authorId"
8+
GROUP BY
9+
u.id,
10+
u.name;

references/prisma-catalog/src/db.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { PrismaClient } from "@prisma/client";
2+
import { getUsersWithPosts } from "@prisma/client/sql";
3+
4+
export const prisma = new PrismaClient();
5+
6+
export { getUsersWithPosts };
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { getUsersWithPosts, prisma } from "../db.js";
2+
import { logger, task } from "@trigger.dev/sdk/v3";
3+
4+
export const prismaTask = task({
5+
id: "prisma-task",
6+
run: async () => {
7+
const users = await prisma.user.findMany();
8+
9+
await prisma.user.create({
10+
data: {
11+
name: "Alice",
12+
},
13+
});
14+
15+
const usersWithPosts = await prisma.$queryRawTyped(getUsersWithPosts());
16+
17+
logger.info("Users with posts", { usersWithPosts });
18+
19+
return users;
20+
},
21+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { prismaExtension } from "@trigger.dev/build/extensions/prisma";
2+
import { defineConfig } from "@trigger.dev/sdk/v3";
3+
4+
export default defineConfig({
5+
runtime: "node",
6+
project: "proj_mpzmrzygzbvmfjnnpcsk",
7+
retries: {
8+
enabledInDev: false,
9+
default: {
10+
maxAttempts: 3,
11+
minTimeoutInMs: 5_000,
12+
maxTimeoutInMs: 30_000,
13+
factor: 2,
14+
randomize: true,
15+
},
16+
},
17+
build: {
18+
extensions: [
19+
prismaExtension({
20+
schema: "prisma/schema.prisma",
21+
directUrlEnvVarName: "DIRECT_DATABASE_URL",
22+
typedSql: true,
23+
}),
24+
],
25+
},
26+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2023",
4+
"module": "Node16",
5+
"moduleResolution": "Node16",
6+
"esModuleInterop": true,
7+
"strict": true,
8+
"skipLibCheck": true,
9+
"customConditions": ["@triggerdotdev/source"],
10+
"jsx": "preserve",
11+
"lib": ["DOM", "DOM.Iterable"],
12+
"noEmit": true
13+
},
14+
"include": ["./src/**/*.ts", "trigger.config.ts"]
15+
}

0 commit comments

Comments
 (0)