Skip to content

Conversation

@moreorover
Copy link
Contributor

@moreorover moreorover commented Dec 27, 2025

closes #765

Summary by CodeRabbit

  • Bug Fixes
    • Startup now fails early with clear errors if required environment variables (e.g., DATABASE_URL) are missing, preventing silent fallback and misconfigured connections.
  • Refactor
    • Database configuration templates made consistent across MySQL, PostgreSQL, and SQLite to strictly rely on provided environment values.
  • Chores
    • Added an extra check for auth token when certain SQLite setups are used.

✏️ Tip: You can customize this high-level summary in your review settings.

@vercel
Copy link

vercel bot commented Dec 27, 2025

@moreorover is attempting to deploy a commit to the Better T Stack Team on Vercel.

A member of the Team first needs to authorize it.

@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. bug Something isn't working labels Dec 27, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 27, 2025

Walkthrough

Three Drizzle config templates now enforce presence of required env vars at runtime and remove empty-string fallbacks for dbCredentials.url; sqlite template also enforces DATABASE_AUTH_TOKEN when dbSetup is "turso". No exported API signatures changed.

Changes

Cohort / File(s) Summary
MySQL Drizzle template
apps/cli/templates/db/drizzle/mysql/drizzle.config.ts.hbs
Added runtime guard that throws if DATABASE_URL is missing; replaced `process.env.DATABASE_URL
Postgres Drizzle template
apps/cli/templates/db/drizzle/postgres/drizzle.config.ts.hbs
Added pre-flight check that throws Error("Missing DATABASE_URL") if unset; changed dbCredentials.url to use process.env.DATABASE_URL without fallback.
SQLite Drizzle template
apps/cli/templates/db/drizzle/sqlite/drizzle.config.ts.hbs
Added runtime checks: throw if DATABASE_URL is missing; if dbSetup === "turso", throw if DATABASE_AUTH_TOKEN is missing. Replaced `process.env.DATABASE_URL

Possibly related PRs

  • Add D1 Database #335 — modifies the same Drizzle config templates (including sqlite) and overlaps on database credential handling and export-time checks.

Pre-merge checks

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The PR does not fully meet issue #765 requirements: it adds runtime checks and removes fallbacks, but fails to replace dotenv with the env package or use env.DATABASE_URL as requested. Import and use the env package from '@{project-name}/env/server' instead of dotenv, and replace process.env.DATABASE_URL with env.DATABASE_URL as specified in issue #765.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: drizzle env loading' directly relates to the main change - fixing environment variable loading for drizzle configuration files.
Out of Scope Changes check ✅ Passed All changes focus on environment variable handling in drizzle configuration files and align with the scope of fixing drizzle env loading; no out-of-scope changes detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
apps/cli/templates/db/drizzle/sqlite/drizzle.config.ts.hbs (1)

2-6: Excellent replacement of dotenv with project env module!

The conditional import logic correctly selects between web and server env modules based on the backend type. Since this is a Handlebars template, the conditional will be evaluated during project scaffolding, generating a single import statement in the final file.

Based on learnings, Bun automatically loads .env files, so removing dotenv is the right approach.

Minor: Inconsistent indentation

The import statements have 4-space indentation, which will generate imports with leading whitespace in the final file. The postgres template (lines 3-7) has no indentation. Consider removing the indentation for consistency:

