migrate

package module
v0.0.0-...-0ba76d7 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
}

func (*Column[T]) Default

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

func (*Column[T]) DefaultFunc

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

func (*Column[T]) Null

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

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
}

func (*Index) Unique

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

type M

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

func (*M) CreateTable

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

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
}

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) error

func (*Migration) Steps

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

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
}

func (*T) Index

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

func (*T) Int

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

func (T) Name

func (t T) Name() string

func (*T) String

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

Directories

Path Synopsis
cmd
gen
internal

Jump to

Keyboard shortcuts

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