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 ¶
- func ApplyMigrations(ctx context.Context, db *sql.DB, dbType string, migrationsFS fs.FS) ([]string, error)
- func DetectDBType(url string) (string, error)
- func EnsureSchemaMigrationsTable(ctx context.Context, db *sql.DB, dbType string) error
- func ExecuteSQLStatements(ctx context.Context, db *sql.DB, sqlContent, dbType string) error
- func ExtractVersionFromFilename(filename string) string
- func GetAppliedMigrations(ctx context.Context, db *sql.DB) (map[string]bool, error)
- func ParseMigrationFile(content string) (upSQL, downSQL string)
- func RecordMigration(ctx context.Context, db *sql.DB, dbType, version string) (bool, error)
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:
- Finds migration files for the given database type
- Checks which migrations have already been applied
- Applies pending migrations in order
- 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
DetectDBType detects database type from connection URL.
Returns one of "sqlite", "postgresql", "mysql".
func EnsureSchemaMigrationsTable ¶ added in v0.3.0
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
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
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
GetAppliedMigrations returns a set of already applied migration versions.
func ParseMigrationFile ¶ added in v0.3.0
ParseMigrationFile parses dbmate migration file content.
Returns (upSQL, downSQL) extracted from the file.
Types ¶
This section is empty.