gostgrator

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: May 3, 2025 License: MIT Imports: 12 Imported by: 0

README

gostgrator

Actions Status SocketDev PkgGoDev

gostgrator: A low dependency, go stdlib port of postgrator supporting postgres and sqlite.

Migrations

Migrations can live in any folder in your project. The default is ./migrations. Migration files are named 001.do.some-optional-description.sql and 001.undo.some-optional-description.sql and come in up and down pairs. The files should contain SQL appropriate for the database you are running them

./migrations
├── 001.do.sql
├── 001.undo.sql
├── 002.do.some-description.sql
├── 002.undo.some-description.sql
├── 003.do.sql
├── 003.undo.sql
├── 004.do.sql
├── 004.undo.sql
├── 005.do.sql
├── 005.undo.sql
├── 006.do.sql
└── 006.undo.sql
Migration Transactions

gostgrator (like postgrator), applies no special or magic transaction around your migrations, other than running multiple statements from a file in one execution which postgres will treat as a transaction. If you need stricter behavior than this, or are migrating databases that don't have this behavior, wrap your migrations in explicite BEGIN/END blocks.

gostgrator CLI

gostgrator is intended to be installed and versioned as a go tool.

Each supported database has it's own CLI you can install.

gostgrator/pg
go get -tool github.com/bcomnes/gostgrator/pg
go tool github.com/bcomnes/gostgrator/pg -help
Usage:
  gostgrator-pg [command] [arguments] [options]

Commands:
  migrate [target]    Migrate the schema to a target version (default: "max").
  down [steps]        Roll back the specified number of migrations (default: 1).
  new <desc>          Create a new empty migration pair with the provided description.
  drop-schema         Drop the schema version table.
  list                List available migrations and annotate the migration matching the database version.

Options:
  -config string
    	Path to JSON configuration file (optional)
  -conn string
    	PostgreSQL connection URL. Can be set with DATABASE_URL env var.
  -help
    	Show help message
  -migration-pattern string
    	Glob pattern for migration files when running up or down migrations (default "migrations/*.sql")
  -mode string
    	Migration numbering mode ("int" or "timestamp") when creating new migrations (default "int")
  -schema-table string
    	Name of the schema table migration state is stored in (default "schemaversion")
  -version
    	Show version
gostgrator/sqlite
go get -tool github.com/bcomnes/gostgrator/sqlite
go tool github.com/bcomnes/gostgrator/sqlite -help

Quick tour

# migrate to latest in ./migrations using DATABASE_URL
go tool github.com/bcomnes/gostgrator/pg migrate

# rollback the last two migrations
go tool github.com/bcomnes/gostgrator/pg down 2

# create a timestamp‑based pair
go tool github.com/bcomnes/gostgrator/pg -mode timestamp new "add-users-table"

# list all migrations and mark current
gostgrator-pg list

Library usage

Full API docs live on PkgGoDev.


Why another migrator?

  • CLI ‑ first – instant productivity; no boilerplate code required.
  • Typed Go API – embed migrations programmatically when you need to.
  • Checksum validation – MD5 guardrails ensure applied migrations never drift.
  • Up ⬆ / Down ⬇ parity – every migration pair keeps rollbacks honest.
  • Zero dependencies – a single static binary per database driver.

License

MIT © Bret Comnes 2025

Documentation

Overview

Package gostgrator provides database migration capabilities.

Package gostgrator provides database migration capabilities.

Package gostgrator provides database migration capabilities.

Package gostgrator provides database-agnostic schema migration utilities for Go applications. It loads *.sql* migration files, tracks execution state in a table you choose, and moves the database forward or backward to any version you specify.

A thin driver layer (currently PostgreSQL and SQLite) supplies SQL dialect differences. Companion CLI tools live under sub-packages *pg* and *sqlite*; the core logic is here.

Install

go get github.com/bcomnes/gostgrator@latest

Quick start

import (
    "context"
    "database/sql"

    _ "github.com/jackc/pgx/v5/stdlib" // or sqlite3
    "github.com/bcomnes/gostgrator"
)

func main() {
    db, _ := sql.Open("pgx", os.Getenv("DATABASE_URL"))
    cfg := gostgrator.Config{
        Driver:           "pg",
        MigrationPattern: "migrations/*.sql",
    }

    g, _ := gostgrator.NewGostgrator(cfg, db)
    g.Migrate(context.Background(), "max")
}

Configuration

Use Config to tweak behaviour:

  • Driver — database driver name ("pg", "sqlite3")
  • SchemaTable — table that stores migration state (default "schemaversion")
  • MigrationPattern — glob for locating migration files
  • Newline — line-ending style when scaffolding new migrations
  • ValidateChecksums — compare MD5 hashes before running *up* migrations

