|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/boringsql/regresql/regresql" |
| 8 | + "github.com/spf13/cobra" |
| 9 | +) |
| 10 | + |
| 11 | +var ( |
| 12 | + migrateCwd string |
| 13 | + migrateScript string |
| 14 | + migrateCommand string |
| 15 | + migrateKeepTemp bool |
| 16 | + migrateVerbose bool |
| 17 | + migrateColor bool |
| 18 | + migrateNoColor bool |
| 19 | + migrateFullDiff bool |
| 20 | + migrateNoDiff bool |
| 21 | + |
| 22 | + migrateCmd = &cobra.Command{ |
| 23 | + Use: "migrate [flags]", |
| 24 | + Short: "Test migration impact on query outputs", |
| 25 | + Long: `Execute all queries before and after a migration to detect output changes. |
| 26 | +
|
| 27 | +Workflow: |
| 28 | + 1. Restore database from snapshot |
| 29 | + 2. Execute all queries -> capture "before" state |
| 30 | + 3. Apply migration (script or command) |
| 31 | + 4. Execute all queries -> capture "after" state |
| 32 | + 5. Compare and report differences |
| 33 | +
|
| 34 | +Examples: |
| 35 | + regresql migrate --script migrations/002_add_status.sql |
| 36 | + regresql migrate --command "goose -dir migrations postgres \$PGURI up-to 002" |
| 37 | + regresql migrate --script migrations/002.sql --verbose`, |
| 38 | + Run: func(cmd *cobra.Command, args []string) { |
| 39 | + if err := checkDirectory(migrateCwd); err != nil { |
| 40 | + fmt.Print(err.Error()) |
| 41 | + os.Exit(1) |
| 42 | + } |
| 43 | + |
| 44 | + // Validate: exactly one of --script or --command required |
| 45 | + if migrateScript == "" && migrateCommand == "" { |
| 46 | + fmt.Println("Error: either --script or --command is required") |
| 47 | + os.Exit(1) |
| 48 | + } |
| 49 | + if migrateScript != "" && migrateCommand != "" { |
| 50 | + fmt.Println("Error: --script and --command are mutually exclusive") |
| 51 | + os.Exit(1) |
| 52 | + } |
| 53 | + |
| 54 | + // Validate script file exists if specified |
| 55 | + if migrateScript != "" { |
| 56 | + if _, err := os.Stat(migrateScript); os.IsNotExist(err) { |
| 57 | + fmt.Printf("Error: migration script not found: %s\n", migrateScript) |
| 58 | + os.Exit(1) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + opts := regresql.MigrateOptions{ |
| 63 | + Root: migrateCwd, |
| 64 | + Script: migrateScript, |
| 65 | + Command: migrateCommand, |
| 66 | + KeepTemp: migrateKeepTemp, |
| 67 | + Verbose: migrateVerbose, |
| 68 | + Color: migrateColor, |
| 69 | + NoColor: migrateNoColor, |
| 70 | + FullDiff: migrateFullDiff, |
| 71 | + NoDiff: migrateNoDiff, |
| 72 | + } |
| 73 | + exitCode := regresql.Migrate(opts) |
| 74 | + os.Exit(exitCode) |
| 75 | + }, |
| 76 | + } |
| 77 | +) |
| 78 | + |
| 79 | +func init() { |
| 80 | + RootCmd.AddCommand(migrateCmd) |
| 81 | + |
| 82 | + migrateCmd.Flags().StringVarP(&migrateCwd, "cwd", "C", ".", "Change to Directory") |
| 83 | + migrateCmd.Flags().StringVar(&migrateScript, "script", "", "Path to migration SQL script") |
| 84 | + migrateCmd.Flags().StringVar(&migrateCommand, "command", "", "External migration command (receives $PGURI env var)") |
| 85 | + migrateCmd.Flags().BoolVar(&migrateKeepTemp, "keep-temp", false, "Preserve temporary before/after directories") |
| 86 | + migrateCmd.Flags().BoolVarP(&migrateVerbose, "verbose", "v", false, "Verbose output") |
| 87 | + migrateCmd.Flags().BoolVar(&migrateColor, "color", false, "Force colored output") |
| 88 | + migrateCmd.Flags().BoolVar(&migrateNoColor, "no-color", false, "Disable colored output") |
| 89 | + migrateCmd.Flags().BoolVar(&migrateFullDiff, "diff", false, "Show full diff output (no truncation)") |
| 90 | + migrateCmd.Flags().BoolVar(&migrateNoDiff, "no-diff", false, "Suppress diff output entirely") |
| 91 | +} |
0 commit comments