migration

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: MIT Imports: 15 Imported by: 0 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 migration renumbers a branch's own schema migrations. The pipeline's guard and bedrock check refuse a migrations directory that is not one sequence the migrate command applies in order: six-digit indexes, one up file each, contiguous, and in a pull-request build read together with the default branch's, so an index the default branch took since the branch was cut is refused. A branch opens two holes in that sequence, an index taken twice and a gap, and this closes both: the files the branch added move, up and down together, to follow the default branch's highest index with no gap, keeping their order. A committed schema migration is never touched once the repository has a release tag: a migration that ran somewhere is never rewritten. Before the first release nothing has run the migrations but disposable environments, so the whole schema sequence is packed from 000001 instead, committed files included. The seed directory's files are editable, so a committed seed file may be removed: the committed seed files after it move down to close the gap, and the branch's own follow them. It also folds the whole schema sequence into one file at the latest index (consolidate.go).

Index

Constants

View Source
const ConsolidatedName = "Consolidated"

ConsolidatedName is the fixed name of the folded file, after its index: the migrator reads the index alone, the pipeline reads the directory's shape, and the name tells the reader of the listing what the file is.

Variables

View Source
var NameRE = regexp.MustCompile(`^(\d{6})_([A-Za-z0-9_-]+)\.(up|down)\.sql$`)

NameRE is a migration file name: a six-digit index, an underscore, a name, and .up.sql or .down.sql. The index is the first group, the name the second, the kind the third.

View Source
var ReleaseTagRE = regexp.MustCompile(`^v\d+\.\d+\.\d+($|-)`)

ReleaseTagRE is a release tag as release-please cuts them: v<major>.<minor>.<patch>, a prerelease suffix allowed.

Functions

func BlobSHA

func BlobSHA(content []byte) string

BlobSHA is the name git gives a file with the content (git hash-object): what the GitHub API lists for a file at a commit, so a file in the tree is compared with the one committed without reading the committed content.

func IsReleaseTag

func IsReleaseTag(name string) bool

IsReleaseTag says whether the tag name is a release's.

func Released

func Released(ctx context.Context, root string) (bool, error)

Released says whether the repository holds a release tag: the freeze on committed schema migrations starts at the first release, and before it the whole sequence may be packed. The tags are the ones the checkout has, so fetch first.

func Sequence

func Sequence(entries []Entry) ([]Problem, Span)

Sequence reads the entries by the rule and answers every problem, in index order: a .sql file that is not a migration, an index with two up files or two down files or a down file without an up, and each gap. Files that are not .sql are not the rule's.

Types

type ConsolidateOptions

type ConsolidateOptions struct {
	// Root is the repository root; Dir the schema migrations directory, root-relative.
	Root string
	Dir  string
	// Branch is the default branch, whose committed sequence is folded; a branch cut from
	// a patch line follows the line instead (followedBranch).
	Branch string
}

ConsolidateOptions names the repository, the schema migrations directory and the branch.

type Consolidation

type Consolidation struct {
	// Ref is the ref the committed sequence was read from, at Commit (short).
	Ref    string
	Commit string
	Dir    string
	// Index is the latest index, which the folded file carries; Up and Down name the
	// files written (Down is empty when no folded migration had a down file), and Folded
	// the stems folded into them, in order. Nothing was done when Folded is empty.
	Index  int
	Up     string
	Down   string
	Folded []string
	// WithoutDown names the folded stems that had no down file: the down file reverses
	// the others.
	WithoutDown []string
}

Consolidation is what the fold read and did.

func Consolidate

func Consolidate(ctx context.Context, opts ConsolidateOptions) (*Consolidation, error)

Consolidate folds the directory's sequence, as the followed branch holds it, into one up file and one down file at the latest index, removes the folded files and stages the change. It refuses a directory that is not as the followed branch holds it (a change under it in the working tree, or a migration of the branch's own): a consolidation is a pull request of its own, cut from the branch it folds. A sequence of one file is left alone.

type Entry

type Entry struct {
	Name string
	Note string
}

Entry is one file of a migrations directory as the rule reads it: its name, and a note saying where it was found when that is not the tree being checked ("on master, not in this pull request").

type Options

type Options struct {
	// Root is the repository root; Dirs are the migration directories, root-relative:
	// the schema migrations and the seed directory beside them.
	Root string
	Dirs []string
	// Branch is the default branch, whose committed sequence the branch's own files
	// follow; a working branch cut from a patch line follows the line instead, which
	// Renumber finds (followedBranch). The followed branch's tree is read from origin's
	// copy of it when the repository has one, else from the local branch.
	Branch string
	// Editable names the directories of Dirs whose committed files may move: the seed
	// directory, whose files are editable, where the files after a removed one move
	// down to close its gap. A committed file elsewhere is never touched.
	Editable []string
}

Options names the repository, the directories and the branch.

type Problem

type Problem struct {
	File string
	Note string
	Text string
}

Problem is one way the entries break the rule: the file it is about (its name and note; empty for the directory as a whole) and what is wrong, the way a person reads it.

type Rename

type Rename struct {
	Dir   string
	From  string
	To    string
	Files []string
}

Rename is one migration moved: its stem (NNNNNN_name) before and after, in Dir, and which of its files (up, down) moved with it.

type Result

type Result struct {
	// Ref is the ref the committed sequence was read from, at Commit (short).
	Ref     string
	Commit  string
	Renames []Rename
	// Notes name the files left alone, and why.
	Notes []string
	// Unreleased says the repository has no release tag, so the schema directories were
	// packed from 000001, committed migrations included.
	Unreleased bool
}

Result is what the renumber read and did.

func Renumber

func Renumber(ctx context.Context, opts Options) (*Result, error)

Renumber moves the branch's own migrations in every directory to follow the default branch's, and in an editable directory the files after a removed one down. A directory that does not exist has nothing to renumber.

type Span

type Span struct {
	Count, Low, High int
}

Span is what a sound sequence holds: how many migrations (up files), and the lowest and highest index. Count is 0 for none.

Jump to

Keyboard shortcuts

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