Golang Simple Migration Manager
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.
To get started, you can install GoSMM
using go get:
go get github.com/k1e1n04/gosmm@latest
You can also install the GoSMM
command-line tool with the following:
go install github.com/k1e1n04/gosmm/cmd/gosmm@latest
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"),
}
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.
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:
- Connect to the database.
- Validate the DB configuration.
- Check migration integrity.
- Execute pending migrations.
- Record migration history.
- Close the database connection.
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
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.
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. |
Contributions are welcome! Feel free to submit a pull request on GitHub.
This project is licensed under the MIT License.
Please make sure to back up your database before running migrations. The maintainers are not responsible for any data loss.