Skip to main content

sqlmigration.go

sqlmigration.go - Overview

  1. Overview This file defines interfaces and functions for managing SQL migrations using the bun library. It provides a way to register, run, and rollback database schema changes.

  2. Detailed Documentation

SQLMigration Interface

  • Purpose: Defines the interface for SQL migrations. Each migration should implement this interface.

    • Register

      • Purpose: Registers the migration with the given migrations object.
      • Parameters:
        • migrations (*migrate.Migrations): The migrations object to register with.
      • Returns:
        • error: An error, if any.
    • Up

      • Purpose: Runs the migration.
      • Parameters:
        • ctx (context.Context): The context for the operation.
        • db (*bun.DB): The database connection.
      • Returns:
        • error: An error, if any.
    • Down

      • Purpose: Rolls back the migration.
      • Parameters:
        • ctx (context.Context): The context for the operation.
        • db (*bun.DB): The database connection.
      • Returns:
        • error: An error, if any.

ErrNoExecute Variable

  • Purpose: Represents an error indicating that execution is not allowed.

OrgReference Variable

  • Purpose: Represents a string constant used as a reference to an "org".

UserReference Variable

  • Purpose: Represents a string constant used as a reference to a "user".

New Function

  • Purpose: Creates a new migrate.Migrations instance and registers all provided SQL migrations.
    • Parameters:
      • ctx (context.Context): The context for the operation.
      • settings (factory.ProviderSettings): Provider settings.
      • config (Config): Configuration for the migrations.
      • factories (factory.NamedMap[factory.ProviderFactory[SQLMigration, Config]]): A map of factories that create SQL migrations.
    • Returns:
      • *migrate.Migrations: The new migrations instance.
      • error: An error, if any.

MustNew Function

  • Purpose: Creates a new migrate.Migrations instance and registers all provided SQL migrations, panicking on error.
    • Parameters:
      • ctx (context.Context): The context for the operation.
      • settings (factory.ProviderSettings): Provider settings.
      • config (Config): Configuration for the migrations.
      • factories (factory.NamedMap[factory.ProviderFactory[SQLMigration, Config]]): A map of factories that create SQL migrations.
    • Returns:
      • *migrate.Migrations: The new migrations instance.

GetColumnType Function

  • Purpose: Retrieves the data type of a column in a table.
    • Parameters:
      • ctx (context.Context): The context for the operation.
      • bun (bun.IDB): The database connection (interface to support different bun versions).
      • table (string): The name of the table.
      • column (string): The name of the column.
    • Returns:
      • string: The data type of the column.
      • error: An error, if any.
  1. Code Examples None.

  2. Clarity and Accuracy The documentation accurately reflects the code and provides clear explanations of the functions and interfaces.

Include in Getting Started: YES