miglite is a minimalist database schema migration tool implemented in Golang.
- Easy to use with minimal dependencies
- Developed based on
database/sqlwithout adding any driver dependencies by default - Uses raw SQL files as migration files
- SQL filename format:
YYYYMMDD-HHMMSS-{migration-name}.sql
- SQL filename format:
- Migration SQL is executed within transactions to ensure data consistency
- Can run migrations with zero configuration via environment variables (e.g.,
DATABASE_URL,MIGRATIONS_PATH)- Automatically attempts to load
.envfile in the directory - Automatically loads default configuration file
./miglite.yaml
- Automatically attempts to load
- Supports
mysql,sqlite,postgresdatabases- When used as a library, you need to add your own DB driver dependencies
- When using the
miglitecommand-line tool directly, driver dependencies are already included
Using the miglite command-line tool:
# install it by go
go install github.com/gookit/miglite/cmd/miglite@latestUsing as a Go dependency library:
go get github.com/gookit/miglite
# import "github.com/gookit/miglite"Using the miglite command-line tool directly.
miglite supports configuration via miglite.yaml file or environment variables.
- Can work without a configuration file, using the environment variable
DATABASE_URLdirectly - Configuration file defaults to
./miglite.yaml, but can be specified via the--configparameter
database:
driver: sqlite # or mysql, postgresql
dsn: ./miglite.db # or connection string for other databases
migrations:
path: ./migrationsDATABASE_URL: Database connection URL (e.g.,sqlite://path/to/your.db,mysql://user:pass@tcp(host:port)/dbname)MIGRATIONS_PATH: Migration files path (default:./migrations)
Examples:
MIGRATIONS_PATH = "./migrations"
# sqlite
DATABASE_URL="sqlite://path/to/your.db"
# mysql
DATABASE_URL="mysql://user:passwd@tcp(127.0.0.1:3306)/local_test?charset=utf8mb4&parseTime=True&loc=Local"
# postgresql
DATABASE_URL="postgres://host=localhost port=5432 user=username password=password dbname=dbname sslmode=disable"NOTE: mysql URLs must be tagged with the 'tcp' protocol
miglite create add-users-tableThis will create an SQL file named with the current date in the ./migrations/ directory, with the format YYYYMMDD-HHMMSS-add-users-table.sql.
./migrations/20251105-102325-create-users-table.sql
SQL file content includes a template:
-- Migrate:UP
-- Add migration SQL here
-- Migrate:DOWN
-- Add rollback SQL here (optional)Example migration file:
-- Migrate:UP
CREATE TABLE post (
id int NOT NULL,
title text,
body text,
PRIMARY KEY(id)
);
-- Migrate:DOWN
DROP TABLE post;# Initialize the migrations schema table
miglite init
# Apply all pending migrations
miglite up
# Execute immediately without confirmation
miglite up --yes
# Rollback the most recent migration
miglite down
# Rollback multiple migrations
miglite down --number 3
# View migration status
miglite statusView migration status:
miglite does not depend on any third-party DB driver libraries by itself, so you can use it as a library with your current database driver library.
- Sqlite drivers:
modernc.org/sqliteCGO-free drivergithub.com/ncruces/go-sqlite3CGO-free Based on Wasm(wazero)github.com/mattn/go-sqlite3NEED cgogithub.com/glebarez/go-sqliteBased onmodernc.org/sqlite
- MySQL driver:
github.com/go-sql-driver/mysql
- Postgres driver:
github.com/lib/pq
- MSSQL driver:
github.com/microsoft/go-mssqldb
More drivers see: https://go.dev/wiki/SQLDrivers
You can directly use the miglite library to quickly build your own migration command tool, allowing you to register only the database drivers you need.
package main
import (
"github.com/gookit/miglite"
"github.com/gookit/miglite/pkg/command"
// add your database driver
_ "github.com/go-sql-driver/mysql"
// _ "github.com/lib/pq"
// _ "modernc.org/sqlite"
)
var Version = "0.1.0"
func main() {
// Optional: Information needs to be specified at build time via ldflags
// miglite.InitInfo(Version, GoVersion, BuildTime, GitCommit)
// Create the CLI application
app := command.NewApp("miglite", Version, "Lite database schema migration tool by Go")
// Run the application
app.Run()
}NOTE: If you want to further customize the CLI application, you can freely choose other CLI libraries, parse options, and then call the
handleXXX()methods undercommandto execute the logic.

