pgdbtemplategolangmigrate

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2025 License: MIT Imports: 12 Imported by: 0

README

pgdbtemplate-golang-migrate

Go Reference CI Coverage License

A golang-migrate adapter for pgdbtemplate.

Features

  • 🔥 Flexible Driver Support - Works with both database/sql (lib/pq) and pgx/v5 drivers
  • 🚀 Fast Test Databases - Leverage pgdbtemplate's template database speed
  • 📦 Most Popular - golang-migrate has 17.5k+ stars and is used by 23.6k+ projects
  • 🔧 Configurable - Full access to golang-migrate's configuration options
  • 🧪 Well-tested - Comprehensive test coverage with real PostgreSQL integration
  • 📁 Standard Migrations - Use your existing golang-migrate migration files

Requirements

  • Go 1.20+ - Compatible with older Go versions for legacy projects
  • PostgreSQL 9.5+
  • Driver: Either pgdbtemplate-pq or pgdbtemplate-pgx

Installation

go get github.com/andrei-polukhin/pgdbtemplate-golang-migrate

Usage

With pgdbtemplate-pq (database/sql + lib/pq)
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/andrei-polukhin/pgdbtemplate"
	pgdbtemplatepq "github.com/andrei-polukhin/pgdbtemplate-pq"
	pgdbtemplategolangmigrate "github.com/andrei-polukhin/pgdbtemplate-golang-migrate"
)

func main() {
	ctx := context.Background()

	// Create connection provider (database/sql).
	connStringFunc := func(dbName string) string {
		return fmt.Sprintf("postgres://user:pass@localhost/%s?sslmode=disable", dbName)
	}
	provider := pgdbtemplatepq.NewConnectionProvider(connStringFunc)

	// Create golang-migrate runner.
	migrationRunner := pgdbtemplategolangmigrate.NewMigrationRunner("./migrations")

	// Create template manager.
	config := pgdbtemplate.Config{
		ConnectionProvider: provider,
		MigrationRunner:    migrationRunner,
	}

	tm, err := pgdbtemplate.NewTemplateManager(config)
	if err != nil {
		log.Fatal(err)
	}

	// Initialize template with migrations.
	if err := tm.Initialize(ctx); err != nil {
		log.Fatal(err)
	}
	defer tm.Cleanup(ctx)

	// Create test databases (fast!).
	testDB, dbName, err := tm.CreateTestDatabase(ctx)
	if err != nil {
		log.Fatal(err)
	}
	defer testDB.Close()
	defer tm.DropTestDatabase(ctx, dbName)

	log.Printf("Test database %s ready with golang-migrate migrations!", dbName)
}
With pgdbtemplate-pgx (pgx/v5 native)
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/andrei-polukhin/pgdbtemplate"
	pgdbtemplatepgx "github.com/andrei-polukhin/pgdbtemplate-pgx"
	pgdbtemplategolangmigrate "github.com/andrei-polukhin/pgdbtemplate-golang-migrate"
)

func main() {
	ctx := context.Background()

	// Create connection provider (pgx/v5 with pooling).
	connStringFunc := func(dbName string) string {
		return fmt.Sprintf("postgres://user:pass@localhost/%s", dbName)
	}
	provider := pgdbtemplatepgx.NewConnectionProvider(connStringFunc)
	defer provider.Close()

	// Create golang-migrate runner (automatically detects driver).
	migrationRunner := pgdbtemplategolangmigrate.NewMigrationRunner("./migrations")

	// Create template manager.
	config := pgdbtemplate.Config{
		ConnectionProvider: provider,
		MigrationRunner:    migrationRunner,
	}

	tm, err := pgdbtemplate.NewTemplateManager(config)
	if err != nil {
		log.Fatal(err)
	}

	// Initialize template with migrations.
	if err := tm.Initialize(ctx); err != nil {
		log.Fatal(err)
	}
	defer tm.Cleanup(ctx)

	// Create test databases (fast!).
	testDB, dbName, err := tm.CreateTestDatabase(ctx)
	if err != nil {
		log.Fatal(err)
	}
	defer testDB.Close()
	defer tm.DropTestDatabase(ctx, dbName)

	log.Printf("Test database %s ready!", dbName)
}

