goflyway

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2023 License: MIT Imports: 14 Imported by: 0

README

Go Flyway

Migration library written in go inspired by flyway

Download

go get github.com/gabrielaraujosouza/goflyway

Key Validations

Duplicated version
  • It checks if there are scritps with same version number
Checksum mismatch
  • This guarantees the integrity of the script, that is, it is not possible to edit a script after it has been executed

Supported Databases

  • PostgreSQL
  • MySQL (connection instance must have multiStatements set to true)
  • Microsoft SQL Server
  • Sqlite3

Usage

import (
	"database/sql"
	"fmt"

	"github.com/gabrielaraujosouza/goflyway"
	_ "github.com/lib/pq"
)

func main() {

	dbUrl := "postgres://root:root@localhost:5432/goflyway?sslmode=disable"

	// you need to open a db connection
	db, err := sql.Open("postgres", dbUrl)
	if err != nil {
		panic(err)
	}
	defer db.Close()

	// create a config
	conf := goflyway.GoFlywayConfig{
		Db:       db,
		Driver:   goflyway.POSTGRES,
		Location: "[SCRIPT_FOLDER_PATH_HERE]", // Example: /home/user/my-prokect/db/migration
	}

	// Call the method Migrate
	totalScriptsExecuted, err := goflyway.Migrate(conf)

	if err != nil {
		panic(err)
	}

	fmt.Println("total migrations applied:", totalScriptsExecuted)
}

Output:

successfully validated 3 migrations (execution time 0ms)
current version of schema: << Empty Schema >>
migrating schema to version 1 - test create table product
migrating schema to version 2 - test alter table product
migrating schema to version 3 - test remove column from product
successfully applied 3 migrations to schema, now at version v3 (execution time 146ms)

Config Properties

Property Default Description
Table goflyway_schema_history Name of the schema history table that will be used by GoFlyway
SqlMigrationPrefix V File name prefix for SQL migrations
SqlMigrationSeparator __ File name separator for SQL migrations.
Location - Location of migrations scripts
OutOfOrder false Whether to allow migrations to be run out of order
IgnoreMissingMigrations false Ignore missing migrations
Db - Database connection
Driver - Database drive
ShowWarningLog false Shows warning logs

Documentation

Index

Constants

View Source
const (
	POSTGRES driver = "postgres"

	// connection instance must have `multiStatements` set to true
	MYSQL       driver = "mysql"
	MSSQLSERVER driver = "sqlserver"
	SQLITE3     driver = "sqlite3"
)

Variables

View Source
var (
	ErrDatabaseConnectionNull    = errors.New("database connection is null")
	ErrUnsupportedDatabaseDriver = errors.New("unsupported database driver")
	ErrRunnerNotInitialized      = errors.New("runner not initialized")
	ErrLocationCannotBeEmpty     = errors.New("migration location cannot be empty")
)

Functions

func CalculateChecksum

func CalculateChecksum(filename string) (string, error)

CalculateChecksum generate file checksum, it is used to check script integrity

func Migrate

func Migrate(c GoFlywayConfig) (int, error)

Migrate apply migrations to database and returns the total of executed migrations

Types

type ErrMigration

type ErrMigration struct {
	// contains filtered or unexported fields
}

func (*ErrMigration) Error

func (e *ErrMigration) Error() string

type GoFlywayConfig

type GoFlywayConfig struct {
	// Name of the schema history table that will be used by GoFlyway. Defaul is "goflyway_schema_history"
	Table string

	// File name prefix for SQL migrations. Default is "V"
	SqlMigrationPrefix string

	// File name separator for SQL migrations. Default is "__"
	SqlMigrationSeparator string

	// Location of migrations scripts. Examle: "/home/user/my-project/migrations"
	Location string

	// Whether to allow migrations to be run out of order. Default is "false"
	OutOfOrder bool

	// Ignore missing migrations. Default is "false"
	IgnoreMissingMigrations bool

	// Database connection
	Db *sql.DB

	// Database drive
	Driver driver

	// Shows warning logs. Default is "false"
	ShowWarningLog bool
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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