Skip to content

feat(drizzle): generate drizzle schema with schema namespace#7169

Open
ping-maxwell wants to merge 11 commits intobetter-auth:mainfrom
ping-maxwell:feat/drizzle-gen-schema-namespace
Open

feat(drizzle): generate drizzle schema with schema namespace#7169
ping-maxwell wants to merge 11 commits intobetter-auth:mainfrom
ping-maxwell:feat/drizzle-gen-schema-namespace

Conversation

@ping-maxwell
Copy link
Copy Markdown
Member

@ping-maxwell ping-maxwell commented Jan 7, 2026

closes #6606

Docs

Custom Schema namespace

If you're using PostgreSQL and you want to generate the schema with a custom schema namespace,
you can pass the schemaName option to the Drizzle adapter.

export const auth = betterAuth({
  database: drizzleAdapter(db, {
    provider: "pg",
    schemaName: "auth", // [!code highlight]
  }),
});

Then when using the Better Auth CLI, it will generate the schema that looks something like this:

npx @better-auth/cli@latest generate
export const authSchema = pgSchema("auth");

export const user = authSchema.table("user", {...});
export const session = authSchema.table("session", {...});

Summary by cubic

Add schemaName support to the Drizzle adapter so the CLI can generate PostgreSQL schemas under a custom namespace. MySQL and SQLite remain unchanged.

  • New Features

    • Added schemaName to DrizzleAdapterConfig.
    • CLI generator imports pgSchema only when schemaName is set, declares const Schema = pgSchema(""), and uses Schema.table(...) for PostgreSQL.
    • Converts schemaName to a safe identifier (handles hyphens, non-alphanumerics, leading numbers).
    • Updated docs and added tests/snapshots for pg with/without schemaName; sqlite/mysql ignore schemaName.
  • Migration

    • PostgreSQL: set schemaName in drizzleAdapter config and re-run the Better Auth CLI.
    • No action needed for MySQL or SQLite.

Written for commit 47c7b2e. Summary will update on new commits.

@vercel
Copy link
Copy Markdown

vercel bot commented Jan 7, 2026

@ping-maxwell is attempting to deploy a commit to the better-auth Team on Vercel.

A member of the Team first needs to authorize it.

@ping-maxwell ping-maxwell changed the title feat(drizzle): generate drizzle schema with schema namespace feat(drizzle): generate drizzle schema with schema namespace Jan 7, 2026
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Jan 7, 2026

Open in StackBlitz

better-auth

npm i https://pkg.pr.new/better-auth/better-auth@7169

@better-auth/cli

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/cli@7169

@better-auth/core

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/core@7169

@better-auth/expo

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/expo@7169

@better-auth/mcp

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/mcp@7169

@better-auth/oauth-provider

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/oauth-provider@7169

@better-auth/passkey

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/passkey@7169

@better-auth/scim

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/scim@7169

@better-auth/sso

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/sso@7169

@better-auth/stripe

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/stripe@7169

@better-auth/telemetry

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/telemetry@7169

commit: 47c7b2e

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 5 files

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="packages/cli/src/generators/drizzle.ts">

<violation number="1" location="packages/cli/src/generators/drizzle.ts:26">
P1: The leading number handling is ineffective. The underscore added by `.replace(/^[0-9]/, "_$&")` will be removed by the subsequent `.replace(/_/g, "")`, resulting in invalid JavaScript identifiers for schema names starting with numbers (e.g., `"123schema"` → `"123schema"` instead of `"_123schema"`).

Reorder operations: remove underscores first (for camelCase), then handle leading numbers.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@SaizFerri
Copy link
Copy Markdown

SaizFerri commented Jan 19, 2026

Would it be possible to pass an existing pgSchema to the config instead of just a string?
This current change seems too rigid as it implements 2 things on top of the schemaName specification:

  • concatenate auth+Schema
  • create the schema on the db schema file and export it

I think most use cases are from apps that already use a pgSchema across the app and want to reuse it for the auth. For this cases this implementation would not be usable.

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Feb 26, 2026

CLA assistant check
All committers have signed the CLA.

@gustavovalverde gustavovalverde added devtools CLI, OpenAPI, telemetry, i18n, test-utils database Database layer, adapters, schema, migrations docs Documentation, demos enhancement New feature or improvement labels Mar 26, 2026
@gustavovalverde gustavovalverde removed the enhancement New feature or improvement label Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

database Database layer, adapters, schema, migrations devtools CLI, OpenAPI, telemetry, i18n, test-utils docs Documentation, demos

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLI generate command: Support for PostgreSQL custom schema (pgSchema) in Drizzle output

6 participants