migrationx

package
v0.0.0-...-5017867 Latest Latest
Warning

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

Go to latest
Published: May 27, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

README

Sqlite Migrationx

Provides a small utility package which performs SQLite database migrations in a single atomic transaction.

The API is modeled on golang-migrate. The schema_migrations table is compatible with this library, so you can change freely back and forth between them.

The goal of this library is to apply migrations atomically in a single transactions. As a bonus this makes it safe for concurrent use (and there are tests which assert this).

This version uses the zombiezen.com/go/sqlite package.

Example usage

import (
  "context"

  "zombiezen.com/go/sqlite/sqlitex"
)

//go:embed: migrations/*.sql
var migrations embed.FS

func main() {
  pool, err := sqlitex.Open(":memory:", 0, 10)
  if err != nil {
    panic(err)
  }

  defer pool.Close()
  
  if err := Up(context.Background(), pool); err != nil {
    panic(err)
  }

  // Do something with the db here
}

func Up(ctx context.Context, pool *sqlitex.Pool) error {
  conn := pool.Get(ctx)
  defer pool.Put(conn)

  m, err := migrations.NewMigrationsFromFs(migrations, ".")
  if err != nil {
    return err
  }

  return m.Up(ctx, conn)
}

Documentation

Index

Constants

View Source
const VersionSchema string = `` /* 155-byte string literal not displayed */

Variables

This section is empty.

Functions

func Version

func Version(conn *sqlite.Conn) (version uint64, err error)

Version returns the current version of the database.

Types

type Migrations

type Migrations struct {
	UpScripts   []string
	DownScripts []string
}

func NewMigrations

func NewMigrations(up []string, down []string) (*Migrations, error)

func NewMigrationsFromFS

func NewMigrationsFromFS(fsys fs.FS, subpath string) (*Migrations, error)

func (*Migrations) Down

func (m *Migrations) Down(ctx context.Context, conn *sqlite.Conn) (err error)

func (*Migrations) Migrate

func (m *Migrations) Migrate(ctx context.Context, conn *sqlite.Conn, targetVersion uint64) (err error)

func (*Migrations) Up

func (m *Migrations) Up(ctx context.Context, conn *sqlite.Conn) (err error)

Jump to

Keyboard shortcuts

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