Simple idea. Just put the SQL migrations into your code.
- No need to ship migration-files with your executable
- No CLI tools
- No extra pipelines executing migrations
- Easy to extend
package main
import (
"github.com/klyngen/mini-migrator",
_ "github.com/mattn/go-sqlite3"
"database/sql"
)
migrations := []migrator.Migration{{
Name: "test1",
Description: "must see that this tooling works",
Script: "CREATE TABLE TEST1 (id INTEGER, name VARHCAR(50))",
}}
func main() {
db, _ := sql.Open("sqlite3", fmt.Sprintf("file:%v", dbName))
m, _ := migrator.NewMigrator(db, migrator.SQLiteDriver, migrator.MigrationOptions{Strict: true})
err := m.MigrateDatabase(migrations)
if err != nil {
// HANDLE this properly
}
}
- 📦 Supports most common databases (postgres, mysql, sqlite)
- 📦 Easy to extend for most common relational databases
Every migration is put into a table with a name, description, status and a hash. The status is to keep track of the executed migrations and the hash is to ensure that you don't change the queries retrospectively.
- Validation of existing migrations. If earlier migrations were successful and hashes compare, we proceed.
- Write the migration with status
PROGRESS
- Execute the migration
- Update the migration status to either
FAILED
orCOMPLETE