feat: add --prune and a rolled-back --dry-run to bm:seed

This commit is contained in:
Jakub Zych
2026-10-02 17:28:07 +02:00
parent e8411adac3
commit 28f68d002f
6 changed files with 192 additions and 23 deletions

View File

@@ -20,7 +20,8 @@ func SeedCommand(withDB func(ctx context.Context, fn func(*gorm.DB) error) error
Description: "Upsert meetings, courses, class sessions, deadlines and readings from a JSON file",
Args: []bonfire.Arg{{Name: "file", Description: "Path of the seed JSON file", Required: true}},
Flags: []bonfire.Flag{
{Name: "dry-run", Description: "Validate the file without writing", Bare: true},
{Name: "dry-run", Description: "Show what the seed would change, then roll it back", Bare: true},
{Name: "prune", Description: "Delete courses of the file's semesters, and sessions and readings of its courses, that the file does not list", Bare: true},
},
Run: func(ctx context.Context, in bonfire.Input, out bonfire.Output) error {
path, _ := in.Argument("file")
@@ -39,17 +40,24 @@ func SeedCommand(withDB func(ctx context.Context, fn func(*gorm.DB) error) error
if err := file.Validate(); err != nil {
return fmt.Errorf("bm:seed: %s is not valid, nothing was written:\n%w", path, err)
}
if dry, _ := in.Flag("dry-run"); dry == "true" {
out.Success(fmt.Sprintf("%s is valid: %d meetings, %d courses, %d class sessions, %d deadlines, %d readings", path,
len(file.Meetings), len(file.Courses), len(file.ClassSessions), len(file.Deadlines), len(file.Readings)))
return nil
}
dry, _ := in.Flag("dry-run")
prune, _ := in.Flag("prune")
opts := classes.SeedOptions{DryRun: dry == "true", Prune: prune == "true"}
out.Printf("%s is valid: %d meetings, %d courses, %d class sessions, %d deadlines, %d readings\n", path,
len(file.Meetings), len(file.Courses), len(file.ClassSessions), len(file.Deadlines), len(file.Readings))
return withDB(ctx, func(db *gorm.DB) error {
sum, err := classes.Seed(ctx, db, file)
sum, err := classes.SeedWith(ctx, db, file, opts)
if err != nil {
return fmt.Errorf("bm:seed: nothing was written: %w", err)
}
out.Table([]string{"kind", "created", "updated", "unchanged"}, sum.Rows())
out.Table([]string{"kind", "created", "updated", "unchanged", "deleted"}, sum.Rows())
for _, r := range sum.Removed {
out.Printf("deleted: %s\n", r)
}
if opts.DryRun {
out.Success("bm:seed: dry run of " + path + ", nothing was written")
return nil
}
out.Success("bm:seed: " + path + " applied")
return nil
})

View File

@@ -55,12 +55,11 @@ func TestSeedCommandChecksTheFileBeforeTheDatabase(t *testing.T) {
}
out.Reset()
if err := bonfire.Call(context.Background(), cmds, "bm:seed", []string{"../testdata/seed.example.json", "--dry-run"}, &out); err != nil {
t.Fatalf("dry run: %v", err)
}
if called || !strings.Contains(out.String(), "3 courses") {
t.Errorf("dry run called=%v output %q", called, out.String())
err = bonfire.Call(context.Background(), cmds, "bm:seed", []string{"../testdata/seed.example.json", "--dry-run", "--prune"}, &out)
if !called || err == nil || !strings.Contains(out.String(), "3 courses") {
t.Errorf("dry run called=%v err=%v output %q, want the validation line and the withDB error", called, err, out.String())
}
called = false
err = bonfire.Call(context.Background(), cmds, "bm:seed", []string{"../testdata/seed.example.json"}, &out)
if !called || err == nil {