Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GuardMySQL ¶
func GuardMySQL(ctx context.Context, conn *sql.Conn, f func(context.Context, *sql.Conn) error) (err error)
GuardMySQL manages migration concurrency with MySQL's GET_LOCK and RELEASE_LOCK functions. It gets a lock named "flit" before calling f and releases it after f returns. GuardMySQL blocks until the lock is acquired or ctx is done. Use this guard function by passing a WithGuard option to New.
Types ¶
type ConfigOption ¶
type ConfigOption func(*Migrator)
A ConfigOption can be passed to New to change the configuration. The WithGlob option configures the pattern used to load migration files. The WithGuard option configures the concurrency guard function.
func WithGlob ¶
func WithGlob(glob string) ConfigOption
WithGlob configures Flit to load migration files matching the given glob.
func WithGuard ¶
func WithGuard(g GuardFunc) ConfigOption
WithGuard configures Flit to call the given GuardFunc for concurrency control. For example, GuardMySQL uses MySQL's GET_LOCK and RELEASE_LOCK functions.
type GuardFunc ¶
GuardFunc is called by Migrator.Migrate to manage concurrency.
type Migrator ¶
type Migrator struct {
// contains filtered or unexported fields
}
A Migrator holds the configuration required to migrate a database. Call New to create a new Migrator.
Example ¶
package main
import (
"context"
"database/sql"
"fmt"
"os"
"github.com/180-studios/flit"
_ "github.com/go-sql-driver/mysql"
_ "github.com/mattn/go-sqlite3"
)
func main() {
db, err := sql.Open("sqlite3", "file::memory:?cache=shared")
if err != nil {
panic(err)
}
defer db.Close()
m := flit.New(db, os.DirFS("testdata/example"))
applied, err := m.Migrate(context.Background())
if err != nil {
panic(err)
}
fmt.Println(applied)
}
Output: [001-first.sql 002-second.sql]
func (*Migrator) Migrate ¶
Migrate applies pending migrations to the database. It returns the names of the migrations that were applied.
Migrations are loaded from .sql files in the root of the configured file system. The migrations are ordered by name before being applied. Each migration is executed as a single SQL statement. After a migration is completed a checksum of its name is recorded in the "flits" table, which is created automatically.
Migrate is guarded by a mutex. This guard can be replaced by passing a WithGuard option to New. For example, GuardMySQL uses MySQL's GET_LOCK and RELEASE_LOCK functions.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
example
command
Example is the example code embedded in the README.
|
Example is the example code embedded in the README. |
|
flit
command
|
|
|
Package mysqltest provides a helper to create test-scoped MySQL databases.
|
Package mysqltest provides a helper to create test-scoped MySQL databases. |
|
Package sqlitetest provides a test helper to create an in-memory SQLite database.
|
Package sqlitetest provides a test helper to create an in-memory SQLite database. |