Skip to content

k1e1n04/gosmm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoSMM

Golang Simple Migration Manager

GoDoc License: MIT GitHub release

Overview

GoSMM is a simple yet powerful SQL migration manager written in Go. This library enables you to manage SQL database migrations for multiple database drivers including Postgres, MySQL, and SQLite3.

The GoSMM package allows you to perform tasks like:

  • Checking migration integrity.
  • Executing migration files in order.
  • Creating migration history tables.

Note: Please write your migration files in SQL format.

By emphasizing SQL-based migration files, GoSMM aims to provide a straightforward and consistent approach to database migration tasks.

Installation

As a Library

To get started, you can install GoSMM using go get:

go get github.com/k1e1n04/gosmm@latest

As a Command-line Tool

You can also install the GoSMM command-line tool with the following:

go install github.com/k1e1n04/gosmm/cmd/gosmm@latest

Usage

As a Library

Configuration

driver := os.Getenv("DB_DRIVER")
config := gosmm.DBConfig{
    Driver:        driver,
    Host:          os.Getenv("DB_HOST"),
	// Port is an int
    Port:          port,
    User:          os.Getenv("DB_USER"),
    Password:      os.Getenv("DB_PASSWORD"),
    DBName:        os.Getenv("DB_NAME"),
}

Fields:

  • Driver: Database driver ("postgres", "mysql", or "sqlite3").
  • Host: Hostname of your database.
  • Port: Port number for the database.
  • User: Username for the database.
  • Password: Password for the database.
  • DBName: The name of the database.

Performing Migrations

To perform migrations, use the Migrate function:

db, err := gosmm.ConnectDB(config)
if err != nil {
    log.Fatalf("Connection failed: %v", err)
}
err = gosmm.Migrate(db, os.Getenv("MIGRATIONS_DIR"), driver)
if err != nil {
    log.Fatalf("Migration failed: %v", err)
}
err = gosmm.CloseDB(db)
if err != nil {
    log.Fatalf("Connection failed: %v", err)
}

This will:

  1. Connect to the database.
  2. Validate the DB configuration.
  3. Check migration integrity.
  4. Execute pending migrations.
  5. Record migration history.
  6. Close the database connection.

As a Command-line Tool

Configuration

The CLI tool uses environment variables for configuration. You can either use export to set them or place them in a .env file.

Here's a list of required environment variables for the CLI:

  • GOSMM_DRIVER: Database driver ("postgres", "mysql", or "sqlite3").
  • GOSMM_HOST: Hostname of your database.
  • GOSMM_PORT: Port number for the database.
  • GOSMM_USER: Username for the database.
  • GOSMM_PASSWORD: Password for the database.
  • GOSMM_DBNAME: The name of the database.
  • GOSMM_MIGRATIONS_DIR (Optional): The directory containing your SQL migration files. By default, this is set to ./migrations in your project's root directory.

Using export

export GOSMM_DRIVER=postgres
export GOSMM_HOST=localhost
export GOSMM_PORT=5432
export GOSMM_USER=postgres
export GOSMM_PASSWORD=password
export GOSMM_DBNAME=gosmm
export GOSMM_MIGRATIONS_DIR=./migrations

Using .env file Create a .env file and fill it like this:

GOSMM_DRIVER=postgres
GOSMM_HOST=localhost
GOSMM_PORT=5432
GOSMM_USER=postgres
GOSMM_PASSWORD=password
GOSMM_DBNAME=gosmm
GOSMM_MIGRATIONS_DIR=./migrations

Command-line Commands

  • gosmm status: Provides the current status of all database migrations.
  • gosmm migrate: Runs all pending database migrations.
  • gosmm restore: Cleans up any failed migration states by removing them from the migration history table.

Migration History Table

GoSMM will create a migration history table in your database to keep track of which migrations have been executed. The table will be named gosmm_migration_history and will have the following schema:

Column Name Data Type Description
installed_rank int The rank of the migration.
filename TEXT The name of the migration script.
installed_on TIMESTAMP The timestamp when the migration was installed.
execution_time int The time it took to execute the migration.
success BOOLEAN Whether the migration was successful or not.

How to Contribute

Contributions are welcome! Feel free to submit a pull request on GitHub.

License

This project is licensed under the MIT License.

Disclaimer

Please make sure to back up your database before running migrations. The maintainers are not responsible for any data loss.