mirror of
https://github.com/therootcompany/golib.git
synced 2026-11-04 06:55:00 +00:00
feat(sql-migrate/examples): add lite-todos embedded migration runner
This commit is contained in:
parent
2256b12223
commit
b9ef91af1d
3
cmd/sql-migrate/examples/lite-todos/.gitignore
vendored
Normal file
3
cmd/sql-migrate/examples/lite-todos/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
lite-todos
|
||||
*.db
|
||||
.env
|
||||
174
cmd/sql-migrate/examples/lite-todos/demo.go
Normal file
174
cmd/sql-migrate/examples/lite-todos/demo.go
Normal file
@ -0,0 +1,174 @@
|
||||
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 (?, ?)",
|
||||
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.NullString
|
||||
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.String[:10])
|
||||
}
|
||||
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 = datetime('now') WHERE id = ?",
|
||||
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 = ?",
|
||||
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,
|
||||
"INSERT OR IGNORE INTO todos (id, title) VALUES (?, ?)",
|
||||
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 = datetime('now') WHERE id = ? 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)
|
||||
}
|
||||
23
cmd/sql-migrate/examples/lite-todos/go.mod
Normal file
23
cmd/sql-migrate/examples/lite-todos/go.mod
Normal file
@ -0,0 +1,23 @@
|
||||
module github.com/therootcompany/golib/cmd/sql-migrate/examples/lite-todos
|
||||
|
||||
go 1.26.1
|
||||
|
||||
require (
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/therootcompany/golib/database/sqlmigrate v1.0.2
|
||||
github.com/therootcompany/golib/database/sqlmigrate/litemigrate v1.0.2
|
||||
modernc.org/sqlite v1.37.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/ncruces/go-strftime v0.1.9 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0 // indirect
|
||||
golang.org/x/sys v0.33.0 // indirect
|
||||
modernc.org/libc v1.65.7 // indirect
|
||||
modernc.org/mathutil v1.7.1 // indirect
|
||||
modernc.org/memory v1.11.0 // indirect
|
||||
)
|
||||
53
cmd/sql-migrate/examples/lite-todos/go.sum
Normal file
53
cmd/sql-migrate/examples/lite-todos/go.sum
Normal file
@ -0,0 +1,53 @@
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs=
|
||||
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA=
|
||||
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/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
|
||||
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
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/litemigrate v1.0.2 h1:Tk6qWqmk9xqcbt6KOP51QrPQMsZVCtIJ3ad6220Q/yY=
|
||||
github.com/therootcompany/golib/database/sqlmigrate/litemigrate v1.0.2/go.mod h1:FBLUKs9lMeahcR0OTiL4yi9bD14Mqj0judkMTma+aB0=
|
||||
golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0 h1:R84qjqJb5nVJMxqWYb3np9L5ZsaDtB+a39EqjV0JSUM=
|
||||
golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0/go.mod h1:S9Xr4PYopiDyqSyp5NjCrhFrqg6A5zA2E/iPHPhqnS8=
|
||||
golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU=
|
||||
golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
|
||||
golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ=
|
||||
golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
|
||||
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/tools v0.33.0 h1:4qz2S3zmRxbGIhDIAgjxvFutSvH5EfnsYrRBj0UI0bc=
|
||||
golang.org/x/tools v0.33.0/go.mod h1:CIJMaWEY88juyUfo7UbgPqbC8rU2OqfAV1h2Qp0oMYI=
|
||||
modernc.org/cc/v4 v4.26.1 h1:+X5NtzVBn0KgsBCBe+xkDC7twLb/jNVj9FPgiwSQO3s=
|
||||
modernc.org/cc/v4 v4.26.1/go.mod h1:uVtb5OGqUKpoLWhqwNQo/8LwvoiEBLvZXIQ/SmO6mL0=
|
||||
modernc.org/ccgo/v4 v4.28.0 h1:rjznn6WWehKq7dG4JtLRKxb52Ecv8OUGah8+Z/SfpNU=
|
||||
modernc.org/ccgo/v4 v4.28.0/go.mod h1:JygV3+9AV6SmPhDasu4JgquwU81XAKLd3OKTUDNOiKE=
|
||||
modernc.org/fileutil v1.3.1 h1:8vq5fe7jdtEvoCf3Zf9Nm0Q05sH6kGx0Op2CPx1wTC8=
|
||||
modernc.org/fileutil v1.3.1/go.mod h1:HxmghZSZVAz/LXcMNwZPA/DRrQZEVP9VX0V4LQGQFOc=
|
||||
modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI=
|
||||
modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito=
|
||||
modernc.org/libc v1.65.7 h1:Ia9Z4yzZtWNtUIuiPuQ7Qf7kxYrxP1/jeHZzG8bFu00=
|
||||
modernc.org/libc v1.65.7/go.mod h1:011EQibzzio/VX3ygj1qGFt5kMjP0lHb0qCW5/D/pQU=
|
||||
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
|
||||
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
|
||||
modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=
|
||||
modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
|
||||
modernc.org/opt v0.1.4 h1:2kNGMRiUjrp4LcaPuLY2PzUfqM/w9N23quVwhKt5Qm8=
|
||||
modernc.org/opt v0.1.4/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns=
|
||||
modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w=
|
||||
modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE=
|
||||
modernc.org/sqlite v1.37.1 h1:EgHJK/FPoqC+q2YBXg7fUmES37pCHFc97sI7zSayBEs=
|
||||
modernc.org/sqlite v1.37.1/go.mod h1:XwdRtsE1MpiBcL54+MbKcaDvcuej+IYSMfLN6gSKV8g=
|
||||
modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0=
|
||||
modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A=
|
||||
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
|
||||
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
|
||||
225
cmd/sql-migrate/examples/lite-todos/main.go
Normal file
225
cmd/sql-migrate/examples/lite-todos/main.go
Normal file
@ -0,0 +1,225 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"embed"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
_ "modernc.org/sqlite"
|
||||
|
||||
"github.com/therootcompany/golib/database/sqlmigrate"
|
||||
"github.com/therootcompany/golib/database/sqlmigrate/litemigrate"
|
||||
)
|
||||
|
||||
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("lite-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()
|
||||
|
||||
liteURL := os.Getenv("LITE_URL")
|
||||
if liteURL == "" {
|
||||
liteURL = "todos.db"
|
||||
}
|
||||
|
||||
db, err := sql.Open("sqlite", liteURL+"?_pragma=foreign_keys(1)")
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: opening database: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer func() { _ = db.Close() }()
|
||||
|
||||
migrations := mustCollectMigrations()
|
||||
runner := litemigrate.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, "lite-todos v%s\n", version)
|
||||
}
|
||||
|
||||
func printUsage(w *os.File) {
|
||||
fmt.Fprintln(w, `USAGE
|
||||
lite-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
|
||||
LITE_URL SQLite database path (default: todos.db, 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])
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
-- init-migrations (down)
|
||||
|
||||
DELETE FROM _migrations WHERE id = '00000001';
|
||||
|
||||
DROP TABLE IF EXISTS _migrations;
|
||||
@ -0,0 +1,10 @@
|
||||
-- init-migrations (up)
|
||||
|
||||
CREATE TABLE IF NOT EXISTS _migrations (
|
||||
id CHAR(8) PRIMARY KEY,
|
||||
name VARCHAR(80) UNIQUE,
|
||||
applied_at TEXT DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
-- leave this as the last line
|
||||
INSERT INTO _migrations (name, id) VALUES ('0001-01-01-001000_init-migrations', '00000001');
|
||||
@ -0,0 +1,6 @@
|
||||
-- create-todos (down)
|
||||
|
||||
DROP TABLE IF EXISTS todos;
|
||||
|
||||
-- leave this as the last line
|
||||
DELETE FROM _migrations WHERE id = 'a1b2c3d4';
|
||||
@ -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 TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
-- leave this as the last line
|
||||
INSERT INTO _migrations (name, id) VALUES ('2026-04-05-001000_create-todos', 'a1b2c3d4');
|
||||
@ -0,0 +1,7 @@
|
||||
-- add-todo-fields (down)
|
||||
|
||||
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';
|
||||
@ -0,0 +1,7 @@
|
||||
-- add-todo-fields (up)
|
||||
|
||||
ALTER TABLE todos ADD COLUMN completed_at TEXT;
|
||||
ALTER TABLE todos ADD COLUMN priority INTEGER NOT NULL DEFAULT 0;
|
||||
|
||||
-- leave this as the last line
|
||||
INSERT INTO _migrations (name, id) VALUES ('2026-04-05-002000_add-todo-fields', 'd4e5f6a7');
|
||||
Loading…
x
Reference in New Issue
Block a user