You can merge Config with your own JSON/YAML file or set it inline.

Migration files

A migration *pair* is two files with the same version and name:

001.do.create_users.sql   // apply
001.undo.create_users.sql // roll back

Versions may be plain integers (*001*, *002*, …) or timestamps if you prefer. The CLI’s *new* command scaffolds these files for you.

Programmatic API

NewGostgrator(cfg, db)        → *Gostgrator
(*Gostgrator).Migrate(ctx, v) → []Migration, error
(*Gostgrator).Down(ctx, n)    → []Migration, error
(*Gostgrator).GetMigrations() → []Migration, error
(*Gostgrator).GetDatabaseVersion(ctx) → int, error

All operations are context-aware; cancel the context to abort long runs.

CLI helpers

If you prefer shell commands, install driver-specific binaries:

go get -tool github.com/bcomnes/gostgrator/pg@latest      # PostgreSQL
go get -tool github.com/bcomnes/gostgrator/sqlite@latest  # SQLite

See each sub-package’s doc for flags and usage.

Exit codes

The library returns errors; the CLIs exit with non-zero status on any failure. ValidateErrors include version and MD5 info for easy triage.

Versioning

A semantic version string is exposed as:

var Version = "vX.Y.Z"

Embed it in your own commands to surface gostgrator’s build version.

Generated documentation; update whenever public API or CLI flags change.

Package gostgrator provides database migration capabilities.

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfig = Config{
	SchemaTable:       "schemaversion",
	ValidateChecksums: true,
}

DefaultConfig provides default values for configuration.

View Source
var (
	Version = "1.0.4"
)

Functions

func CreateMigration

func CreateMigration(cfg Config, description string, mode string) error

CreateMigration creates a new pair of migration files (do/undo). description: a human-readable description that will be kebab-cased for the filename. mode: "int" for integer increment (default) or "timestamp" to use the Unix timestamp.

Types

type Client

type Client interface {
	QueryContext(ctx context.Context, query string) (*sql.Rows, error)
	ExecContext(ctx context.Context, script string) (sql.Result, error)
	GetDatabaseVersionSql() string
	HasVersionTable(ctx context.Context) (bool, error)
	EnsureTable(ctx context.Context) error
	GetMd5Sql(m Migration) string
	PersistActionSql(m Migration) string
}

Client defines the interface for migration clients.

func NewClient

func NewClient(cfg Config, db *sql.DB) (Client, error)

NewClient creates a new Client based on the provided configuration and database connection.

func NewPostgresClient

func NewPostgresClient(cfg Config, db *sql.DB) Client

NewPostgresClient creates a new PostgresClient.

func NewSqlite3Client

func NewSqlite3Client(cfg Config, db *sql.DB) Client

NewSqlite3Client creates a new Sqlite3Client.

type Config

type Config struct {
	// Driver is the database driver, e.g., "pg" or "sqlite3".
	Driver string
	// SchemaTable is the name of the migration table.
	SchemaTable string

	// MigrationPattern is the glob pattern for migration files (e.g. "./migrations/*.sql").
	MigrationPattern string

	// Newline is the desired newline style ("LF", "CR", or "CRLF").
	Newline string

	// ValidateChecksums indicates if the tool should validate migration checksums.
	ValidateChecksums bool
}

Config holds settings for migrations.

type Gostgrator

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

Gostgrator is the main orchestrator for running database migrations.

It loads migration files, determines the current database version, validates checksums (if enabled), and runs the necessary migrations to reach a target version.

func NewGostgrator

func NewGostgrator(cfg Config, db *sql.DB) (*Gostgrator, error)

NewGostgrator creates a new Gostgrator instance with the provided configuration and database connection.

func (*Gostgrator) CreateMigration

func (g *Gostgrator) CreateMigration(description, mode string) error

(Optional) If you prefer to expose this functionality as a method on Gostgrator, you can add the following method.

func (*Gostgrator) Down

func (g *Gostgrator) Down(ctx context.Context, steps int) ([]Migration, error)

Down rolls back the migrations by the given number of steps. It computes the target version as the current version minus steps (not going below zero), and then calls Migrate to perform the undo operations.

func (*Gostgrator) GetDatabaseVersion

func (g *Gostgrator) GetDatabaseVersion(ctx context.Context) (int, error)

GetDatabaseVersion returns the current database version. If the migration table is not initialized, it returns 0.

func (*Gostgrator) GetMaxVersion

func (g *Gostgrator) GetMaxVersion() (int, error)

GetMaxVersion returns the highest migration version available.

func (*Gostgrator) GetMigrations

func (g *Gostgrator) GetMigrations() ([]Migration, error)

