Skip to content

Commit

Permalink
chore: batch update size 50 (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-bytebase authored Feb 7, 2025
1 parent 3268780 commit a4fe15b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.11
1.0.12
20 changes: 16 additions & 4 deletions provider/resource_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ func resourceProjectDelete(ctx context.Context, d *schema.ResourceData, m interf
return diags
}

const batchSize = 50

func updateDatabasesInProject(ctx context.Context, d *schema.ResourceData, client api.Client, projectName string) diag.Diagnostics {
filter := fmt.Sprintf(`project == "%s"`, projectName)
databases, err := client.ListDatabase(ctx, "-", filter)
Expand Down Expand Up @@ -409,9 +411,14 @@ func updateDatabasesInProject(ctx context.Context, d *schema.ResourceData, clien
}
}

if len(batchTransferDatabases) > 0 {
for i := 0; i < len(batchTransferDatabases); i += batchSize {
end := i + batchSize
if end > len(batchTransferDatabases) {
end = len(batchTransferDatabases)
}
batch := batchTransferDatabases[i:end]
if _, err := client.BatchUpdateDatabases(ctx, &v1pb.BatchUpdateDatabasesRequest{
Requests: batchTransferDatabases,
Requests: batch,
Parent: "instances/-",
}); err != nil {
return diag.Errorf("failed to assign databases to project %s with error: %v", projectName, err.Error())
Expand Down Expand Up @@ -439,9 +446,14 @@ func updateDatabasesInProject(ctx context.Context, d *schema.ResourceData, clien
})
}
}
if len(unassignDatabases) > 0 {
for i := 0; i < len(unassignDatabases); i += batchSize {
end := i + batchSize
if end > len(unassignDatabases) {
end = len(unassignDatabases)
}
batch := unassignDatabases[i:end]
if _, err := client.BatchUpdateDatabases(ctx, &v1pb.BatchUpdateDatabasesRequest{
Requests: unassignDatabases,
Requests: batch,
Parent: "instances/-",
}); err != nil {
return diag.Errorf("failed to move databases to default project with error: %v", err.Error())
Expand Down

0 comments on commit a4fe15b

Please sign in to comment.