migration

package module
v0.0.0-...-7248949 Latest Latest
Warning

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

Go to latest
Published: May 4, 2022 License: MIT Imports: 7 Imported by: 0

README

migration

migration is a project to create a Go framework for handling database migrations using actual Go code. It is still in early development, but the general idea is to pattern migration definition after go test unit test definitions. A user should be able to write a package containing functions that look like the example below, run go generate, and get a package that they can import elsewhere in their module that will perform the desired migrations on an *sql.DB instance.

Example of Intended Functionality

This is very early design and hasn't been thought through completely yet. The idea is to pattern after ActiveRecord's migrations, but with some changes. In particular, a dependency system is planned that will hopefully make it easier to structure migrations in a project being worked on by multiple people simultaneously.

func MigrateInit(m *migration.M) {
	m.CreateTable("example", func(t *migration.T) {
		t.AddColumn("id", migration.BigInt).PrimaryKey()
		t.AddColumn("name", migration.String).NotNull(),
		t.AddColumn("val", migration.String)
	})
}

func MigrateMakeValNotNull(m *migration.M) {
	m.Require("Init")

	m.AlterTable("example", func(t *migration.T) {
		t.Column("val").NotNull()
	})
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrIrreversible = errors.New("migration is irreversible")
)

Functions

This section is empty.

Types

type Column

type Column[T any] struct {
	// contains filtered or unexported fields
}

Column represents the configuration of a column in a table.

func (*Column[T]) Default

func (c *Column[T]) Default(d T) *Column[T]

Default sets the default value of the column.

func (*Column[T]) NoDefault

func (c *Column[T]) NoDefault() *Column[T]

NoDefault disables the default value of the column.

func (*Column[T]) Null

func (c *Column[T]) Null(allow bool) *Column[T]

Null configures whether or not NULL values are allowed in the column.

type Dialect

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

Dialect represents an SQL dialect. Dialects are comparable, and the instances of this struct returned by the various functions in this package are guaranteed to always be equal to the result of another call to the same function.

func Postgres

func Postgres() Dialect

func SQLite3

func SQLite3() Dialect

func (Dialect) Name

func (d Dialect) Name() string

type Index

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

Index represents the configuration of an index in a table.

func (*Index) Unique

func (i *Index) Unique(unique bool) *Index

Unique sets whether or not the column enforces uniqueness.

type M

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

M is a type passed to Migrate functions to configure the migration.

func (*M) CreateTable

func (m *M) CreateTable(name string, f func(*T))

CreateTable creates a new table using a configuration determined by f.

func (*M) Require

func (m *M) Require(migrations ...string)

Require marks other migrations as being dependencies of this one. In other words, the named migrations should be applied before this one is.

Calling this function more than once is equivalent to calling it once with all of the same arguments.

The provided migration names should be the name of the migration function minus the "Migrate" prefix. For example,

func MigrateFirst(m *migration.M) {}

func MigrateSecond(m *migration.M) {
  // MigrateSecond depends on MigrateFirst.
  m.Require("First")
}

func (*M) SQL

func (m *M) SQL(stmt string, args ...any)

SQL registers a custom SQL statement to be executed as part of the migration.

Warning: Use of this method causes the migration to become irreversible. To register reversible SQL statements, use UpDown.

func (*M) UpDown

func (m *M) UpDown(up func(*MUp), down func(*MDown))

UpDown registers separate manual steps for migrating up and down. The provided functions should perform opposite operations.

type MDown

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

func (*MDown) SQL

func (m *MDown) SQL(stmt string, args ...any)

type MUp

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

func (*MUp) SQL

func (m *MUp) SQL(stmt string, args ...any)

type MigrationFunc

type MigrationFunc func(m *M)

MigrationFunc is the signature matched by functions that define migrations.

type MigrationPlan

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

MigrationPlan represents a migration plan generated by the Migrate functions. It is intended for internal use.

func PlanUp

func PlanUp(ctx context.Context, db *sql.DB, funcs map[string]MigrationFunc) (*MigrationPlan, error)

PlanUp produces a migration plan that runs all of the migrations, moving the database to the latest schema. It is intended for internal use.

func PlanUpTo

func PlanUpTo(ctx context.Context, db *sql.DB, funcs map[string]MigrationFunc, target string) (*MigrationPlan, error)

func (*MigrationPlan) Run

func (m *MigrationPlan) Run(ctx context.Context, db *sql.DB, dialect Dialect) error

func (*MigrationPlan) Steps

func (m *MigrationPlan) Steps() []string

Steps returns the names of the migrations that will be run in the order that they will be run in. Note that this list includes migrations that might be skipped because they've already been run previously.

type T

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

T provides methods to configure modifications to a database table.

func (*T) Index

func (t *T) Index(names ...string) *Index

Index adds an index to the table that applies to the named columns.

func (*T) Int

func (t *T) Int(name string) *Column[int]

Int adds an integer column to the table.

func (T) Name

func (t T) Name() string

Name returns the name of the table being modified.

func (*T) String

func (t *T) String(name string) *Column[string]

String adds a string column to the table.

Directories

Path Synopsis
cmd
migrationc command
internal

Jump to

Keyboard shortcuts

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