migrations

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: MIT Imports: 16 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 and PostgreSQL

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyMigrations

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

ApplyMigrations applies dbmate migration files automatically.

Migration lock waits use ctx, which App.Start passes through from its caller. Without a deadline, startup can wait indefinitely unless that context is canceled. This applies to SQLite writer locks and PostgreSQL advisory locks.

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"
  • migrationsFS: Filesystem containing migrations (expects dbType subdirectory)

Returns list of applied migration versions.

func DetectDBType

func DetectDBType(url string) (string, error)

DetectDBType detects database type from connection URL.

Returns one of "sqlite", "postgresql".

func EmbeddedMigrationsFS

func EmbeddedMigrationsFS() fs.FS

EmbeddedMigrationsFS returns the embedded schemas rooted at their dialects.

func EnsureSchemaMigrationsTable

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 ExtractVersionFromFilename

func ExtractVersionFromFilename(filename string) string

ExtractVersionFromFilename extracts version from migration filename.

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

func ParseMigrationFile

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

ParseMigrationFile parses dbmate migration file content.

Returns (upSQL, downSQL) extracted from the file.

Types

This section is empty.

Jump to

Keyboard shortcuts

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