herd

package
v0.2.48 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package herd provides support for running migrations against a PostgreSQL database.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB interface {
	ExecContext(context.Context, string, ...any) (sql.Result, error)
	QueryContext(context.Context, string, ...any) (*sql.Rows, error)
	QueryRowContext(context.Context, string, ...any) *sql.Row
}

DB contains the methods from sql.DB that migrations can use.

type FileMigration

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

FileMigration uses the contents of a SQL file as a migration.

func NewFileMigration

func NewFileMigration(version int64, contents string) *FileMigration

NewFileMigration creates a new FileMigration.

func NewFileMigrationFromFS

func NewFileMigrationFromFS(filesystem fs.FS, path string) (*FileMigration, error)

NewFileMigrationFromFS reads the file at the path from the filesystem. The last element of the path is taken as the filename, and is passed with the contents to NewFileMigrationFromFilename.

func NewFileMigrationFromFilename

func NewFileMigrationFromFilename(filename, contents string) (*FileMigration, error)

NewFileMigrationFromFilename extracts the migration version from the given filename and calls NewFileMigration.

The version must be at the start of the filename followed by a underscore ("_"). For example, 1_initial.sql would have a version of 1. Versions can be padded with 0s to make them easier to view in a file browser. 0001_initial.sql and 1_initial.sql would have the same version.

func (*FileMigration) Migrate

func (m *FileMigration) Migrate(ctx context.Context, db DB) error

Migrate executes the SQL file contents against the database.

func (*FileMigration) Version

func (m *FileMigration) Version() int64

Version returns the migration version.

type Migration

type Migration interface {
	// Version returns the migration version. It must be greater than 0 and unique across all
	// migrations.
	Version() int64

	// Migrate applies the schema or data changes for the migration.
	Migrate(context.Context, DB) error
}

Migration implementations apply schema or data changes to a database.

func CollectFileMigrations

func CollectFileMigrations(filesystem fs.FS) ([]Migration, error)

CollectFileMigrations walks the filesystem, searching for files with a ".sql" extension. For each found file, NewFileMigrationFromFS is called to create the migration.

Jump to

Keyboard shortcuts

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