golangmigrator

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2024 License: MIT Imports: 10 Imported by: 0

README

golangmigrator provides a sqlitestdb.Migrator that can be used to migrate the template database using golang-migrate.

Because Hash() requires calculating a unique hash based on the contents of the migrations, this implementation only supports reading migration files from disk or from an embedded filesystem.

package db_test

import (
	"testing"

	_ "github.com/mattn/go-sqlite3"
	"github.com/terinjokes/sqlitestdb"
	"github.com/terinjokes/sqlitestdb/migrators/golangmigrator"
)

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

func TestMigrateFromDisk(t *testing.T) {
	gm := golangmigrator.New("migrations")
	db := sqlitestdb.New(t, sqlitestdb.Config{Driver: "sqlite3"}, gm)

	var version string
	err := db.QueryRowContext("sqlite_version()").Scan(&version)
	if err != nil {
		t.Fatalf("could not read from SQLite: %+v\n", err)
	}
}

func TestMigrateFromEmbeddedFS(t *testing.T) {
	gm := golangmigrator.New("migrations", golangmigrator.WithFS(migrationsFS))
	db := sqlitestdb.New(t, sqlitestdb.Config{Driver: "sqlite3"}, gm)

	var version string
	err := db.QueryRowContext("sqlite_version()").Scan(&version)
	if err != nil {
		t.Fatalf("could not read from SQLite: %+v\n", err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GolangMigrator

type GolangMigrator struct {
	MigrationsDir string
	FS            fs.FS
}

GolangMigrator is a sqlitestdb.Migrator that uses golang-migrate to perform migrations.

Because [Hash] requires calculating a unique hash based on the contents of the migrations, this implementation only supports reading migration files from disk or an embedded filesystem.

func New

func New(migrationsDir string, opts ...Option) *GolangMigrator

New returns a GolangMigrator, which implements sqlitestdb.Migrator using golang-migrate to perform up migrations.

func (*GolangMigrator) Hash

func (gm *GolangMigrator) Hash() (string, error)

func (*GolangMigrator) Migrate

func (gm *GolangMigrator) Migrate(_ context.Context, _ *sql.DB, templateConfig sqlitestdb.Config) error

Migrate runs migrate.Up() to migrate the template database.

type Option

type Option func(*GolangMigrator)

Option provides a way to configure the GolangMigrator struct and its behavior.

golang-migrate documentation: https://github.com/golang-migrate/migrate

func WithFS

func WithFS(dir fs.FS) Option

WithFS specifies a fs.FS from which to read the migration files. If not specified as an option to New, the migrator will read from the real filesystem.

Jump to

Keyboard shortcuts

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