migrate

package module
v0.0.0-...-4a4f425 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2022 License: MIT Imports: 5 Imported by: 0

README

migrate

migrate 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 *migrate.M) {
	m.CreateTable("example", func(t *migrate.T) {
		t.AddColumn("id", migrate.BigInt).PrimaryKey()
		t.AddColumn("name", migrate.String).NotNull(),
		t.AddColumn("val", migrate.String)
	})
}

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

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 *migrate.M) {}

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

type Migration

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

Migration represents a migration plan generated by the Migrate functions.

func Plan

func Plan(funcs map[string]MigrationFunc) (*Migration, error)

Plan produces a migration plan for a given set of migration functions. It is intended for internal use.

func (*Migration) Run

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

func (*Migration) Steps

func (m *Migration) 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 MigrationFunc

type MigrationFunc func(m *M)

MigrationFunc is the signature matched by functions that define migrations.

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
internal

Jump to

Keyboard shortcuts

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