shmigrate

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2026 License: MPL-2.0-no-copyleft-exception Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package shmigrate implements sqlmigrate.Runner by generating POSIX shell commands that reference migration files on disk. It is used by the sql-migrate CLI to produce scripts that can be piped to sh.

Index

Constants

View Source
const ShHeader = `#!/bin/sh
set -e
set -u

if test -s ./.env; then
    . ./.env
fi
`

ShHeader is the standard header for generated shell scripts.

Variables

This section is empty.

Functions

This section is empty.

Types

type Migrator

type Migrator struct {
	// Writer receives the generated shell script.
	Writer io.Writer

	// SqlCommand is the shell command template with %s for the file path.
	// Example: `psql "$PG_URL" -v ON_ERROR_STOP=on -A -t --file %s`
	SqlCommand string

	// MigrationsDir is the path to the migrations directory on disk.
	MigrationsDir string

	// LogQueryPath is the path to the _migrations.sql query file.
	// Used to sync the migrations log after each migration.
	LogQueryPath string

	// LogPath is the path to the migrations.log file.
	LogPath string

	// FS is an optional filesystem for reading the migrations log.
	// When nil, the OS filesystem is used.
	FS fs.FS
	// contains filtered or unexported fields
}

Migrator generates shell scripts for migration execution. It implements sqlmigrate.Migrator so it can be used with sqlmigrate.Up, sqlmigrate.Down, and sqlmigrate.GetStatus.

func (*Migrator) Applied

func (r *Migrator) Applied(ctx context.Context) ([]sqlmigrate.Migration, error)

Applied reads the migrations log file and returns applied migrations. Supports two formats:

  • New: "id\tname" (tab-separated, written by updated _migrations.sql)
  • Old: "name" (name only, for backwards compatibility)

Returns an empty slice if the file does not exist. When FS is set, reads from that filesystem; otherwise reads from the OS filesystem.

func (*Migrator) ExecDown

func (r *Migrator) ExecDown(ctx context.Context, m sqlmigrate.Migration, sql string) error

ExecDown outputs a shell command to run the .down.sql migration file. The sql parameter is ignored — shmigrate references files on disk.

func (*Migrator) ExecUp

func (r *Migrator) ExecUp(ctx context.Context, m sqlmigrate.Migration, sql string) error

ExecUp outputs a shell command to run the .up.sql migration file. The sql parameter is ignored — shmigrate references files on disk.

func (*Migrator) Reset

func (r *Migrator) Reset()

Reset resets the migration counter. Call between Up and Down if generating both in the same script.

Jump to

Keyboard shortcuts

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