gooselite

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 16, 2021 License: MIT Imports: 19 Imported by: 2

README

gooselite

Build Status Coverage Status GoDevDoc Time Tracker Code lines Comments

gooselite is a reduced version of goose tailored for embedding in your application.

Usage

Install gooselite binary with

go install github.com/vearutop/gooselite/cmd/gooselite

or download it from releases.

Create migrations with gooselite create add_some_table sql in your migrations directory.

Apply migrations from your application.

package main

import (
	"database/sql"
	"embed"
	"log"

	"github.com/vearutop/gooselite/iofs"
)

//go:embed migrations
var migrations embed.FS

func main() {
	db, err := sql.Open("sqlite3", "sql.db") // Open connection to your DB.
	if err != nil {
		log.Fatalf("Failed to open test database: %v", err)
	}

	// Apply migrations.
	if err := iofs.Up(db, migrations, "migrations"); err != nil {
		log.Fatalf("Failed to run up migrations: %v", err)
	}
}

Documentation

Overview

Package gooselite is a reduced version of github.com/pressly/goose tailored for embedding.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoCurrentVersion when a current migration version is not found.
	ErrNoCurrentVersion = errors.New("no current version found")
	// ErrNoNextVersion when the next migration version is not found.
	ErrNoNextVersion = errors.New("no next version found")
	// MaxVersion is the maximum allowed version.
	MaxVersion int64 = 9223372036854775807 // max(int64)

)

Functions

func AddMigration

func AddMigration(up func(*sql.Tx) error, down func(*sql.Tx) error)

AddMigration adds a migration.

func AddNamedMigration

func AddNamedMigration(filename string, up func(*sql.Tx) error, down func(*sql.Tx) error)

AddNamedMigration : Add a named migration.

func Create

func Create(dir, name, migrationType string) error

Create writes a new blank migration file.

func CreateWithTemplate

func CreateWithTemplate(dir string, tmpl *template.Template, name, migrationType string) (err error)

CreateWithTemplate writes a new blank migration file.

func Down

func Down(db *sql.DB, dir string) error

Down rolls back a single migration from the current version.

func DownTo

func DownTo(db *sql.DB, dir string, version int64) error

DownTo rolls back migrations to a specific version.

func EnsureDBVersion

func EnsureDBVersion(db *sql.DB) (int64, error)

EnsureDBVersion retrieves the current version for this DB. Create and initialize the DB version table if it doesn't exist.

func Fix

func Fix(dir string) error

Fix renames timestamped filenames with sequential versions.

func GetDBVersion

func GetDBVersion(db *sql.DB) (int64, error)

GetDBVersion is an alias for EnsureDBVersion, but returns -1 in error.

func NumericComponent

func NumericComponent(name string) (int64, error)

NumericComponent looks for migration scripts with names in the form: XXX_descriptivename.ext where XXX specifies the version number and ext specifies the type of migration.

func OpenDBWithDriver

func OpenDBWithDriver(driver string, dbstring string) (*sql.DB, error)

OpenDBWithDriver creates a connection a database, and modifies goose internals to be compatible with the supplied driver by calling SetDialect.

func Redo

func Redo(db *sql.DB, dir string) error

Redo rolls back the most recently applied migration, then runs it again.

func Reset

func Reset(db *sql.DB, dir string) error

Reset rolls back all migrations.

func Run

func Run(command string, dir string, args ...string) error

Run runs a goose command.

func SetDialect

func SetDialect(d string) error

SetDialect sets the SQLDialect.

func SetLogger

func SetLogger(l Logger)

SetLogger sets the logger for package output.

func SetSequential

func SetSequential(s bool)

SetSequential set whether to use sequential versioning instead of timestamp based versioning.

func SetTableName

func SetTableName(n string)

SetTableName set goose db version table name.

func SetVerbose

func SetVerbose(v bool)

SetVerbose set the goose verbosity mode.

func Status

func Status(db *sql.DB, dir string) error

Status prints the status of all migrations.

func TableName

func TableName() string

TableName returns goose db version table name.

func Up

func Up(db *sql.DB, dir string) error

Up applies all available migrations.

func UpByOne

func UpByOne(db *sql.DB, dir string) error

UpByOne migrates up by a single version.

