@rvoh/dream
    Preparing search index...

    Class DreamMigrationHelpers

    Index

    Constructors

    Methods

    • Unique indexes cannot be populated by the same value even within a transaction, but deferrable unique constraints can.

      The Sortable decorator requires deferrable unique constraints rather than unique indexes.

      Parameters

      • db: Kysely<any>

        The Kysely database object passed into the migration up/down function

      • constraintName: string

        The name of the constraint to create

      • options: { columns: string[]; table: string }

        Configuration options

        • columns: string[]

          The names of the columns to include in the constraint

        • table: string

          The name of the table

      Returns Promise<void>

    • Add a value to an enum.

      Note that this always includes "IF NOT EXISTS", so is safe to re-run multiple times.

      Parameters

      • db: Kysely<any>

        The Kysely database object passed into the migration up/down function

      • __namedParameters: AddValueToEnumOpts

        The options for adding the enum value

        • enumName

          The name of the enum to modify

        • value

          The name of the value to add to the enum

      Returns Promise<void>

    • Create a database extension.

        // Add the case insensitive extension
      await DreamMigrationHelpers.createExtension(db, 'citext')

      // Add the pg trigram extension
      await DreamMigrationHelpers.createExtension(db, 'pg_trgm')

      Parameters

      • db: Kysely<any>

        The Kysely database object passed into the migration up/down function

      • extensionName: string

        The name of the database extension to add

      • options: { ifNotExists?: boolean; publicSchema?: boolean } = {}

        Configuration options

        • OptionalifNotExists?: boolean

          Only add the extension if it doesn't already exist

        • OptionalpublicSchema?: boolean

          Create using the public schema

      Returns Promise<void>

    • Create a gin index

      Parameters

      • db: Kysely<any>

        The Kysely database object passed into the migration up/down function

      • indexName: string

        The name of the constraint to create

      • options: { column: string; table: string }

        Configuration options

        • column: string

          The name of the column to index

        • table: string

          The name of the table

      Returns Promise<void>

    • Drop a constraint

      Note that this always includes "IF NOT EXISTS", so is safe to re-run multiple times.

      Parameters

      • db: Kysely<any>

        The Kysely database object passed into the migration up/down function

      • constraintName: string

        The name of the constraint to create

      • options: { table: string }

        Configuration options

        • table: string

          The name of the table

      Returns Promise<void>

    • Drop a value from an enum and replace it with a different enum already present in the enum type (or optionally remove it from array columns).

      Parameters

      • db: Kysely<any>

        The Kysely database object passed into the migration up/down function

      • __namedParameters: DropValueFromEnumOpts

        The options for dropping the enum value

        • enumName

          The name of the enum to modify

        • value

          The name of the value to drop from the enum

        • replacements

          Details about which table and column to change and which value to replace the dropped value with (or remove it if the column is an array)

      Returns Promise<void>

    • Forces a new transaction boundary in migration execution.

      When called in a migration file, this method ensures that any existing transaction is committed before this migration runs, and a new transaction is started before the migration in this file. This is essential for migrations that depend on previously committed changes.

      Some database operations require that dependent changes be committed before they can be executed. For example, check constraints that reference enum values require those enum values to be committed to the database first.

      // first migration file: Add enum value
      export async function up(db: Kysely<any>): Promise<void> {
      await DreamMigrationHelpers.addEnumValue(db, {
      enumName: 'user_status',
      value: 'premium'
      })
      }

      // second migration file: Add check constraint that depends on the enum value
      export async function up(db: Kysely<any>): Promise<void> {
      DreamMigrationHelpers.newTransaction() // Ensure enum value is committed first

      await db.schema
      .alterTable('users')
      .addCheckConstraint(
      'check_premium_users',
      sql`status = 'premium' OR credits < 100`
      )
      .execute()
      }

      Returns void

    • Rename a table and its associated primary key index and sequence.

      This method renames the table, its primary key index ({tablename}_pkey), and its primary key sequence ({tablename}_id_seq) to keep them in sync.

      The sequence rename is skipped for tables with UUID primary keys (which have no associated sequence). The primary key index is always renamed since PostgreSQL does not automatically rename it when the table is renamed.

      Parameters

      • db: Kysely<any>

        The Kysely database object passed into the migration up/down function

      • from: string

        The current name of the table to rename

      • to: string

        The new name for the table

      Returns Promise<void>