Advanced Configuration

runner := pgdbtemplategolangmigrate.NewMigrationRunner(
	"./migrations",
	pgdbtemplategolangmigrate.WithMigrationsTable("custom_migrations"),
	pgdbtemplategolangmigrate.WithSchemaName("myschema"),
	pgdbtemplategolangmigrate.WithStatementTimeout(60*time.Second),
	pgdbtemplategolangmigrate.WithMultiStatementEnabled(true),
	pgdbtemplategolangmigrate.WithMultiStatementMaxSize(10*1024*1024), // 10MB
)

Migration Files

golang-migrate uses standard migration file format:

migrations/
  000001_create_users_table.up.sql
  000001_create_users_table.down.sql
  000002_add_email_column.up.sql
  000002_add_email_column.down.sql

Example migration file (000001_create_users_table.up.sql):

CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    name TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT NOW()
);

Why golang-migrate?

  • Most Popular - 17.5k+ stars, used by 23.6k+ projects
  • Production Ready - Battle-tested in large-scale applications
  • Flexible - Supports 20+ databases
  • Two Drivers - Works with both database/sql and pgx/v5
  • Rich Features - Versioning, rollbacks, CLI tools

Comparison with Other Adapters

Feature golang-migrate goose dbmate
Stars 17.5k ⭐ 9.2k 6.4k
Projects Using 23.6k 2k 618
pgx/v5 Support ✅ Yes ✅ Yes ❌ No
database/sql Support ✅ Yes ✅ Yes ✅ Yes
Up/Down Migrations ✅ Yes ✅ Yes ✅ Yes
Go 1.20 Compatible ✅ Yes ❌ No (1.21+) ✅ Yes

License

MIT license.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	MigrationsTable       string
	SchemaName            string
	StatementTimeout      time.Duration
	MultiStatementEnabled bool
	MultiStatementMaxSize int
}

Config holds configuration for the migration runner.

type MigrationRunner

type MigrationRunner struct {
	// contains filtered or unexported fields
}

MigrationRunner implements pgdbtemplate.MigrationRunner using golang-migrate.

func NewMigrationRunner

func NewMigrationRunner(migrationsFs fs.FS, options ...Option) *MigrationRunner

NewMigrationRunner creates a new golang-migrate based migration runner.

The migrationsFs parameter accepts any fs.FS implementation containing migration files. This provides flexibility to use os.DirFS(), embed.FS, or any custom fs.FS implementation.

Example with filesystem directory:

migrationsFs := os.DirFS("./migrations")
runner := pgdbtemplategolangmigrate.NewMigrationRunner(migrationsFs)

Example with embedded filesystem:

//go:embed migrations/*.sql
var migrationsFS embed.FS
runner := pgdbtemplategolangmigrate.NewMigrationRunner(migrationsFS)

func (*MigrationRunner) RunMigrations

RunMigrations implements pgdbtemplate.MigrationRunner.RunMigrations.

It runs all pending golang-migrate migrations on the provided database connection. Supports both pgdbtemplate-pq (database/sql) and pgdbtemplate-pgx (pgx/v5).

type Option

type Option func(*MigrationRunner)

Option configures the migration runner.

func WithMigrationsTable

func WithMigrationsTable(tableName string) Option

WithMigrationsTable sets the migrations table name.

func WithMultiStatementEnabled

func WithMultiStatementEnabled(enabled bool) Option

WithMultiStatementEnabled enables multi-statement migrations.

func WithMultiStatementMaxSize

func WithMultiStatementMaxSize(maxSize int) Option

WithMultiStatementMaxSize sets the max size for multi-statement migrations.

func WithSchemaName

func WithSchemaName(schemaName string) Option

WithSchemaName sets the schema name.

func WithStatementTimeout

func WithStatementTimeout(timeout time.Duration) Option

WithStatementTimeout sets the statement timeout.

Jump to

Keyboard shortcuts

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