[PR #5808] [CLOSED] feat: allow passing overrides to getMigration #23140

Closed
opened 2026-04-15 21:30:45 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/5808
Author: @ping-maxwell
Created: 11/6/2025
Status: Closed

Base: canaryHead: feat/get-migration-overrides


📝 Commits (4)

  • 810a011 feat: Allow passing overrides to getMigration
  • 87f0e7f chore: lint
  • 841aa86 fix: id change support
  • eb43727 Merge branch 'canary' into feat/get-migration-overrides

📊 Changes

2 files changed (+995 additions, -53 deletions)

View changed files

📝 packages/better-auth/src/db/get-migration-schema.test.ts (+819 -2)
📝 packages/better-auth/src/db/get-migration.ts (+176 -51)

📄 Description

Example

import { getMigrations } from "better-auth/db";
import type { BetterAuthOptions } from "@better-auth/core";
import type { FieldTypeOverride } from "better-auth/db/get-migration";

const config: BetterAuthOptions = {
  database: yourPostgresDatabase, // Your PostgreSQL database connection
  emailAndPassword: {
    enabled: true,
  },
  // ... other config
};

const fieldTypeOverride: FieldTypeOverride = (
  field,
  fieldName,
  tableName,
  dbType,
  config,
  defaultType,
) => {
  // Override all id fields to UUID for PostgreSQL
  if (dbType === "postgres" && fieldName === "id") {
    return { type: "uuid" };
  }

  // Optionally, also override foreign keys that reference id fields
  if (dbType === "postgres" && field.references?.field === "id") {
    return { type: "uuid" };
  }

  return undefined; // Use defaults for everything else
};

const { runMigrations, compileMigrations } = await getMigrations(
  config,
  fieldTypeOverride,
);

// To see the SQL that will be generated:
const sql = await compileMigrations();
console.log(sql);

// To actually run the migrations:
await runMigrations();

Summary by cubic

Adds an override callback to getMigrations so you can customize column SQL types and attributes per field/table/database without changing defaults. This makes migrations more flexible across Postgres, MySQL, SQLite, and MSSQL.

  • New Features

    • getMigrations now accepts an optional fieldTypeOverride callback.
    • Callback can override type (string or RawBuilder), required, and unique.
    • Receives field, fieldName, tableName, dbType, config, and defaultType for context.
    • Overrides apply to both new tables and ALTER TABLE column additions.
    • Primary key id columns now respect overrides when creating tables.
    • Extensive tests cover ids, foreign keys, booleans, json, arrays, and constraints.
  • Migration

    • No action needed; behavior is unchanged if no override is provided.
    • If using overrides, import FieldTypeOverride from better-auth/db/get-migration.

Written for commit eb43727cce. Summary will update automatically on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/5808 **Author:** [@ping-maxwell](https://github.com/ping-maxwell) **Created:** 11/6/2025 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `feat/get-migration-overrides` --- ### 📝 Commits (4) - [`810a011`](https://github.com/better-auth/better-auth/commit/810a011a88a19a13e109a4edb01e165df6f2bd7f) feat: Allow passing overrides to getMigration - [`87f0e7f`](https://github.com/better-auth/better-auth/commit/87f0e7f6eaf1cb9ec750031ec24a186492e5f6eb) chore: lint - [`841aa86`](https://github.com/better-auth/better-auth/commit/841aa867b7bdde234798e8ba77096f37f79e6473) fix: id change support - [`eb43727`](https://github.com/better-auth/better-auth/commit/eb43727cce00fc11c924bae579d7b32cc3bce9cf) Merge branch 'canary' into feat/get-migration-overrides ### 📊 Changes **2 files changed** (+995 additions, -53 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/db/get-migration-schema.test.ts` (+819 -2) 📝 `packages/better-auth/src/db/get-migration.ts` (+176 -51) </details> ### 📄 Description Example ```ts import { getMigrations } from "better-auth/db"; import type { BetterAuthOptions } from "@better-auth/core"; import type { FieldTypeOverride } from "better-auth/db/get-migration"; const config: BetterAuthOptions = { database: yourPostgresDatabase, // Your PostgreSQL database connection emailAndPassword: { enabled: true, }, // ... other config }; const fieldTypeOverride: FieldTypeOverride = ( field, fieldName, tableName, dbType, config, defaultType, ) => { // Override all id fields to UUID for PostgreSQL if (dbType === "postgres" && fieldName === "id") { return { type: "uuid" }; } // Optionally, also override foreign keys that reference id fields if (dbType === "postgres" && field.references?.field === "id") { return { type: "uuid" }; } return undefined; // Use defaults for everything else }; const { runMigrations, compileMigrations } = await getMigrations( config, fieldTypeOverride, ); // To see the SQL that will be generated: const sql = await compileMigrations(); console.log(sql); // To actually run the migrations: await runMigrations(); ``` <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Adds an override callback to getMigrations so you can customize column SQL types and attributes per field/table/database without changing defaults. This makes migrations more flexible across Postgres, MySQL, SQLite, and MSSQL. - **New Features** - getMigrations now accepts an optional fieldTypeOverride callback. - Callback can override type (string or RawBuilder), required, and unique. - Receives field, fieldName, tableName, dbType, config, and defaultType for context. - Overrides apply to both new tables and ALTER TABLE column additions. - Primary key id columns now respect overrides when creating tables. - Extensive tests cover ids, foreign keys, booleans, json, arrays, and constraints. - **Migration** - No action needed; behavior is unchanged if no override is provided. - If using overrides, import FieldTypeOverride from better-auth/db/get-migration. <sup>Written for commit eb43727cce00fc11c924bae579d7b32cc3bce9cf. Summary will update automatically on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-15 21:30:45 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#23140