Files
lincodex/backend/cmd/migrate/main.go
T

117 lines
3.4 KiB
Go

// Command migrate applies, inspects, or rolls back the embedded schema
// migrations against DATABASE_URL with the same runner and advisory lock that
// the backend uses at startup. It is intended for local development.
package main
import (
"context"
"errors"
"fmt"
"io"
"os"
"os/signal"
"syscall"
"time"
"lincodex/backend/config"
"lincodex/backend/db"
"lincodex/backend/internal/adapter/secondary/postgres"
)
const usage = "usage: migrate status|up|down-one"
func main() {
if err := run(os.Args[1:], os.Stdout); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func run(args []string, output io.Writer) (runErr error) {
if len(args) != 1 {
return errors.New("main.run - " + usage)
}
command := args[0]
databaseURL := config.DatabaseURLFromEnv(os.LookupEnv)
if databaseURL == "" {
return errors.New("main.run - DATABASE_URL is not set")
}
migrations, err := db.Migrations()
if err != nil {
return fmt.Errorf("main.run - load migrations: %w", err)
}
migrator, err := postgres.NewMigrator(databaseURL, migrations)
if err != nil {
return fmt.Errorf("main.run - create migrator: %w", err)
}
defer func() {
if err := migrator.Close(); err != nil {
runErr = errors.Join(runErr, fmt.Errorf("main.run - close migrator: %w", err))
}
}()
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
switch command {
case "status":
return printStatus(ctx, migrator, output)
case "up":
return applyUp(ctx, migrator, output)
case "down-one":
return rollBackOne(ctx, migrator, output)
default:
return errors.New("main.run - " + usage)
}
}
func printStatus(ctx context.Context, migrator *postgres.Migrator, output io.Writer) error {
statuses, err := migrator.Status(ctx)
if err != nil {
return fmt.Errorf("main.printStatus - read status: %w", err)
}
for _, status := range statuses {
appliedAt := "pending"
if status.Applied {
appliedAt = status.AppliedAt.UTC().Format(time.RFC3339)
}
if _, err := fmt.Fprintf(output, "%-20s %s\n", appliedAt, status.Name); err != nil {
return fmt.Errorf("main.printStatus - write status: %w", err)
}
}
return nil
}
func applyUp(ctx context.Context, migrator *postgres.Migrator, output io.Writer) error {
applied, err := migrator.Up(ctx)
if err != nil {
return fmt.Errorf("main.applyUp - apply migrations: %w", err)
}
for _, migration := range applied {
if _, err := fmt.Fprintf(output, "applied %s (%s)\n", migration.Name, migration.Duration); err != nil {
return fmt.Errorf("main.applyUp - write result: %w", err)
}
}
if _, err := fmt.Fprintf(output, "%d migration(s) applied\n", len(applied)); err != nil {
return fmt.Errorf("main.applyUp - write summary: %w", err)
}
return nil
}
func rollBackOne(ctx context.Context, migrator *postgres.Migrator, output io.Writer) error {
migration, err := migrator.DownOne(ctx)
if errors.Is(err, postgres.ErrNoMigrationToRollBack) {
_, writeErr := fmt.Fprintln(output, "no applied migration to roll back")
if writeErr != nil {
return fmt.Errorf("main.rollBackOne - write result: %w", writeErr)
}
return nil
}
if err != nil {
return fmt.Errorf("main.rollBackOne - roll back migration: %w", err)
}
if _, err := fmt.Fprintf(output, "rolled back %s (%s)\n", migration.Name, migration.Duration); err != nil {
return fmt.Errorf("main.rollBackOne - write result: %w", err)
}
return nil
}