migration

package
v0.0.0-...-a671f9f Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

README

Sqlite Migration

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 stdlib database/sql package.

Example usage

import (
  "database/sql"
  "context"

  _ "modernc.org/sqlite"
)

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

func main() {
  db, err := sql.Open("sqlite", ":memory:")
  if err != nil {
    panic(err)
  }
  defer db.Close()
  
  m, err := migrations.NewMigrationsFromFs(migrations, ".")
  if err != nil {
    panic(err)
  }

  if err := m.Up(ctx, conn); err != nil {
    panic(err)
  }

  // Do something with the db here
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Version

func Version(ctx context.Context, db *sql.DB) (uint64, error)

Version returns the current version of the database.

Types

type Migrations

type Migrations struct {
	*migrationx.Migrations
}

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, db *sql.DB) error

func (*Migrations) Migrate

func (m *Migrations) Migrate(ctx context.Context, db *sql.DB, targetVersion uint64) error

func (*Migrations) Up

func (m *Migrations) Up(ctx context.Context, db *sql.DB) error

Jump to

Keyboard shortcuts

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