Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions internal/web/handlers_pages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ func TestServer_routes_smoke(t *testing.T) {
{"/static/style.css", http.StatusOK, ".table-modal"},
{"/static/app.js", http.StatusOK, "/api/table?"},
{"/static/app.js", http.StatusOK, "openTableModal"},
{"/static/app.js", http.StatusOK, "GENERATED_DRAFT_KEY"},
{"/static/app.js", http.StatusOK, "GRAPH_ROUTE_KEY"},
{"/static/app.js", http.StatusOK, "route-step"},
{"/static/app.js", http.StatusOK, "routeColorFor"},
{"/static/style.css", http.StatusOK, ".result-shell"},
{"/static/style.css", http.StatusOK, ".ws-route-toggle"},
}
for _, c := range cases {
t.Run(c.path, func(t *testing.T) {
Expand Down
73 changes: 54 additions & 19 deletions internal/web/runners.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,22 @@ func (s *Server) runSeed(ctx context.Context, sess *Session, req SeedRequest, jc

jc.Phase("insert")
totalRows := 0
tableCounts := make(map[string]int, len(targetTables))
var dryRunSQL strings.Builder
for idx, tableName := range targetTables {
tableRows := data[tableName]
tableCounts[tableName] = len(tableRows)
log.Info().Str("table", tableName).Int("rows", len(tableRows)).Msg("Seeding table")
if !req.DryRun {
for i := 0; i < len(tableRows); i += req.BatchSize {
end := i + req.BatchSize
if end > len(tableRows) {
end = len(tableRows)
}
for i := 0; i < len(tableRows); i += req.BatchSize {
end := i + req.BatchSize
if end > len(tableRows) {
end = len(tableRows)
}
if req.DryRun {
query, _ := db.BuildBatchInsert(tableName, tableRows[i:end], sess.DBType)
dryRunSQL.WriteString(query)
dryRunSQL.WriteString(";\n")
} else {
query, values := db.BuildBatchInsert(tableName, tableRows[i:end], sess.DBType)
if _, err := conn.ExecContext(ctx, query, values...); err != nil {
return nil, fmt.Errorf("insert into %s: %w", tableName, err)
Expand All @@ -156,14 +163,20 @@ func (s *Server) runSeed(ctx context.Context, sess *Session, req SeedRequest, jc
for t := range autoSelected {
autoList = append(autoList, t)
}
return map[string]any{
"tables": len(targetTables),
"totalRows": totalRows,
"durationMs": elapsed.Milliseconds(),
"dryRun": req.DryRun,
"order": targetTables,
"auto": autoList,
}, nil
result := map[string]any{
"tables": len(targetTables),
"totalRows": totalRows,
"durationMs": elapsed.Milliseconds(),
"dryRun": req.DryRun,
"order": targetTables,
"auto": autoList,
"tableCounts": tableCounts,
}
if req.DryRun {
result["output"] = dryRunSQL.String()
result["format"] = "sql"
}
return result, nil
}

// GapsRequest mirrors the gaps CLI flags. Tables, when set, restricts the
Expand Down Expand Up @@ -324,15 +337,29 @@ func (s *Server) runGenerate(ctx context.Context, sess *Session, req GenerateReq
if err != nil {
return nil, err
}
tableCounts, totalRows := tableRowCounts(data, targetTables)
jc.Phase("done")
log.Info().Int("tables", len(targetTables)).Str("format", req.Format).Msg("Generation complete")
return map[string]any{
"output": output,
"format": req.Format,
"tables": targetTables,
"output": output,
"format": req.Format,
"tables": targetTables,
"tableCounts": tableCounts,
"totalRows": totalRows,
}, nil
}

func tableRowCounts(data map[string][]map[string]any, sortedTables []string) (map[string]int, int) {
counts := make(map[string]int, len(sortedTables))
total := 0
for _, tableName := range sortedTables {
n := len(data[tableName])
counts[tableName] = n
total += n
}
return counts, total
}

func encodeData(data map[string][]map[string]any, sortedTables []string, format, dbType string) (string, error) {
switch strings.ToLower(format) {
case "json":
Expand Down Expand Up @@ -467,9 +494,17 @@ func (s *Server) runExport(_ context.Context, sess *Session, req ExportRequest,
output = sb.String()
}
jc.Phase("done")
tableNames := make([]string, 0, len(data))
for tableName := range data {
tableNames = append(tableNames, tableName)
}
tableCounts, totalRows := tableRowCounts(data, tableNames)
log.Info().Str("format", req.Format).Int("tables", len(data)).Msg("Export complete")
return map[string]any{
"output": output,
"format": req.Format,
"output": output,
"format": req.Format,
"tables": len(data),
"tableCounts": tableCounts,
"totalRows": totalRows,
}, nil
}
28 changes: 28 additions & 0 deletions internal/web/runners_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package web

import (
"reflect"
"testing"
)

func TestTableRowCounts(t *testing.T) {
data := map[string][]map[string]any{
"users": {
{"id": 1},
{"id": 2},
},
"orders": {
{"id": 10},
},
}

counts, total := tableRowCounts(data, []string{"users", "orders", "missing"})

want := map[string]int{"users": 2, "orders": 1, "missing": 0}
if !reflect.DeepEqual(counts, want) {
t.Fatalf("counts = %+v, want %+v", counts, want)
}
if total != 3 {
t.Fatalf("total = %d, want 3", total)
}
}
Loading
Loading