migrations

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 20, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package migrations provides automatic dbmate-compatible migration support.

This package provides functions to automatically apply dbmate migration files at application startup, eliminating the need to manually run `dbmate up`.

The migration system is compatible with dbmate:

  • Uses the same `schema_migrations` table for tracking applied migrations
  • Reads the same `-- migrate:up` / `-- migrate:down` SQL format
  • Supports SQLite, PostgreSQL, and MySQL

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyMigrations added in v0.3.0

func ApplyMigrations(ctx context.Context, db *sql.DB, dbType string, migrationsFS fs.FS) ([]string, error)

ApplyMigrations applies dbmate migration files automatically.

This function:

  1. Finds migration files for the given database type
  2. Checks which migrations have already been applied
  3. Applies pending migrations in order
  4. Records applied migrations in schema_migrations table

Parameters:

  • ctx: Context for cancellation
  • db: Database connection
  • dbType: One of "sqlite", "postgresql", "mysql"
  • migrationsFS: Filesystem containing migrations (expects dbType subdirectory)

Returns list of applied migration versions.

func DetectDBType added in v0.3.0

func DetectDBType(url string) (string, error)

DetectDBType detects database type from connection URL.

Returns one of "sqlite", "postgresql", "mysql".

func EnsureSchemaMigrationsTable added in v0.3.0

func EnsureSchemaMigrationsTable(ctx context.Context, db *sql.DB, dbType string) error

EnsureSchemaMigrationsTable creates the schema_migrations table if it doesn't exist. This table is compatible with dbmate's migration tracking. Safe for concurrent execution by multiple workers.

func ExecuteSQLStatements added in v0.3.0

func ExecuteSQLStatements(ctx context.Context, db *sql.DB, sqlContent, dbType string) error

ExecuteSQLStatements executes SQL statements from migration file.

Handles:

  • Multiple statements separated by semicolons
  • Comment lines
  • "already exists" errors (idempotent)

func ExtractVersionFromFilename added in v0.3.0

func ExtractVersionFromFilename(filename string) string

ExtractVersionFromFilename extracts version from migration filename.

dbmate uses format: YYYYMMDDHHMMSS_description.sql Example: "20251217000000_initial_schema.sql" -> "20251217000000"

func GetAppliedMigrations added in v0.3.0

func GetAppliedMigrations(ctx context.Context, db *sql.DB) (map[string]bool, error)

GetAppliedMigrations returns a set of already applied migration versions.

func ParseMigrationFile added in v0.3.0

func ParseMigrationFile(content string) (upSQL, downSQL string)

ParseMigrationFile parses dbmate migration file content.

Returns (upSQL, downSQL) extracted from the file.

func RecordMigration added in v0.3.0

func RecordMigration(ctx context.Context, db *sql.DB, dbType, version string) (bool, error)

RecordMigration records a migration as applied.

Returns true if recorded successfully, false if already recorded (race condition).

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL