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

Closed
opened 2026-04-13 09:29:56 -05:00 by GiteaMirror · 0 comments
Owner

Original Pull Request: https://github.com/better-auth/better-auth/pull/5808

State: closed
Merged: No


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.

**Original Pull Request:** https://github.com/better-auth/better-auth/pull/5808 **State:** closed **Merged:** No --- 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. -->
GiteaMirror added the pull-request label 2026-04-13 09:29:56 -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#14486