feat: add the bm:seed command
Validates a whole JSON file (course keys, meeting numbers, values, dates) and upserts meetings, courses, class sessions, deadlines and readings in one transaction, matched on their natural identity. Prints a created, updated and unchanged summary; --dry-run only validates. Ships a small example fixture with made-up data.
This commit is contained in:
58
console/seed.go
Normal file
58
console/seed.go
Normal file
@@ -0,0 +1,58 @@
|
||||
// Code generated by summer make. DO NOT EDIT.
|
||||
|
||||
package console
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/bonfire"
|
||||
"git.golem15.com/jakub/sm-bm-plugin/classes"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// SeedCommand returns the bm:seed console command. withDB runs the
|
||||
// command's work with the application's database; the plugin supplies it.
|
||||
func SeedCommand(withDB func(ctx context.Context, fn func(*gorm.DB) error) error) bonfire.Command {
|
||||
return bonfire.Command{
|
||||
Name: "bm:seed",
|
||||
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},
|
||||
},
|
||||
Run: func(ctx context.Context, in bonfire.Input, out bonfire.Output) error {
|
||||
path, _ := in.Argument("file")
|
||||
if path == "" {
|
||||
return fmt.Errorf("bm:seed: a file is required")
|
||||
}
|
||||
fh, err := os.Open(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("bm:seed: %w", err)
|
||||
}
|
||||
defer fh.Close()
|
||||
file, err := classes.ParseSeed(fh)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
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
|
||||
}
|
||||
return withDB(ctx, func(db *gorm.DB) error {
|
||||
sum, err := classes.Seed(ctx, db, file)
|
||||
if err != nil {
|
||||
return fmt.Errorf("bm:seed: nothing was written: %w", err)
|
||||
}
|
||||
out.Table([]string{"kind", "created", "updated", "unchanged"}, sum.Rows())
|
||||
out.Success("bm:seed: " + path + " applied")
|
||||
return nil
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
69
console/seed_test.go
Normal file
69
console/seed_test.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package console_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/bonfire"
|
||||
"git.golem15.com/jakub/sm-bm-plugin/console"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func TestSeedCommandShape(t *testing.T) {
|
||||
cmd := console.SeedCommand(nil)
|
||||
if cmd.Name != "bm:seed" || cmd.Description == "" {
|
||||
t.Errorf("command = %q %q", cmd.Name, cmd.Description)
|
||||
}
|
||||
if len(cmd.Args) != 1 || cmd.Args[0].Name != "file" || !cmd.Args[0].Required {
|
||||
t.Errorf("Args = %+v, want one required file argument", cmd.Args)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSeedCommandChecksTheFileBeforeTheDatabase(t *testing.T) {
|
||||
called := false
|
||||
withDB := func(ctx context.Context, fn func(*gorm.DB) error) error {
|
||||
called = true
|
||||
return errors.New("no database in this test")
|
||||
}
|
||||
cmds := []bonfire.Command{console.SeedCommand(withDB)}
|
||||
dir := t.TempDir()
|
||||
invalid := filepath.Join(dir, "invalid.json")
|
||||
if err := os.WriteFile(invalid, []byte(`{"readings": [{"course": "ghost", "title": "X"}]}`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
broken := filepath.Join(dir, "broken.json")
|
||||
if err := os.WriteFile(broken, []byte(`{"courses": [`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var out bytes.Buffer
|
||||
for _, args := range [][]string{nil, {filepath.Join(dir, "missing.json")}, {broken}, {invalid}} {
|
||||
if err := bonfire.Call(context.Background(), cmds, "bm:seed", args, &out); err == nil {
|
||||
t.Errorf("bm:seed %v succeeded", args)
|
||||
}
|
||||
}
|
||||
err := bonfire.Call(context.Background(), cmds, "bm:seed", []string{invalid}, &out)
|
||||
if err == nil || !strings.Contains(err.Error(), "nothing was written") || !strings.Contains(err.Error(), "ghost") {
|
||||
t.Errorf("invalid file error = %v", err)
|
||||
}
|
||||
if called {
|
||||
t.Error("bm:seed reached the database with an invalid file")
|
||||
}
|
||||
|
||||
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"}, &out)
|
||||
if !called || err == nil {
|
||||
t.Errorf("valid file: called=%v err=%v, want the withDB error", called, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user