blitz-js / legacy-framework

MIT License
3 stars 2 forks source link

Railway deployment failed after updating to latest version of blitz #12

Open christianto88 opened 2 years ago

christianto88 commented 2 years ago

What is the problem?

Deployment on Railway failed because cannot find environment variable DATABASE_URL. I am deploying using Dockerfile and the error occurs when running blitz build

Found a workaround by adding ARG DATABASE_URL to my Dockerfile

Paste all your error logs here:

app/node_modules/@prisma/client/runtime/index.js:36166
            throw new PrismaClientInitializationError(error2.message, this.config.clientVersion, error2.error_code);
                  ^

PrismaClientInitializationError: error: Environment variable not found: DATABASE_URL.
  -->  schema.prisma:6
   | 
 5 |   provider = "postgres"
 6 |   url      = env("DATABASE_URL")
   | 

Validation Error Count: 1
    at Object.loadEngine (/app/node_modules/@prisma/client/runtime/index.js:36166:19)
    at async Object.instantiateLibrary (/app/node_modules/@prisma/client/runtime/index.js:36095:5) {
  clientVersion: '3.6.0',
  errorCode: 'P1012'
}

Paste all relevant code snippets here:

Dockerfile before upgrading blitz ( worked previously )

# Install dependencies only when needed
FROM node:15 AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile 

# Rebuild the source code only when needed
FROM node:15 AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules
RUN yarn prisma generate
RUN yarn build && yarn install --production --ignore-scripts --prefer-offline
EXPOSE 3000
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry.
# ENV NEXT_TELEMETRY_DISABLED 1
CMD ["yarn", "start:prod"]

Package.json before upgrading blitz ( worked previously )

{
  "name": "myapp",
  "version": "1.0.0",
  "scripts": {
    "dev": "blitz dev",
    "build": "blitz build",
    "start": "blitz start",
    "start:prod": "prisma migrate deploy && blitz start",
    "studio": "blitz prisma studio",
    "lint": "eslint --ignore-path .gitignore --ext .js,.ts,.tsx .",
    "test": "jest",
    "test:watch": "jest --watch",
    "prepare": "husky install"
  },
  "prisma": {
    "schema": "db/schema.prisma"
  },
  "prettier": {
    "semi": false,
    "printWidth": 100
  },
  "lint-staged": {
    "*.{js,ts,tsx}": [
      "eslint --fix"
    ]
  },
  "dependencies": {
    "@headlessui/react": "1.4.0",
    "@heroicons/react": "1.0.3",
    "@prisma/client": "2.29.0",
    "@tailwindcss/forms": "0.3.3",
    "@tailwindcss/line-clamp": "0.2.1",
    "@types/preview-email": "2.0.1",
    "node-mailjet": "3.3.4",
    "passport-auth0": "1.4.0",
    "preview-email": "3.0.4",
    "prisma": "2.29.0",
    "react": "alpha",
    "react-dom": "alpha",
    "react-final-form": "6.5.3",
    "tailwindcss": "2.2.7",
    "typescript": "4.3.5",
    "zod": "3.7.1"
  },
  "devDependencies": {
    "@types/react": "17.0.17",
    "autoprefixer": "10.3.1",
    "eslint": "7.32.0",
    "husky": "7.0.1",
    "lint-staged": "11.1.2",
    "postcss": "8.3.6",
    "prettier": "2.3.2",
    "prettier-plugin-prisma": "2.29.0",
    "pretty-quick": "3.1.1"
  },
  "private": true
}

New package.json

{
  "name": "myapp",
  "version": "1.0.0",
  "scripts": {
    "dev": "blitz dev",
    "build": "APP_ENV=production blitz build",
    "start": "blitz start",
    "start:prod": "prisma migrate deploy && blitz start",
    "studio": "blitz prisma studio",
    "lint": "eslint --ignore-path .gitignore --ext .js,.ts,.tsx .",
    "test": "jest",
    "test:watch": "jest --watch",
    "prepare": "husky install"
  },
  "prisma": {
    "schema": "db/schema.prisma"
  },
  "prettier": {
    "semi": false,
    "printWidth": 100
  },
  "lint-staged": {
    "*.{js,ts,tsx}": [
      "eslint --fix"
    ]
  },
  "dependencies": {
    "@headlessui/react": "1.4.2",
    "@heroicons/react": "1.0.5",
    "@prisma/client": "3.6.0",
    "@tailwindcss/forms": "0.3.4",
    "@tailwindcss/line-clamp": "0.2.2",
    "blitz": "0.44.3",
    "cors": "2.8.5",
    "final-form": "4.20.6",
    "lodash": "4.17.21",
    "moment": "2.29.1",
    "node-mailjet": "3.3.4",
    "react": "18.0.0-beta-149b420f6-20211119",
    "react-dom": "18.0.0-alpha-5ca4b0433-20211020",
    "react-final-form": "6.5.7",
    "tailwindcss": "2.2.19",
    "zod": "3.11.6"
  },
  "devDependencies": {
    "@types/preview-email": "2.0.1",
    "@types/react": "17.0.37",
    "eslint": "7.32.0",
    "husky": "7.0.4",
    "lint-staged": "11.3.0-beta.2",
    "prettier": "2.5.1",
    "prettier-plugin-prisma": "3.6.0",
    "pretty-quick": "3.1.2",
    "preview-email": "3.0.5",
    "prisma": "3.6.0",
    "typescript": "~4.5"
  },
  "private": true
}

