gom

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2023 License: MIT Imports: 11 Imported by: 0

README

gom

A database migration tool for Go.

Build Status Go Report Card Go Docs License GitHub tag

Gom is a database migration tool, it uses embedding SQL migrations. Requires Go v1.16 or higher.

Features

  • Supports SQLite
  • CLI
  • Embedded migrations
  • Plain SQL for writing schema migrations
  • Incremental migration version using timestamps
  • Run migrations inside a transaction

Install

# To install the gom binary to your $GOPATH/bin directory
go install github.com/alexandermac/gom/cmd/gom

Usage

CLI
gom [FLAGS] DRIVER DBSTRING COMMAND

Flags:
  --dir                Migrations directory name (absolute or relative path)
  --name               A new migration file suffix
  --verbose            Prints debug information

Drivers:
  sqlite3

Commands:
  help                 Shows this help
  version              Prints app version
  init                 Creates the migration directory with a sample migration file and the migrations table in the database
  create               Creates a new migration file
  migrate              Migrates the DB to the most recent version available
  rollback             Roll backs the version by 1

Examples:
  gom --dir db_migrations sqlite3 ./foo.db init
  gom --dir db_migrations --name create_users sqlite3 ./foo.db create
  gom sqlite3 ./foo.db migrate
  gom sqlite3 ./foo.db rollback
Embedded migrations

It's possible to embed sql files into binary and corresponding filesystem abstraction. Such migrations can be applied when the app starts.

package main

import (
    "database/sql"
    "embed"

    "github.com/alexandermac/gom"
)

//go:embed my_migrations
var migrationsDir embed.FS

func main() {
	// connect the database

	log.Println("Migrating the database")
	gom.SetBaseFS(migrationsDir)
	gom.SetMigrationsDir("my_migrations")
	if err := gom.Migrate(db); err != nil {
		panic(err)
	}
}

API

func SetBaseFS(fsys simpleFS)

Sets a base file system to discover migrations. Call this function to pass an embedded migrations variable.

func SetMigrationsDir(dir string)

Sets the migrations directory.

func SetLogger(l Logger)

Sets the logger. Must be compatible with gom.Logger interface.

func Create(dir, name, content string) error

Creates a new migration file. Used in CLI tool.

func Migrate(db *sqlx.DB) error

Migrates the DB to the most recent version available.

func Rollback(db *sqlx.DB) error

Roll backs the version by 1.

License

Licensed under the MIT license.

Author

Alexander Mac

Documentation

Index

Constants

View Source
const (
	LOG_LEVEL_DEBUG = iota
	LOG_LEVEL_INFO
	LOG_LEVEL_WARN
	LOG_LEVEL_ERROR
)
View Source
const (
	DefaultMigrationsDir = "migrations"
)

Variables

This section is empty.

Functions

func Create

func Create(dir, name, content string) error

func Migrate

func Migrate(db *sqlx.DB) error

func Rollback

func Rollback(db *sqlx.DB) error

func SetBaseFS

func SetBaseFS(fsys simpleFS)

func SetDefLogLevel added in v0.1.1

func SetDefLogLevel(logLevel int)

func SetLogger

func SetLogger(l Logger)

func SetMigrationsDir

func SetMigrationsDir(dir string)

Types

type Logger

type Logger interface {
	Debug(args ...any)
	Info(args ...any)
	Warn(args ...any)
	Error(args ...any)

	Debugf(format string, args ...any)
	Infof(format string, args ...any)
	Warnf(format string, args ...any)
	Errorf(format string, args ...any)
}

Directories

Path Synopsis
cmd
gom

Jump to

Keyboard shortcuts

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