calcom / cal.com

Scheduling infrastructure for absolutely everyone.
https://cal.com
Other
31.81k stars 7.78k forks source link

Issue with DATABASE_DIRECT_URL not recognized in Prisma schema validation #16875

Open ManavMalhotra opened 1 week ago

ManavMalhotra commented 1 week ago

Title: Issue with DATABASE_DIRECT_URL not recognized in Prisma schema validation

Description:
I encountered a bug while running database migrations for the Cal.com project using Prisma. Even though the .env file is properly set at the root of the project, Prisma is throwing an error indicating that the DATABASE_DIRECT_URL environment variable is missing.

Error Log:

Error: Prisma schema validation - (get-config wasm)  
Error code: P1012  
error: Environment variable not found: DATABASE_DIRECT_URL.  
  -->  schema.prisma:7  
   |  
 6 |   url       = env("DATABASE_URL")  
 7 |   directUrl = env("DATABASE_DIRECT_URL")  
   |  
Validation Error Count: 1

Steps to Reproduce:

  1. Clone the Cal.com repository.
  2. Set up the .env file at the root level with the required database URLs (DATABASE_URL and DATABASE_DIRECT_URL).
  3. Run the following command:
    yarn workspace @calcom/prisma db-migrate
  4. The error will occur during the schema validation step.

Workaround:
I had to add the DATABASE_DIRECT_URL directly into the Prisma folder's .env file (relative to the Prisma directory) for the migration to work. This suggests that Prisma may not be correctly resolving the path to the root .env file.

Expected Behavior:
Prisma should properly read the DATABASE_DIRECT_URL from the root .env file, as it does with DATABASE_URL.

Environment:

jeevan4476 commented 1 day ago

Title: Solution for DATABASE_DIRECT_URL Not Recognized in Prisma Schema Validation

@ManavMalhotra , I encountered this issue while trying to run migrations for the Cal.com project using Prisma. After investigating, I found that the issue was related to Prisma not resolving the DATABASE_DIRECT_URL because the database wasn't running locally and it couldn't find or connect to the database.

Root Cause:

The problem occurred because Prisma expected a locally hosted PostgreSQL database. My environment was misconfigured, which caused Prisma not to pick up the DATABASE_DIRECT_URL properly.

Solution Steps:

  1. Ensure Local PostgreSQL is Running:

    • Set up a local PostgreSQL instance.
    • Ensure it's properly configured in the .env file, particularly the DATABASE_URL and DATABASE_DIRECT_URL variables.
  2. Check Connection Settings:

    • Verify that both DATABASE_URL and DATABASE_DIRECT_URL are pointing to the correct local PostgreSQL instance.
  3. Run Migration Command:

    • Once PostgreSQL is running locally and the .env file is configured correctly, I was able to run the migrations successfully using the following command:
      yarn workspace @calcom/prisma db-migrate

This resolved the issue for me.