🔎 Optional formatting fix
 \{{#if (eq backend "self")}}
-    import { env } from "@\{{projectName}}/env/web";
+import { env } from "@\{{projectName}}/env/web";
 \{{else}}
-    import { env } from "@\{{projectName}}/env/server";
+import { env } from "@\{{projectName}}/env/server";
 \{{/if}}
apps/cli/templates/db/drizzle/mysql/drizzle.config.ts.hbs (1)

3-7: Correct approach with minor formatting inconsistency.

The conditional import logic properly replaces dotenv usage with the project's env module. However, the import statements have 4-space indentation, which differs from the postgres template and will generate imports with leading whitespace.

🔎 Optional formatting fix for consistency
 \{{#if (eq backend "self")}}
-    import { env } from "@\{{projectName}}/env/web";
+import { env } from "@\{{projectName}}/env/web";
 \{{else}}
-    import { env } from "@\{{projectName}}/env/server";
+import { env } from "@\{{projectName}}/env/server";
 \{{/if}}
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8f16a69 and 2420ebf.

📒 Files selected for processing (3)
  • apps/cli/templates/db/drizzle/mysql/drizzle.config.ts.hbs
  • apps/cli/templates/db/drizzle/postgres/drizzle.config.ts.hbs
  • apps/cli/templates/db/drizzle/sqlite/drizzle.config.ts.hbs
🧰 Additional context used
📓 Path-based instructions (1)
**/*.hbs

📄 CodeRabbit inference engine (.cursor/rules/better-t-stack-repo.mdc)

**/*.hbs: In Handlebars templates, avoid generic if/else blocks. Write explicit conditions, such as: use if (eq orm "prisma") for Prisma, and else if (eq orm "drizzle") for Drizzle
Escape the '{{' in Handlebars templates like '{{'

Files:

  • apps/cli/templates/db/drizzle/sqlite/drizzle.config.ts.hbs
  • apps/cli/templates/db/drizzle/postgres/drizzle.config.ts.hbs
  • apps/cli/templates/db/drizzle/mysql/drizzle.config.ts.hbs
🧠 Learnings (4)
📓 Common learnings
Learnt from: CR
Repo: AmanVarshney01/create-better-t-stack PR: 0
File: .cursor/rules/use-bun-instead-of-node-vite-npm-pnpm.mdc:0-0
Timestamp: 2025-12-03T07:48:26.419Z
Learning: Applies to **/*.{ts,tsx,js,jsx} : Bun automatically loads .env files, so don't use the dotenv package
📚 Learning: 2025-12-03T07:48:26.419Z
Learnt from: CR
Repo: AmanVarshney01/create-better-t-stack PR: 0
File: .cursor/rules/use-bun-instead-of-node-vite-npm-pnpm.mdc:0-0
Timestamp: 2025-12-03T07:48:26.419Z
Learning: Applies to **/*.{ts,tsx,js,jsx} : Use `bun:sqlite` module for SQLite instead of `better-sqlite3`

Applied to files:

  • apps/cli/templates/db/drizzle/sqlite/drizzle.config.ts.hbs
📚 Learning: 2025-12-03T07:48:26.419Z
Learnt from: CR
Repo: AmanVarshney01/create-better-t-stack PR: 0
File: .cursor/rules/use-bun-instead-of-node-vite-npm-pnpm.mdc:0-0
Timestamp: 2025-12-03T07:48:26.419Z
Learning: Applies to **/*.{ts,tsx,js,jsx} : Bun automatically loads .env files, so don't use the dotenv package

Applied to files:

  • apps/cli/templates/db/drizzle/sqlite/drizzle.config.ts.hbs
  • apps/cli/templates/db/drizzle/postgres/drizzle.config.ts.hbs
  • apps/cli/templates/db/drizzle/mysql/drizzle.config.ts.hbs
📚 Learning: 2025-12-03T07:48:26.419Z
Learnt from: CR
Repo: AmanVarshney01/create-better-t-stack PR: 0
File: .cursor/rules/use-bun-instead-of-node-vite-npm-pnpm.mdc:0-0
Timestamp: 2025-12-03T07:48:26.419Z
Learning: Applies to **/*.{ts,tsx,js,jsx} : Use `Bun.sql` for Postgres instead of `pg` or `postgres.js`

Applied to files:

  • apps/cli/templates/db/drizzle/postgres/drizzle.config.ts.hbs
🔇 Additional comments (4)
apps/cli/templates/db/drizzle/postgres/drizzle.config.ts.hbs (2)

3-7: LGTM!

The conditional import logic is clean and correctly implements the backend-based selection between web and server env modules. The formatting is also consistent with no leading whitespace on the import statements.

Based on learnings, this aligns with the project's approach of relying on Bun's automatic .env loading rather than using the dotenv package.


14-14: Verify that DATABASE_URL is validated as required.

The change from process.env.DATABASE_URL || "" to env.DATABASE_URL is an improvement for type safety. However, the old code provided a fallback empty string, while the new code expects the variable to always be defined. Ensure that the env module validates DATABASE_URL as a required non-empty string.

You can use the verification script from the sqlite file review to check the env module's validation schema.

apps/cli/templates/db/drizzle/mysql/drizzle.config.ts.hbs (1)

14-14: Verify DATABASE_URL is validated as required.

The change from process.env.DATABASE_URL || "" to env.DATABASE_URL improves type safety and code clarity. Ensure that the env module validates this variable as a required non-empty string to prevent runtime errors.

Use the verification script from the sqlite file review to check the env validation schema.

apps/cli/templates/db/drizzle/sqlite/drizzle.config.ts.hbs (1)

18-20: The env module properly validates DATABASE_URL and DATABASE_AUTH_TOKEN as required non-empty strings.

The template's use of env.DATABASE_URL and env.DATABASE_AUTH_TOKEN is correct. The generated env module (in packages/env/src/server.ts) validates both variables as z.string().min(1) when using Turso, ensuring they are required and cannot be empty. The type-safe access to env properties is well-supported by this validation, eliminating the need for fallback values.

@AmanVarshney01
Copy link
Owner

You tried running the "bun db:push" with this setup? It didnt work for me.

@moreorover
Copy link
Contributor Author

You tried running the "bun db:push" with this setup? It didnt work for me.

yeah, you right, that is a problem, let me have a look a bit more into this

@moreorover
Copy link
Contributor Author

had a bit of a read on how turbo manages environment variables

https://turborepo.com/docs/crafting-your-repository/using-environment-variables

https://turborepo.com/docs/reference/configuration#env

I will make example shortly and report back

@AmanVarshney01
Copy link
Owner

If you can find a easy solution then its cool but the env used in drizzle config or prisma config are build time env and not required in runtime so it is fine.

@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Dec 27, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
apps/cli/templates/db/drizzle/postgres/drizzle.config.ts.hbs (1)

1-10: PR objective not met: dotenv should be replaced with the project's env package.

According to issue #765, these lines should be replaced with the project's env package. The requested implementation was:

  • Replace import dotenv from "dotenv" with import { env } from "@{project-name}/env/server"
  • Remove the dotenv.config() call and conditional path logic

The current changes only add runtime validation but don't address the core objective of switching to the env package.

🔎 Proposed fix based on issue #765 requirements
-import { defineConfig } from "drizzle-kit";
-import dotenv from "dotenv";
-
-dotenv.config({
-    {{#if (eq backend "self")}}
-    path: "../../apps/web/.env",
-    {{else}}
-    path: "../../apps/server/.env",
-    {{/if}}
-});
+import { defineConfig } from "drizzle-kit";
+import { env } from "@\{{projectName}}/env/server";

Note: Adjust the import path based on your project's package naming convention.

apps/cli/templates/db/drizzle/sqlite/drizzle.config.ts.hbs (1)

1-10: PR objective not met: dotenv should be replaced with the project's env package.

According to issue #765, these lines should be replaced with the project's env package. The requested implementation was:

  • Replace import dotenv from "dotenv" with import { env } from "@{project-name}/env/server"
  • Remove the dotenv.config() call and conditional path logic

The current changes only add runtime validation but don't address the core objective of switching to the env package.

🔎 Proposed fix based on issue #765 requirements
-import { defineConfig } from "drizzle-kit";
-import dotenv from "dotenv";
-
-dotenv.config({
-    {{#if (eq backend "self")}}
-    path: "../../apps/web/.env",
-    {{else}}
-    path: "../../apps/server/.env",
-    {{/if}}
-});
+import { defineConfig } from "drizzle-kit";
+import { env } from "@\{{projectName}}/env/server";

Note: Adjust the import path based on your project's package naming convention.

📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2420ebf and 862fe99.

📒 Files selected for processing (3)
  • apps/cli/templates/db/drizzle/mysql/drizzle.config.ts.hbs
  • apps/cli/templates/db/drizzle/postgres/drizzle.config.ts.hbs
  • apps/cli/templates/db/drizzle/sqlite/drizzle.config.ts.hbs
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/cli/templates/db/drizzle/mysql/drizzle.config.ts.hbs
🧰 Additional context used
📓 Path-based instructions (1)
**/*.hbs

📄 CodeRabbit inference engine (.cursor/rules/better-t-stack-repo.mdc)

**/*.hbs: In Handlebars templates, avoid generic if/else blocks. Write explicit conditions, such as: use if (eq orm "prisma") for Prisma, and else if (eq orm "drizzle") for Drizzle
Escape the '{{' in Handlebars templates like '{{'

Files:

  • apps/cli/templates/db/drizzle/sqlite/drizzle.config.ts.hbs
  • apps/cli/templates/db/drizzle/postgres/drizzle.config.ts.hbs
🔇 Additional comments (1)
apps/cli/templates/db/drizzle/postgres/drizzle.config.ts.hbs (1)

1-23: The template implementation appears correct. The conditional logic using backend variable to select .env files is appropriate for distinguishing backend architecture (self-hosted vs. separate server), not ORM selection. Since this file is in a drizzle-specific directory, the guideline requiring explicit ORM conditions applies only to multi-ORM templates.

However, the core concern—whether bun db:push functions correctly—requires manual runtime testing with an actual database environment, which cannot be verified through code inspection alone. The template includes proper DATABASE_URL validation and follows the correct drizzle-kit command pattern. Verify functionality by running bun db:push in a local environment with a properly configured database.

@moreorover
Copy link
Contributor Author

I can not find a simple solution yet. But I still feel like turbo should be able to handle environment variables in this case as well. In the mean time I suggest these changes. It will clearly say if referenced .env file does not contain required variable so it a bit more developer friendly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: drizzle env loading

2 participants