Skip to main content

migrator.go

migrator.go - Overview

  1. Overview

This file defines the migrator struct and its associated methods for performing database migrations using the uptrace/bun library. It handles migration initialization, execution, rollback, and locking to prevent concurrent migrations.

  1. Detailed Documentation

Constants

migrationTableName

  • Purpose: Specifies the name of the database table used to store migration status.
  • Type: string
  • Value: "migration"

migrationLockTableName

  • Purpose: Specifies the name of the database table used for migration locking.
  • Type: string
  • Value: "migration_lock"

type migrator

type migrator struct {
settings factory.ScopedProviderSettings
config Config
migrator *migrate.Migrator
dialect string
}
  • Purpose: Represents the database migrator.
  • Fields:
    • settings: Scoped provider settings for logging and configuration.
    • config: Configuration settings for the migrator, including lock timeout and interval.
    • migrator: An instance of migrate.Migrator from the uptrace/bun library.
    • dialect: The SQL dialect being used (e.g., "postgres", "mysql").

func New

func New(ctx context.Context, providerSettings factory.ProviderSettings, sqlstore sqlstore.SQLStore, migrations *migrate.Migrations, config Config) SQLMigrator
  • Purpose: Creates a new migrator instance.
  • Parameters:
    • ctx: The context for the operation.
    • providerSettings: Provider settings for the migrator.
    • sqlstore: An SQLStore instance providing access to the database.
    • migrations: A migrate.Migrations instance containing the migration definitions.
    • config: Configuration settings for the migrator.
  • Returns: An SQLMigrator interface.

func (migrator *migrator) Migrate

func (migrator *migrator) Migrate(ctx context.Context) error
  • Purpose: Executes pending database migrations.
  • Parameters:
    • ctx: The context for the operation.
  • Returns: An error if migration fails, or nil if successful.

func (migrator *migrator) Rollback

func (migrator *migrator) Rollback(ctx context.Context) error
  • Purpose: Rolls back the last applied group of migrations.
  • Parameters:
    • ctx: The context for the operation.
  • Returns: An error if rollback fails, or nil if successful.

func (migrator *migrator) Lock

func (migrator *migrator) Lock(ctx context.Context) error
  • Purpose: Attempts to acquire a migration lock, preventing concurrent migrations. It retries until the lock is acquired or the timeout is reached.
  • Parameters:
    • ctx: The context for the operation.
  • Returns: An error if the lock cannot be acquired within the configured timeout, or nil if the lock is successfully acquired.
  1. Code Examples

None

  1. Clarity and Accuracy

The documentation is based on the provided code and aims to be precise.

  1. Markdown & MDX Perfection

The markdown syntax has been checked.

  1. Edge Cases To Avoid Breaking MDX

All special characters are properly escaped, and the format is verified.

  1. Getting Started Relevance

Include in Getting Started: NO