func UpTo

func UpTo(db *sql.DB, dir string, version int64) error

UpTo migrates up to a specific version.

Types

type ClickHouseDialect

type ClickHouseDialect struct{}

ClickHouseDialect struct.

type Logger

type Logger interface {
	Fatal(v ...interface{})
	Fatalf(format string, v ...interface{})
	Print(v ...interface{})
	Println(v ...interface{})
	Printf(format string, v ...interface{})
}

Logger is standard logger interface.

type Migration

type Migration struct {
	Version      int64
	Next         int64         // next version, or -1 if none
	Previous     int64         // previous version, -1 if none
	Source       string        // path to .sql script or go file
	SourceReader io.ReadCloser // optional reader of .sql script
	Registered   bool
	UpFn         func(*sql.Tx) error // Up go migration function
	DownFn       func(*sql.Tx) error // Down go migration function
}

Migration struct.

func (*Migration) Down

func (m *Migration) Down(db *sql.DB) error

Down runs a down migration.

func (*Migration) String

func (m *Migration) String() string

func (*Migration) Up

func (m *Migration) Up(db *sql.DB) error

Up runs an up migration.

type MigrationRecord

type MigrationRecord struct {
	VersionID int64
	TStamp    time.Time
	IsApplied bool // was this a result of up() or down()
}

MigrationRecord struct.

type Migrations

type Migrations []*Migration

Migrations slice.

func CollectMigrations

func CollectMigrations(dirpath string, current, target int64) (Migrations, error)

CollectMigrations returns all the valid looking migration scripts in the migrations folder and go func registry, and key them by version.

func (Migrations) Current

func (ms Migrations) Current(current int64) (*Migration, error)

Current gets the current migration.

func (Migrations) Down

func (ms Migrations) Down(db *sql.DB) error

Down rolls back a single migration from the current version.

func (Migrations) DownTo

func (ms Migrations) DownTo(db *sql.DB, targetVersion int64) error

DownTo rolls back migrations to a specific version.

func (Migrations) Last

func (ms Migrations) Last() (*Migration, error)

Last gets the last migration.

func (Migrations) Len

func (ms Migrations) Len() int

func (Migrations) Less

func (ms Migrations) Less(i, j int) bool

func (Migrations) Next

func (ms Migrations) Next(current int64) (*Migration, error)

Next gets the next migration.

func (Migrations) Previous

func (ms Migrations) Previous(current int64) (*Migration, error)

Previous : Get the previous migration.

func (Migrations) Redo

func (ms Migrations) Redo(db *sql.DB) error

Redo rolls back the most recently applied migration, then runs it again.

func (Migrations) Reset

func (ms Migrations) Reset(db *sql.DB) error

Reset rolls back all migrations.

func (Migrations) Status

func (ms Migrations) Status(db *sql.DB) error

Status prints the status of all migrations.

func (Migrations) String

func (ms Migrations) String() string

func (Migrations) Swap

func (ms Migrations) Swap(i, j int)

func (Migrations) Up

func (ms Migrations) Up(db *sql.DB) error

Up applies all available migrations.

func (Migrations) UpByOne

func (ms Migrations) UpByOne(db *sql.DB) error

UpByOne migrates up by a single version.

type MySQLDialect

type MySQLDialect struct{}

MySQLDialect struct.

type PostgresDialect

type PostgresDialect struct{}

PostgresDialect struct.

type RedshiftDialect

type RedshiftDialect struct{}

RedshiftDialect struct.

type SQLDialect

type SQLDialect interface {
	// contains filtered or unexported methods
}

SQLDialect abstracts the details of specific SQL dialects for goose's few SQL specific statements.

func GetDialect

func GetDialect() SQLDialect

GetDialect gets the SQLDialect.

type SQLServerDialect

type SQLServerDialect struct{}

SQLServerDialect struct.

type Sqlite3Dialect

type Sqlite3Dialect struct{}

Sqlite3Dialect struct.

type TiDBDialect

type TiDBDialect struct{}

TiDBDialect struct.

Directories

Path Synopsis
cmd
Package iofs contains functions that allows to work with migration using io/fs package.
Package iofs contains functions that allows to work with migration using io/fs package.

Jump to

Keyboard shortcuts

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