miglite

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2026 License: MIT Imports: 1 Imported by: 0

README

miglite - Lite database schema migration tool by Go

GitHub go.mod Go version GitHub tag (latest SemVer) Go Report Card Unit-Tests Go Reference

中文说明

miglite is a minimalist database schema migration tool implemented in Golang.

  • Easy to use with minimal dependencies
  • Developed based on database/sql without adding any driver dependencies by default
  • Uses raw SQL files as migration files
    • SQL filename format: YYYYMMDD-HHMMSS-{migration-name}.sql
  • 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 .env file in the directory
    • Automatically loads default configuration file ./miglite.yaml
  • Supports mysql, sqlite, postgres databases
    • When used as a library, you need to add your own DB driver dependencies
    • When using the miglite command-line tool directly, driver dependencies are already included

Installation

Using the miglite command-line tool:

# install it by go
go install github.com/gookit/miglite/cmd/miglite@latest

Using as a Go dependency library:

go get github.com/gookit/miglite

# import "github.com/gookit/miglite"

Direct CLI Usage

Using the miglite command-line tool directly.

help

Configuration

miglite supports configuration via miglite.yaml file or environment variables.

  • Can work without a configuration file, using the environment variable DATABASE_URL directly
  • Configuration file defaults to ./miglite.yaml, but can be specified via the --config parameter
miglite.yaml Example
database:
  driver: sqlite  # or mysql, postgresql
  dsn: ./miglite.db  # or connection string for other databases
migrations:
  path: ./migrations
Environment Variables
  • DATABASE_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

Creating Migrations
miglite create add-users-table

This 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;
Running Migrations
# 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 status

View migration status:

status

Using as a Library

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/sqlite CGO-free driver
    • github.com/ncruces/go-sqlite3 CGO-free Based on Wasm(wazero)
    • github.com/mattn/go-sqlite3 NEED cgo
    • github.com/glebarez/go-sqlite Based on modernc.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

Building Your Own Command Tool

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 under command to execute the logic.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BuildTime = "2025-11-05T09:00:00Z"
View Source
var GitCommit = "ab3cd4ef"
View Source
var GoVersion = "1.21"
View Source
var Version = "0.1.0"

Version represents the version of the application

Functions

func InitInfo

func InitInfo(version, goVer, buildTime, gitCommit string)

InitInfo initializes the version, build time, and git commit

Types

This section is empty.

Directories

Path Synopsis
cmd
miglite module
pkg

Jump to

Keyboard shortcuts

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