migrate

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package migrate implements database migrations SQLite3 using the database/sql interface. In theory, it should work with any SQLite3 driver, but it is tested against github.com/mattn/go-sqlite3.

This package aims to be simple. Versions are tracked using the user_version PRAGMA. Only migrations to the latest version are supported.

Example
s := NewMigrationSet([]Migration{
	{
		From: 0,
		To:   1,
		Func: func(ctx context.Context, d *sql.DB) error {
			t, err := d.Begin()
			if err != nil {
				return err
			}
			defer t.Rollback()
			_, err = t.Exec(`
CREATE TABLE user (
    id INTEGER,
    name TEXT,
    PRIMARY KEY (id)
)`)
			if err != nil {
				return err
			}
			return nil
		},
	},
	{
		From: 1,
		To:   2,
		Func: func(ctx context.Context, d *sql.DB) error {
			t, err := d.Begin()
			if err != nil {
				return err
			}
			defer t.Rollback()
			_, err = t.Exec(`ALTER TABLE user ADD COLUMN description TEXT`)
			if err != nil {
				return err
			}
			return nil
		},
	},
})

d, err := sql.Open("sqlite3", "file::memory:?mode=memory&cache=shared")
if err != nil {
	panic(err)
}
_ = s.Migrate(context.Background(), d)
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Migration

type Migration struct {
	From int
	To   int
	// The migration function must be idempotent.  The function is
	// not wrapped in a transaction, so the function is free to
	// use transactions itself however it wants.
	Func func(context.Context, *sql.DB) error
}

A Migration describes how to migrate a database from one version to another. Only migrations from lower versions to higher versions are supported.

type MigrationSet

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

A MigrationSet contains a set of Migration rules which together define how to migrate various database versions to a latest version.

func NewMigrationSet

func NewMigrationSet(m []Migration) *MigrationSet

NewMigrationSet creates a new MigrationSet.

The input slice is used in the returned MigrationSet and should not be retained by the caller.

The input slice should be sorted in increasing order of the "From" field.

If there is more than one Migration with the same "From" value, then one is chosen arbitrarily when upgrading databases with that version.

func (*MigrationSet) Migrate

func (s *MigrationSet) Migrate(ctx context.Context, d *sql.DB) error

Migrate migrates the database to the latest version.

func (*MigrationSet) NeedsMigrate added in v1.1.0

func (s *MigrationSet) NeedsMigrate(d *sql.DB) (bool, error)

NeedsMigrate returns whether the database needs migration. This is equivalent to checking if the database is not the latest version.

Jump to

Keyboard shortcuts

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