prismaExtension fixes for #1325 and #1327

This commit is contained in:
Eric Allam
2024-09-19 15:21:03 +01:00
parent 4e0bc485a1
commit b4be736555
15 changed files with 208 additions and 15 deletions
+6
View File
@@ -0,0 +1,6 @@
---
"trigger.dev": patch
"@trigger.dev/build": patch
---
prismaExtension fixes for #1325 and #1327
+8
View File
@@ -36,6 +36,14 @@
"cwd": "${workspaceFolder}/references/v3-catalog",
"sourceMaps": true
},
{
"type": "node-terminal",
"request": "launch",
"name": "Debug prisma-catalog deploy CLI",
"command": "pnpm exec trigger deploy --self-hosted --load-image",
"cwd": "${workspaceFolder}/references/prisma-catalog",
"sourceMaps": true
},
{
"type": "node-terminal",
"request": "launch",
+2 -5
View File
@@ -444,12 +444,9 @@ export default defineConfig({
```
<Note>
The `prismaExtension` will inject the `DATABASE_URL` environment variable into the build process
when running the `deploy` command. This means the CLI needs to have `process.env.DATABASE_URL` set
at the time of calling the `deploy` command. You can do this via a `.env` file and passing the
`--env-file .env` option to the deploy command or via shell environment variables. This goes for direct database URLs as well.
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.
These environment variables are only used during the build process and are not embedded in the final image.
These environment variables are only used during the build process and are not embedded in the final container image.
</Note>
+23 -8
View File
@@ -127,8 +127,9 @@ export class PrismaExtension implements BuildExtension {
if (this.options.typedSql) {
generatorFlags.push(`--sql`);
const schemaDir = dirname(this._resolvedSchemaPath);
const prismaDir = dirname(schemaDir);
const prismaDir = usingSchemaFolder
? dirname(dirname(this._resolvedSchemaPath))
: dirname(this._resolvedSchemaPath);
context.logger.debug(`Using typedSql`);
@@ -226,15 +227,29 @@ export class PrismaExtension implements BuildExtension {
commands.push(
`${binaryForRuntime(manifest.runtime)} node_modules/prisma/build/index.js migrate deploy`
);
}
env.DATABASE_URL = manifest.deploy.env?.DATABASE_URL;
env.DATABASE_URL = manifest.deploy.env?.DATABASE_URL;
if (this.options.directUrlEnvVarName) {
env[this.options.directUrlEnvVarName] =
manifest.deploy.env?.[this.options.directUrlEnvVarName];
} else {
env.DIRECT_URL = manifest.deploy.env?.DIRECT_URL;
if (this.options.directUrlEnvVarName) {
env[this.options.directUrlEnvVarName] =
manifest.deploy.env?.[this.options.directUrlEnvVarName] ??
process.env[this.options.directUrlEnvVarName];
if (!env[this.options.directUrlEnvVarName]) {
context.logger.warn(
`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`
);
}
} else {
env.DIRECT_URL = manifest.deploy.env?.DIRECT_URL;
env.DIRECT_DATABASE_URL = manifest.deploy.env?.DIRECT_DATABASE_URL;
}
if (!env.DATABASE_URL) {
context.logger.warn(
`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`
);
}
context.logger.debug(`Adding the prisma layer with the following commands`, {
+2 -2
View File
@@ -492,7 +492,7 @@ COPY --chown=bun:bun . .
${postInstallCommands}
from build as indexer
FROM build AS indexer
USER bun
WORKDIR /app
@@ -601,7 +601,7 @@ COPY --chown=node:node . .
${postInstallCommands}
from build as indexer
FROM build AS indexer
USER node
WORKDIR /app
+22
View File
@@ -1360,6 +1360,28 @@ importers:
specifier: workspace:*
version: link:../../packages/cli-v3
references/prisma-catalog:
dependencies:
'@prisma/client':
specifier: 5.19.0
version: 5.19.0(prisma@5.19.0)
'@trigger.dev/sdk':
specifier: workspace:*
version: link:../../packages/trigger-sdk
devDependencies:
'@trigger.dev/build':
specifier: workspace:*
version: link:../../packages/build
prisma:
specifier: 5.19.0
version: 5.19.0
trigger.dev:
specifier: workspace:*
version: link:../../packages/cli-v3
typescript:
specifier: ^5.5.4
version: 5.5.4
references/v3-catalog:
dependencies:
'@infisical/sdk':
+18
View File
@@ -0,0 +1,18 @@
{
"name": "references-prisma-catalog",
"private": true,
"type": "module",
"devDependencies": {
"trigger.dev": "workspace:*",
"@trigger.dev/build": "workspace:*",
"typescript": "^5.5.4",
"prisma": "5.19.0"
},
"dependencies": {
"@trigger.dev/sdk": "workspace:*",
"@prisma/client": "5.19.0"
},
"scripts": {
"generate:prisma": "prisma generate --sql"
}
}
@@ -0,0 +1,20 @@
-- CreateTable
CREATE TABLE "User" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
CONSTRAINT "User_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "Post" (
"id" SERIAL NOT NULL,
"title" TEXT NOT NULL,
"content" TEXT NOT NULL,
"authorId" INTEGER NOT NULL,
CONSTRAINT "Post_pkey" PRIMARY KEY ("id")
);
-- AddForeignKey
ALTER TABLE "Post" ADD CONSTRAINT "Post_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "postgresql"
@@ -0,0 +1,26 @@
generator client {
provider = "prisma-client-js"
previewFeatures = ["typedSql"]
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
directUrl = env("DIRECT_DATABASE_URL")
}
// user.prisma
model User {
id Int @id @default(autoincrement())
name String
posts Post[]
}
// post.prisma
model Post {
id Int @id @default(autoincrement())
title String
content String
authorId Int
author User @relation(fields: [authorId], references: [id])
}
@@ -0,0 +1,10 @@
SELECT
u.id,
u.name,
COUNT(p.id) as "postCount"
FROM
"User" u
LEFT JOIN "Post" p ON u.id = p."authorId"
GROUP BY
u.id,
u.name;
+6
View File
@@ -0,0 +1,6 @@
import { PrismaClient } from "@prisma/client";
import { getUsersWithPosts } from "@prisma/client/sql";
export const prisma = new PrismaClient();
export { getUsersWithPosts };
@@ -0,0 +1,21 @@
import { getUsersWithPosts, prisma } from "../db.js";
import { logger, task } from "@trigger.dev/sdk/v3";
export const prismaTask = task({
id: "prisma-task",
run: async () => {
const users = await prisma.user.findMany();
await prisma.user.create({
data: {
name: "Alice",
},
});
const usersWithPosts = await prisma.$queryRawTyped(getUsersWithPosts());
logger.info("Users with posts", { usersWithPosts });
return users;
},
});
@@ -0,0 +1,26 @@
import { prismaExtension } from "@trigger.dev/build/extensions/prisma";
import { defineConfig } from "@trigger.dev/sdk/v3";
export default defineConfig({
runtime: "node",
project: "proj_mpzmrzygzbvmfjnnpcsk",
retries: {
enabledInDev: false,
default: {
maxAttempts: 3,
minTimeoutInMs: 5_000,
maxTimeoutInMs: 30_000,
factor: 2,
randomize: true,
},
},
build: {
extensions: [
prismaExtension({
schema: "prisma/schema.prisma",
directUrlEnvVarName: "DIRECT_DATABASE_URL",
typedSql: true,
}),
],
},
});
+15
View File
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "ES2023",
"module": "Node16",
"moduleResolution": "Node16",
"esModuleInterop": true,
"strict": true,
"skipLibCheck": true,
"customConditions": ["@triggerdotdev/source"],
"jsx": "preserve",
"lib": ["DOM", "DOM.Iterable"],
"noEmit": true
},
"include": ["./src/**/*.ts", "trigger.config.ts"]
}