func (*Gostgrator) GetRunnableMigrations

func (g *Gostgrator) GetRunnableMigrations(databaseVersion, targetVersion int) ([]Migration, error)

func (*Gostgrator) Migrate

func (g *Gostgrator) Migrate(ctx context.Context, target string) ([]Migration, error)

Migrate moves the schema to the target version. If target is "max" or empty, it migrates to the highest available version.

func (*Gostgrator) QueryContext

func (g *Gostgrator) QueryContext(ctx context.Context, query string) (*sql.Rows, error)

QueryContext is a helper to execute a query using the underlying client.

func (*Gostgrator) RunMigrations

func (g *Gostgrator) RunMigrations(ctx context.Context, migrations []Migration) ([]Migration, error)

RunMigrations applies the provided migrations in sequence.

func (*Gostgrator) ValidateMigrations

func (g *Gostgrator) ValidateMigrations(ctx context.Context, databaseVersion int) error

ValidateMigrations verifies that applied migrations have not changed by comparing MD5 checksums.

type Migration

type Migration struct {
	// Version of the migration.
	Version int

	// Action, e.g., "do" or "undo".
	Action string

	// Filename is the path to the migration file.
	Filename string

	// Name is an optional descriptive name of the migration.
	Name string

	// Md5 is the MD5 checksum of the migration file.
	Md5 string
}

Migration represents a single migration file.

type PostgresClient

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

PostgresClient implements the Client interface for PostgreSQL.

func (*PostgresClient) EnsureTable

func (c *PostgresClient) EnsureTable(ctx context.Context) error

EnsureTable creates the migration table if it does not exist and adds missing columns.

func (*PostgresClient) ExecContext

func (c *PostgresClient) ExecContext(ctx context.Context, script string) (sql.Result, error)

Exposing ExecContext from the configured db connection.

func (*PostgresClient) GetDatabaseVersionSql

func (c *PostgresClient) GetDatabaseVersionSql() string

GetDatabaseVersionSql returns SQL to fetch the highest applied migration version.

func (*PostgresClient) GetMd5Sql

func (c *PostgresClient) GetMd5Sql(m Migration) string

GetMd5Sql returns SQL to fetch the MD5 checksum for a migration version.

func (*PostgresClient) HasVersionTable

func (c *PostgresClient) HasVersionTable(ctx context.Context) (bool, error)

HasVersionTable checks for the existence of the migration table.

func (*PostgresClient) PersistActionSql

func (c *PostgresClient) PersistActionSql(m Migration) string

PersistActionSql generates SQL to record a migration action.

func (*PostgresClient) QueryContext

func (c *PostgresClient) QueryContext(ctx context.Context, query string) (*sql.Rows, error)

Exposes the QueryContext method from the configured db connection.

type Sqlite3Client

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

Sqlite3Client implements the Client interface for SQLite.

func (*Sqlite3Client) EnsureTable

func (c *Sqlite3Client) EnsureTable(ctx context.Context) error

EnsureTable creates the migration table if it does not exist and adds missing columns.

func (*Sqlite3Client) ExecContext

func (c *Sqlite3Client) ExecContext(ctx context.Context, script string) (sql.Result, error)

Exposing ExecContext from the configured db connection.

func (*Sqlite3Client) GetDatabaseVersionSql

func (c *Sqlite3Client) GetDatabaseVersionSql() string

GetDatabaseVersionSql returns SQL to fetch the highest applied migration version.

func (*Sqlite3Client) GetMd5Sql

func (c *Sqlite3Client) GetMd5Sql(m Migration) string

GetMd5Sql returns SQL to fetch the MD5 checksum for a migration version.

func (*Sqlite3Client) HasVersionTable

func (c *Sqlite3Client) HasVersionTable(ctx context.Context) (bool, error)

HasVersionTable checks for the existence of the migration table.

func (*Sqlite3Client) PersistActionSql

func (c *Sqlite3Client) PersistActionSql(m Migration) string

PersistActionSql generates SQL to record a migration action.

func (*Sqlite3Client) QueryContext

func (c *Sqlite3Client) QueryContext(ctx context.Context, query string) (*sql.Rows, error)

Exposes the QueryContext method from the configured db connection.

Directories

Path Synopsis
Package main provides gostgrator-pg, a PostgreSQL-specific command-line interface for the gostgrator migration library.
Package main provides gostgrator-pg, a PostgreSQL-specific command-line interface for the gostgrator migration library.
Package main provides gostgrator-sqlite, a SQLite-specific command-line interface for the gostgrator migration library.
Package main provides gostgrator-sqlite, a SQLite-specific command-line interface for the gostgrator migration library.

Jump to

Keyboard shortcuts

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