redwing

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2024 License: MIT Imports: 8 Imported by: 0

README

Redwing

Go Report Card

Simple database migrations for Go

Installation

go get github.com/andrewapj/redwing

Databases

  • MySQL

Example usage

var (
//go:embed migrations
migrations embed.FS
)

func main() {
db, err := sql.Open("mysql", "redwing:redwing@tcp(127.0.0.1:3306)/redwing")
if err != nil {
panic("Can't connect to the DB")
}
defer db.Close()

processed, err := redwing.Migrate(db, redwing.MySQL, migrations, &redwing.Options{Logging: true})

if err != nil {
fmt.Printf("Error processing migrations: %v\n", err)
}

fmt.Printf("Processed the following migrations: %v", processed)

  • The user is responsible for creating a sql.DB and closing it when appropriate.
  • In this example the migrations are stored under migrations/. Any other valid path can be used.
  • The Migrate function will return a []int. This contains the numbers representing the migrations successfully processed.
  • The Migrate function will also return an error if a migration fails.
  • Further examples can be found at redwing_examples

Migrations

Migrations should be stored in the following format:

1.sql
2.sql
3.sql
etc...
  • Each migration will be processed sequentially in turn. Any migration that is successfully processed will be returned within the []int

  • If the system encounters a migration that can not be processed it will return an error in addition to the processed migrations, if any.

  • Migrations that have already been processed are stored in the migration table and will not be repeated.

Migrations table

  • Database migrations are stored within the redwing_migrate table.
  • This contains two columns:
    • An id related to the sequential number of the migration.
    • A timestamp of when the migration was applied.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrDialectNotSupported = errors.New("dialect not supported")

ErrDialectNotSupported is an error returned when a dialect is not supported

Functions

func Migrate

func Migrate(db *sql.DB, dialect Dialect, f fs.FS, options *Options) ([]int, error)

Migrate starts a database migration

func PrintLog added in v0.4.0

func PrintLog(s string, r *Options)

Types

type Dialect

type Dialect int

Dialect - A supported database dialect

const (
	//MySQL - The MySQL dialect
	MySQL Dialect = iota
)

type Options added in v0.4.0

type Options struct {
	Logging bool
}

Options allows the user to provide extra optional parameters for the migration

Directories

Path Synopsis
examples
internal

Jump to

Keyboard shortcuts

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