What are detailed steps to reproduce this?

  1. Upgrade blitz to new version using guides from https://github.com/blitz-js/blitz/discussions/992
  2. Deploy to railway using railway up

Run blitz -v and paste the output here:

Loaded env from /Users/christianto/Project/myapp/.env.local
Loaded env from /Users/christianto/Project/myapp/.env
macOS Monterey | darwin-x64 | Node: v16.13.0

blitz: 0.44.3 (global)
blitz: 0.44.3 (local)

  Package manager: yarn 
  System:
    OS: macOS 12.0.1
    CPU: (4) x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
    Memory: 215.02 MB / 8.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.0 - ~/.nvm/versions/node/v16.13.0/bin/node
    Yarn: 1.22.5 - /usr/local/bin/yarn
    npm: 8.1.0 - ~/.nvm/versions/node/v16.13.0/bin/npm
    Watchman: Not Found
  npmPackages:
    @prisma/client: 3.6.0 => 3.6.0 
    blitz: 0.44.3 => 0.44.3 
    prisma: 3.6.0 => 3.6.0 
    react: 18.0.0-beta-149b420f6-20211119 => 18.0.0-beta-149b420f6-20211119 
    react-dom: 18.0.0-alpha-5ca4b0433-20211020 => 18.0.0-alpha-5ca4b0433-20211020 
    typescript: ~4.5 => 4.5.2 

Please include below any other applicable logs and screenshots that show your problem:

No response

JuanM04 commented 2 years ago

Try adding these lines:

ARG DATABASE_URL
ENV DATABASE_URL ${DATABASE_URL}
abuuzayr commented 2 years ago

Hi @christianto88 thank you for reporting this

Just to confirm,

  1. does the output of railway vars include your DATABASE_URL?
  2. since you are using railway up do you get the same issue if you deploy via the UI?
christianto88 commented 2 years ago

@abuuzayr Yes it is on railway vars. I tried deploying using trigger on one github branches, it also failed

jcmcneal commented 2 years ago

This error shows up for me whenever I upgrade to Prisma@^3 and run blitz build.If a valid DATABASE_URL is present, then it will build fine, but that's a flawed solution IMO.

It shouldn't have to be present to build, and one could argue is a security risk to expose that variable to the bundler. That should only be a runtime variable/secret that even the Dockerfile shouldn't have to know about. When I run my app in production it gets injected as a K8s secret. For now I'm still on prisma@2.30.3 until this gets resolved.

beerose commented 2 years ago

This error shows up for me whenever I upgrade to Prisma@^3 and run blitz build.If a valid DATABASE_URL is present, then it will build fine, but that's a flawed solution IMO.

It shouldn't have to be present to build, and one could argue is a security risk to expose that variable to the bundler. That should only be a runtime variable/secret that even the Dockerfile shouldn't have to know about. When I run my app in production it gets injected as a K8s secret. For now I'm still on prisma@2.30.3 until this gets resolved.

@jcmcneal Is there an open issue for that in prisma repo?

chrsep commented 2 years ago

A workaround that worked on one of my projects is to provide a dummy DATABASE_URL to blitz. I used DATABASE_URL=postgresql://postgres:postgres@localhost:5432/main?schema=public and the build succeeded in GitHub action without any Postgres instance running on localhost:5432.

It seems to me that blitz might somehow always trigger Prisma to run during build without running any query? This doesn't happen when I'm using Next with Prisma.

jcmcneal commented 2 years ago

That's insightful information @chrsep, thank you! I will try using the dummy variable in my dockerfile and see how it goes.

If Next doesn't have this issue with Prisma, but Blitz does, then you may be right about it triggering Prisma to connect to the db unintentionally.

@beerose I didn't find any open issues in the Prisma repo about this. However, I think it's related to how Blitz works with Prisma@^3.

beerose commented 2 years ago

Do you have static pages that make calls to DB? If yes, that could cause the issue with the missing DB URL during the build.

Otherwise, I haven't yet found anything internally that could cause this issue. We changed the Prisma version in new app templates from 2.x to 3.x but that didn't require any changes in Blitz code. I suppose something changed with prisma generate that it started requiring database URL?

JuanM04 commented 2 years ago

I have an app running on Railway with Blitz v0.44.3 and a Dockerfile very similar to this one. I remember using the trick I mentioned earlier because there has to be a DATABASE_URL on buildtime.

Btw, that may need to be fixed. It also happened to me when trying to apply the same method enhancedPrisma uses to other clients and it's kinda dangerous