Skip to content

Commit 082d256

Browse files
committed
Added commands to manage groups
1 parent 9cce7c0 commit 082d256

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

groupmanagement.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/jackc/pgx/v5/pgxpool"
8+
)
9+
10+
// DeleteGroupAndResults deletes a group by its ID and all associated results
11+
func DeleteGroupAndResults(ctx context.Context, db *pgxpool.Pool, groupID int) error {
12+
// Delete associated results first
13+
res1, err := db.Exec(ctx, `DELETE FROM dprompts_results WHERE group_id = $1`, groupID)
14+
if err != nil {
15+
return fmt.Errorf("failed to delete results: %w", err)
16+
}
17+
18+
// Delete the group itself
19+
res2, err := db.Exec(ctx, `DELETE FROM dprompt_groups WHERE id = $1`, groupID)
20+
if err != nil {
21+
return fmt.Errorf("failed to delete group: %w", err)
22+
}
23+
24+
fmt.Printf("Deleted %d results and %d group(s) for group ID %d\n", res1.RowsAffected(), res2.RowsAffected(), groupID)
25+
return nil
26+
}

main.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ func main() {
2222

2323
totalGroups := flag.Bool("total-groups", false, "Display total number of groups (view mode)")
2424
groupID := flag.Int("group", 0, "Display results for a specific group ID (view mode)")
25-
25+
deleteGroupID := flag.Int("delete-group-id", 0, "Delete a specific group ID and all its associated results")
26+
2627
n := flag.Int("n", 10, "Number of results to display (view mode)")
2728

2829
queueN := flag.Int("queue-n", 10, "Number of queued jobs to display (for view action)")
@@ -47,6 +48,14 @@ func main() {
4748
RunClient(ctx, driver, *argsJSON, *metadataJSON, *bulkFile, dbPool)
4849
case "worker":
4950
RunWorker(ctx, driver, cancel, dbPool)
51+
case "delete-group":
52+
if *deleteGroupID == 0 {
53+
log.Fatal().Msg("Please provide --delete-group-id")
54+
}
55+
if err := DeleteGroupAndResults(ctx, dbPool, *deleteGroupID); err != nil {
56+
log.Fatal().Err(err).Msg("Failed to delete group and results")
57+
}
58+
log.Info().Int("group_id", *deleteGroupID).Msg("Deleted group and associated results")
5059
case "view":
5160
if *totalGroups {
5261
if err := viewTotalGroups(ctx, dbPool); err != nil {

0 commit comments

Comments
 (0)