feat(sql-migrate/examples): add ms-todos embedded migration runner

This commit is contained in:
AJ ONeal 2026-04-09 03:48:23 -06:00
parent 2256b12223
commit 4c5338f16f
No known key found for this signature in database
11 changed files with 544 additions and 0 deletions

View File

@ -0,0 +1,2 @@
ms-todos
.env

View File

@ -0,0 +1,190 @@
package main
import (
"context"
"crypto/rand"
"database/sql"
"encoding/hex"
"fmt"
"strings"
)
func runAdd(ctx context.Context, db *sql.DB, args []string) error {
if len(args) == 0 {
return fmt.Errorf("add requires a title")
}
title := strings.Join(args, " ")
id := mustRandomHex(4)
_, err := db.ExecContext(
ctx,
"INSERT INTO todos (id, title) VALUES (@p1, @p2)",
id,
title,
)
if err != nil {
return fmt.Errorf("creating todo: %w", err)
}
fmt.Printf("Created: %s %s\n", id, title)
return nil
}
func runList(ctx context.Context, db *sql.DB, args []string) error {
rows, err := db.QueryContext(
ctx,
"SELECT id, title, status, completed_at, priority FROM todos ORDER BY created_at",
)
if err != nil {
return fmt.Errorf("listing todos: %w", err)
}
defer func() { _ = rows.Close() }()
count := 0
for rows.Next() {
var (
id string
title string
status string
completedAt sql.NullTime
priority int32
)
if err := rows.Scan(&id, &title, &status, &completedAt, &priority); err != nil {
return fmt.Errorf("scanning todo: %w", err)
}
displayStatus := status
if completedAt.Valid {
displayStatus = fmt.Sprintf("done %s", completedAt.Time.Format("2006-01-02"))
}
pri := ""
if priority > 0 {
pri = fmt.Sprintf(" [p%d]", priority)
}
fmt.Printf(" %s %-10s %s%s\n", id, displayStatus, title, pri)
count++
}
if err := rows.Err(); err != nil {
return fmt.Errorf("reading todos: %w", err)
}
if count == 0 {
fmt.Println("No todos.")
}
return nil
}
func runDone(ctx context.Context, db *sql.DB, args []string) error {
if len(args) == 0 {
return fmt.Errorf("done requires a todo ID")
}
result, err := db.ExecContext(
ctx,
"UPDATE todos SET status = 'done', completed_at = SYSDATETIME() WHERE id = @p1",
args[0],
)
if err != nil {
return fmt.Errorf("marking done: %w", err)
}
n, _ := result.RowsAffected()
if n == 0 {
return fmt.Errorf("todo %q not found", args[0])
}
fmt.Printf("Done: %s\n", args[0])
return nil
}
func runRm(ctx context.Context, db *sql.DB, args []string) error {
if len(args) == 0 {
return fmt.Errorf("rm requires a todo ID")
}
result, err := db.ExecContext(
ctx,
"DELETE FROM todos WHERE id = @p1",
args[0],
)
if err != nil {
return fmt.Errorf("deleting todo: %w", err)
}
n, _ := result.RowsAffected()
if n == 0 {
return fmt.Errorf("todo %q not found", args[0])
}
fmt.Printf("Deleted: %s\n", args[0])
return nil
}
func runSeed(ctx context.Context, db *sql.DB) error {
type seedTodo struct {
ID string
Title string
}
todos := []seedTodo{
{
ID: "seed0010",
Title: "Review Q2 financial reports",
},
{
ID: "seed0011",
Title: "Update SSL certificates",
},
{
ID: "seed0020",
Title: "Deploy v2.3.1 to staging",
},
{
ID: "seed0021",
Title: "Rotate API keys",
},
{
ID: "seed0022",
Title: "Audit access logs",
},
}
for _, t := range todos {
_, err := db.ExecContext(
ctx,
`IF NOT EXISTS (SELECT 1 FROM todos WHERE id = @p1)
INSERT INTO todos (id, title) VALUES (@p1, @p2)`,
t.ID,
t.Title,
)
if err != nil {
return fmt.Errorf("seeding todo %q: %w", t.Title, err)
}
fmt.Printf(" Todo: %s %s\n", t.ID, t.Title)
}
// Mark two as done
for _, id := range []string{"seed0010", "seed0011"} {
_, err := db.ExecContext(
ctx,
"UPDATE todos SET status = 'done', completed_at = SYSDATETIME() WHERE id = @p1 AND status != 'done'",
id,
)
if err != nil {
return fmt.Errorf("seeding mark done %s: %w", id, err)
}
}
fmt.Println("Seed complete.")
return nil
}
func mustRandomHex(n int) string {
b := make([]byte, n)
if _, err := rand.Read(b); err != nil {
panic(err)
}
return hex.EncodeToString(b)
}

