-
-
Notifications
You must be signed in to change notification settings - Fork 278
fix: tenant delete request will delete all related data too #1517
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
|
Caution Review failedThe pull request is closed. WalkthroughThe changes involve significant modifications to the Changes
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 4
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (3)
- internal/storage/memory/tenantWriter.go (1 hunks)
- internal/storage/postgres/tenantWriter.go (2 hunks)
- internal/storage/postgres/utils/common.go (1 hunks)
Additional comments not posted (5)
internal/storage/postgres/tenantWriter.go (4)
72-76: Ensure proper error handling for transaction initiation.The code correctly handles errors during the transaction initiation.
The code changes are approved.
78-88: Ensure proper error handling for deletion operations.The code correctly handles errors during the deletion operations and tracks the total number of deleted records.
The code changes are approved.
93-97: Ensure proper error handling for tenant retrieval.The code correctly handles errors during the tenant retrieval operation.
The code changes are approved.
100-103: Ensure proper error handling for transaction commit.The code correctly handles errors during the transaction commit operation.
The code changes are approved.
internal/storage/postgres/utils/common.go (1)
43-43: LGTM!The new SQL template is correctly implemented and enhances the functionality for bulk deletion of tenant-associated records.
The code changes are approved.
| _, err = txn.First(constants.RelationTuplesTable, "tenant_id", tenantID) | ||
| if err != nil { | ||
| if _, deleteErr := txn.DeleteAll(constants.RelationTuplesTable, "tenant_id", tenantID); deleteErr != nil { | ||
| return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) | ||
| } | ||
| return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the logic to handle the absence of records correctly.
The current implementation returns an error if no records are found in the RelationTuplesTable. Instead, it should only return an error if the deletion operation fails.
Apply this diff to fix the logic:
- _, err = txn.First(constants.RelationTuplesTable, "tenant_id", tenantID)
- if err != nil {
- if _, deleteErr := txn.DeleteAll(constants.RelationTuplesTable, "tenant_id", tenantID); deleteErr != nil {
- return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String())
- }
- return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String())
+ if _, deleteErr := txn.DeleteAll(constants.RelationTuplesTable, "tenant_id", tenantID); deleteErr != nil {
+ return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String())
}Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| _, err = txn.First(constants.RelationTuplesTable, "tenant_id", tenantID) | |
| if err != nil { | |
| if _, deleteErr := txn.DeleteAll(constants.RelationTuplesTable, "tenant_id", tenantID); deleteErr != nil { | |
| return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) | |
| } | |
| return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) | |
| } | |
| if _, deleteErr := txn.DeleteAll(constants.RelationTuplesTable, "tenant_id", tenantID); deleteErr != nil { | |
| return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) | |
| } |
|
|
||
| _, err = txn.First(constants.AttributesTable, "tenant_id", tenantID) | ||
| if err != nil { | ||
| if _, deleteErr := txn.DeleteAll(constants.AttributesTable, "tenant_id", tenantID); deleteErr != nil { | ||
| return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) | ||
| } | ||
| return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the logic to handle the absence of records correctly.
The current implementation returns an error if no records are found in the AttributesTable. Instead, it should only return an error if the deletion operation fails.
Apply this diff to fix the logic:
- _, err = txn.First(constants.AttributesTable, "tenant_id", tenantID)
- if err != nil {
- if _, deleteErr := txn.DeleteAll(constants.AttributesTable, "tenant_id", tenantID); deleteErr != nil {
- return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String())
- }
- return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String())
+ if _, deleteErr := txn.DeleteAll(constants.AttributesTable, "tenant_id", tenantID); deleteErr != nil {
+ return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String())
}Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| _, err = txn.First(constants.AttributesTable, "tenant_id", tenantID) | |
| if err != nil { | |
| if _, deleteErr := txn.DeleteAll(constants.AttributesTable, "tenant_id", tenantID); deleteErr != nil { | |
| return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) | |
| } | |
| return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) | |
| } | |
| if _, deleteErr := txn.DeleteAll(constants.AttributesTable, "tenant_id", tenantID); deleteErr != nil { | |
| return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) | |
| } |
| _, err = txn.First(constants.BundlesTable, "tenant_id", tenantID) | ||
| if err != nil { | ||
| if _, deleteErr := txn.DeleteAll(constants.BundlesTable, "tenant_id", tenantID); deleteErr != nil { | ||
| return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) | ||
| } | ||
| return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the logic to handle the absence of records correctly.
The current implementation returns an error if no records are found in the BundlesTable. Instead, it should only return an error if the deletion operation fails.
Apply this diff to fix the logic:
- _, err = txn.First(constants.BundlesTable, "tenant_id", tenantID)
- if err != nil {
- if _, deleteErr := txn.DeleteAll(constants.BundlesTable, "tenant_id", tenantID); deleteErr != nil {
- return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String())
- }
- return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String())
+ if _, deleteErr := txn.DeleteAll(constants.BundlesTable, "tenant_id", tenantID); deleteErr != nil {
+ return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String())
}Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| _, err = txn.First(constants.BundlesTable, "tenant_id", tenantID) | |
| if err != nil { | |
| if _, deleteErr := txn.DeleteAll(constants.BundlesTable, "tenant_id", tenantID); deleteErr != nil { | |
| return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) | |
| } | |
| return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) | |
| } | |
| if _, deleteErr := txn.DeleteAll(constants.BundlesTable, "tenant_id", tenantID); deleteErr != nil { | |
| return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) | |
| } |
| _, err = txn.First(constants.SchemaDefinitionsTable, "tenant_id", tenantID) | ||
| if err != nil { | ||
| if _, deleteErr := txn.DeleteAll(constants.SchemaDefinitionsTable, "tenant_id", tenantID); deleteErr != nil { | ||
| return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) | ||
| } | ||
| return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the logic to handle the absence of records correctly.
The current implementation returns an error if no records are found in the SchemaDefinitionsTable. Instead, it should only return an error if the deletion operation fails.
Apply this diff to fix the logic:
- _, err = txn.First(constants.SchemaDefinitionsTable, "tenant_id", tenantID)
- if err != nil {
- if _, deleteErr := txn.DeleteAll(constants.SchemaDefinitionsTable, "tenant_id", tenantID); deleteErr != nil {
- return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String())
- }
- return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String())
+ if _, deleteErr := txn.DeleteAll(constants.SchemaDefinitionsTable, "tenant_id", tenantID); deleteErr != nil {
+ return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String())
}Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| _, err = txn.First(constants.SchemaDefinitionsTable, "tenant_id", tenantID) | |
| if err != nil { | |
| if _, deleteErr := txn.DeleteAll(constants.SchemaDefinitionsTable, "tenant_id", tenantID); deleteErr != nil { | |
| return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) | |
| } | |
| return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) | |
| } | |
| if _, deleteErr := txn.DeleteAll(constants.SchemaDefinitionsTable, "tenant_id", tenantID); deleteErr != nil { | |
| return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
Outside diff range, codebase verification and nitpick comments (1)
internal/storage/postgres/tenantWriter.go (1)
81-89: Ensure Proper Error HandlingThe deferred function ensures that the batch result is closed properly. However, consider logging the
closeErrfor better traceability.Add logging for
closeErr:defer func() { if closeErr := br.Close(); closeErr != nil { if err == nil { err = closeErr } + slog.ErrorContext(ctx, "error closing batch result", slog.Any("error", closeErr)) } }()
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- internal/storage/postgres/tenantWriter.go (2 hunks)
Additional comments not posted (2)
internal/storage/postgres/tenantWriter.go (2)
72-79: Efficient Batch DeletionThe batch deletion of tenant-related records from multiple tables is efficient and ensures data integrity.
The code changes are approved.
91-93: Proper Error HandlingThe error handling for the batch execution is correctly implemented.
The code changes are approved.
| tenant := w.database.WritePool.QueryRow(ctx, utils.DeleteTenantTemplate, tenantID) | ||
| if err != nil { | ||
| return nil, utils.HandleError(ctx, span, err, base.ErrorCode_ERROR_CODE_EXECUTION) | ||
| } | ||
| tenant.Scan(&name, &createdAt) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handle Potential Errors in QueryRow
Ensure that tenant.Scan(&name, &createdAt) is only called if tenant.QueryRow does not return an error.
Add error handling for tenant.QueryRow:
tenant := w.database.WritePool.QueryRow(ctx, utils.DeleteTenantTemplate, tenantID)
+ if err = tenant.Scan(&name, &createdAt); err != nil {
+ return nil, utils.HandleError(ctx, span, err, base.ErrorCode_ERROR_CODE_EXECUTION)
+ }Committable suggestion was skipped due to low confidence.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- internal/storage/postgres/tenantWriter.go (2 hunks)
Additional comments not posted (11)
internal/storage/postgres/tenantWriter.go (11)
6-6: LGTM!The import of the
fmtpackage is appropriate for string formatting.The code changes are approved.
72-75: LGTM!Starting a transaction ensures atomicity for the batch deletion process.
The code changes are approved.
76-76: LGTM!Deferring the rollback ensures that the transaction is rolled back if any error occurs.
The code changes are approved.
78-85: LGTM!Preparing batch operations for deleting tenant-related records from multiple tables improves efficiency and ensures that all deletions are executed in a single operation.
The code changes are approved.
86-87: LGTM!Adding the tenant deletion query to the batch ensures that the tenant is deleted after all related records are deleted.
The code changes are approved.
88-90: LGTM!Executing the batch of delete queries ensures that all deletions are performed in a single operation.
The code changes are approved.
91-106: LGTM!Iterating through the batch results ensures that any errors are caught and the number of affected rows is counted.
The code changes are approved.
108-111: LGTM!Retrieving the tenant details after deletion ensures that the tenant's information is returned after the deletion process.
The code changes are approved.
113-119: LGTM!Handling errors when retrieving tenant details ensures that the system can gracefully manage cases where the tenant does not exist.
The code changes are approved.
121-123: LGTM!Closing the batch result ensures that resources are properly released.
The code changes are approved.
126-129: LGTM!Committing the transaction ensures that all deletions are finalized.
The code changes are approved.
Summary by CodeRabbit
New Features
Bug Fixes