View File

@ -0,0 +1,19 @@
module github.com/therootcompany/golib/cmd/sql-migrate/examples/ms-todos
go 1.26.1
require (
github.com/joho/godotenv v1.5.1
github.com/microsoft/go-mssqldb v1.9.8
github.com/therootcompany/golib/database/sqlmigrate v1.0.2
github.com/therootcompany/golib/database/sqlmigrate/msmigrate v1.0.2
)
require (
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
golang.org/x/crypto v0.48.0 // indirect
golang.org/x/text v0.34.0 // indirect
)

View File

@ -0,0 +1,50 @@
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.21.0 h1:fou+2+WFTib47nS+nz/ozhEBnvU96bKHy6LjRsY4E28=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.21.0/go.mod h1:t76Ruy8AHvUAC8GfMWJMa0ElSbuIcO03NLpynfbgsPA=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.13.1 h1:Hk5QBxZQC1jb2Fwj6mpzme37xbCDdNTxU7O9eb5+LB4=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.13.1/go.mod h1:IYus9qsFobWIc2YVwe/WPjcnyCkPKtnHAqUYeebc8z0=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.11.2 h1:9iefClla7iYpfYWdzPCRDozdmndjTm8DXdpCzPajMgA=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.11.2/go.mod h1:XtLgD3ZD34DAaVIIAyG3objl5DynM3CQ/vMcbBNJZGI=
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azkeys v1.4.0 h1:E4MgwLBGeVB5f2MdcIVD3ELVAWpr+WD6MUe1i+tM/PA=
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azkeys v1.4.0/go.mod h1:Y2b/1clN4zsAoUd/pgNAQHjLDnTis/6ROkUfyob6psM=
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/internal v1.2.0 h1:nCYfgcSyHZXJI8J0IWE5MsCGlb2xp9fJiXyxWgmOFg4=
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/internal v1.2.0/go.mod h1:ucUjca2JtSZboY8IoUqyQyuuXvwbMBVwFOm0vdQPNhA=
github.com/AzureAD/microsoft-authentication-library-for-go v1.6.0 h1:XRzhVemXdgvJqCH0sFfrBUTnUJSBrBf7++ypk+twtRs=
github.com/AzureAD/microsoft-authentication-library-for-go v1.6.0/go.mod h1:HKpQxkWaGLJ+D/5H8QRpyQXA1eKjxkFlOMwck5+33Jk=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang-jwt/jwt/v5 v5.3.1 h1:kYf81DTWFe7t+1VvL7eS+jKFVWaUnK9cB1qbwn63YCY=
github.com/golang-jwt/jwt/v5 v5.3.1/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 h1:au07oEsX2xN0ktxqI+Sida1w446QrXBRJ0nee3SNZlA=
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
github.com/golang-sql/sqlexp v0.1.0 h1:ZCD6MBpcuOVfGVqsEmY5/4FtYiKz6tSyUv9LPEDei6A=
github.com/golang-sql/sqlexp v0.1.0/go.mod h1:J4ad9Vo8ZCWQ2GMrC4UCQy1JpCbwU9m3EOqtpKwwwHI=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/microsoft/go-mssqldb v1.9.8 h1:d4IFMvF/o+HdpXUqbBfzHvn/NlFA75YGcfHUUvDFJEM=
github.com/microsoft/go-mssqldb v1.9.8/go.mod h1:eGSRSGAW4hKMy5YcAenhCDjIRm2rhqIdmmwgciMzLus=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/therootcompany/golib/database/sqlmigrate v1.0.2 h1:hcmhYyUFVj/GqyChP+0Ry2WZCHnoruFMbsy+2KVzsfA=
github.com/therootcompany/golib/database/sqlmigrate v1.0.2/go.mod h1:7PQUjwT78Hx+SftcIKI2PH4zSFlrSO0V9h618PJqC38=
github.com/therootcompany/golib/database/sqlmigrate/msmigrate v1.0.2 h1:pqQX7RIqIuB1N3wALUYI6+gQmwGggTW7BusNZ1B2OzU=
github.com/therootcompany/golib/database/sqlmigrate/msmigrate v1.0.2/go.mod h1:/8Or/f+7lSwspebv4fNagW4gEwJfu/JvfQiOrT8SqDk=
golang.org/x/crypto v0.48.0 h1:/VRzVqiRSggnhY7gNRxPauEQ5Drw9haKdM0jqfcCFts=
golang.org/x/crypto v0.48.0/go.mod h1:r0kV5h3qnFPlQnBSrULhlsRfryS2pmewsg+XfMgkVos=
golang.org/x/net v0.51.0 h1:94R/GTO7mt3/4wIKpcR5gkGmRLOuE/2hNGeWq/GBIFo=
golang.org/x/net v0.51.0/go.mod h1:aamm+2QF5ogm02fjy5Bb7CQ0WMt1/WVM7FtyaTLlA9Y=
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/text v0.34.0 h1:oL/Qq0Kdaqxa1KbNeMKwQq0reLCCaFtqu2eNuSeNHbk=
golang.org/x/text v0.34.0/go.mod h1:homfLqTYRFyVYemLBFl5GgL/DWEiH5wcsQ5gSh1yziA=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -0,0 +1,226 @@
package main
import (
"context"
"database/sql"
"embed"
"errors"
"flag"
"fmt"
"os"
"strconv"
"github.com/joho/godotenv"
_ "github.com/microsoft/go-mssqldb"
"github.com/therootcompany/golib/database/sqlmigrate"
"github.com/therootcompany/golib/database/sqlmigrate/msmigrate"
)
const version = "0.1.0"
//go:embed sql/migrations/*.sql
var migrationsFS embed.FS
func main() {
_ = godotenv.Load()
if len(os.Args) > 1 {
switch os.Args[1] {
case "-V", "-version", "--version", "version":
printVersion(os.Stdout)
os.Exit(0)
case "help", "-help", "--help":
printVersion(os.Stdout)
fmt.Fprintln(os.Stdout, "")
printUsage(os.Stdout)
os.Exit(0)
}
}
fs := flag.NewFlagSet("ms-todos", flag.ContinueOnError)
fs.Usage = func() { printUsage(os.Stderr) }
if err := fs.Parse(os.Args[1:]); err != nil {
if errors.Is(err, flag.ErrHelp) {
os.Exit(0)
}
os.Exit(1)
}
args := fs.Args()
if len(args) == 0 {
printVersion(os.Stdout)
fmt.Fprintln(os.Stdout, "")
printUsage(os.Stdout)
os.Exit(0)
}
ctx := context.Background()
msURL := os.Getenv("MS_URL")
if msURL == "" {
fmt.Fprintf(os.Stderr, "Error: MS_URL environment variable is required\n")
os.Exit(1)
}
db, err := sql.Open("sqlserver", msURL)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: connecting to database: %v\n", err)
os.Exit(1)
}
defer func() { _ = db.Close() }()
migrations := mustCollectMigrations()
runner := msmigrate.New(db)
subcmd := args[0]
subArgs := args[1:]
switch subcmd {
case "migrate":
err = runMigrate(ctx, runner, migrations, subArgs)
case "add":
autoMigrate(ctx, runner, migrations)
err = runAdd(ctx, db, subArgs)
case "list":
autoMigrate(ctx, runner, migrations)
err = runList(ctx, db, subArgs)
case "done":
autoMigrate(ctx, runner, migrations)
err = runDone(ctx, db, subArgs)
case "rm":
autoMigrate(ctx, runner, migrations)
err = runRm(ctx, db, subArgs)
case "seed":
autoMigrate(ctx, runner, migrations)
err = runSeed(ctx, db)
default:
fmt.Fprintf(os.Stderr, "Unknown command: %s\n\n", subcmd)
printUsage(os.Stderr)
os.Exit(1)
}
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
func mustCollectMigrations() []sqlmigrate.Script {
migrations, err := sqlmigrate.Collect(migrationsFS, "sql/migrations")
if err != nil {
fmt.Fprintf(os.Stderr, "Error: collecting migrations: %v\n", err)
os.Exit(1)
}
return migrations
}
func autoMigrate(ctx context.Context, runner sqlmigrate.Migrator, migrations []sqlmigrate.Script) {
if _, err := sqlmigrate.Latest(ctx, runner, migrations); err != nil {
fmt.Fprintf(os.Stderr, "Error: auto-migrate: %v\n", err)
os.Exit(1)
}
}
func printVersion(w *os.File) {
fmt.Fprintf(w, "ms-todos v%s\n", version)
}
func printUsage(w *os.File) {
fmt.Fprintln(w, `USAGE
ms-todos <command> [args]
COMMANDS
migrate up [n] apply pending migrations (all by default)
migrate down [n] roll back migrations (1 by default)
migrate status show applied and pending migrations
migrate reset drop all tables (rollback all migrations)
seed insert deterministic test data (idempotent)
add <title> create a new todo
list list todos
done <id> mark a todo as done
rm <id> delete a todo
ENVIRONMENT
MS_URL SQL Server connection string (loaded from .env)`)
}
func runMigrate(ctx context.Context, runner sqlmigrate.Migrator, migrations []sqlmigrate.Script, args []string) error {
if len(args) == 0 {
return fmt.Errorf("migrate requires a subcommand: up, down, status, or reset")
}
switch args[0] {
case "up":
n := -1
if len(args) > 1 {
var err error
n, err = strconv.Atoi(args[1])
if err != nil || n < 0 {
return fmt.Errorf("%q is not a valid count", args[1])
}
}
applied, err := sqlmigrate.Up(ctx, runner, migrations, n)
if len(applied) == 0 && err == nil {
fmt.Println("Already up-to-date.")
} else if err == nil {
fmt.Printf("Applied %d migration(s).\n", len(applied))
for _, m := range applied {
fmt.Printf(" %s\n", m.Name)
}
}
return err
case "down":
n := 1
if len(args) > 1 {
var err error
n, err = strconv.Atoi(args[1])
if err != nil || n < 0 {
return fmt.Errorf("%q is not a valid count", args[1])
}
}
rolled, err := sqlmigrate.Down(ctx, runner, migrations, n)
if len(rolled) == 0 && err == nil {
fmt.Println("No migrations to roll back.")
} else if err == nil {
fmt.Printf("Rolled back %d migration(s).\n", len(rolled))
for _, m := range rolled {
fmt.Printf(" %s\n", m.Name)
}
}
return err
case "status":
status, err := sqlmigrate.GetStatus(ctx, runner, migrations)
if err != nil {
return err
}
fmt.Printf("Applied: %d\n", len(status.Applied))
for _, m := range status.Applied {
fmt.Printf(" %s\n", m.Name)
}
fmt.Printf("\nPending: %d\n", len(status.Pending))
for _, m := range status.Pending {
fmt.Printf(" %s\n", m.Name)
}
return nil
case "reset":
rolled, err := sqlmigrate.Drop(ctx, runner, migrations)
if len(rolled) == 0 && err == nil {
fmt.Println("Nothing to reset.")
} else if err == nil {
fmt.Printf("Reset: rolled back %d migration(s).\n", len(rolled))
for _, m := range rolled {
fmt.Printf(" %s\n", m.Name)
}
}
return err
default:
return fmt.Errorf("unknown migrate subcommand: %s (use up, down, status, or reset)", args[0])
}
}

View File

@ -0,0 +1,5 @@
-- init-migrations (down)
DELETE FROM _migrations WHERE id = '00000001';
DROP TABLE IF EXISTS _migrations;

View File

@ -0,0 +1,11 @@
-- init-migrations (up)
IF OBJECT_ID('_migrations', 'U') IS NULL
CREATE TABLE _migrations (
id CHAR(8) PRIMARY KEY,
name VARCHAR(80) NULL UNIQUE,
applied_at DATETIME2 DEFAULT SYSDATETIME()
);
-- leave this as the last line
INSERT INTO _migrations (name, id) VALUES ('0001-01-01-001000_init-migrations', '00000001');

View File

@ -0,0 +1,6 @@
-- create-todos (down)
DROP TABLE IF EXISTS todos;
-- leave this as the last line
DELETE FROM _migrations WHERE id = 'a1b2c3d4';

View File

@ -0,0 +1,12 @@
-- create-todos (up)
CREATE TABLE todos (
id CHAR(8) PRIMARY KEY,
title VARCHAR(255) NOT NULL,
status VARCHAR(20) NOT NULL DEFAULT 'pending',
created_at DATETIME2 NOT NULL DEFAULT SYSDATETIME(),
updated_at DATETIME2 NOT NULL DEFAULT SYSDATETIME()
);
-- leave this as the last line
INSERT INTO _migrations (name, id) VALUES ('2026-04-05-001000_create-todos', 'a1b2c3d4');

View File

@ -0,0 +1,16 @@
-- add-todo-fields (down)
-- SQL Server requires dropping default constraints before dropping columns
DECLARE @constraint NVARCHAR(256);
SELECT @constraint = name FROM sys.default_constraints
WHERE parent_object_id = OBJECT_ID('todos')
AND parent_column_id = (SELECT column_id FROM sys.columns
WHERE object_id = OBJECT_ID('todos') AND name = 'priority');
IF @constraint IS NOT NULL
EXEC('ALTER TABLE todos DROP CONSTRAINT ' + @constraint);
ALTER TABLE todos DROP COLUMN priority;
ALTER TABLE todos DROP COLUMN completed_at;
-- leave this as the last line
DELETE FROM _migrations WHERE id = 'd4e5f6a7';

View File

@ -0,0 +1,7 @@
-- add-todo-fields (up)
ALTER TABLE todos ADD completed_at DATETIME2 NULL;
ALTER TABLE todos ADD priority INT NOT NULL DEFAULT 0;
-- leave this as the last line
INSERT INTO _migrations (name, id) VALUES ('2026-04-05-002000_add-todo-fields', 'd4e5f6a7');