-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: Create a composite unique key to remove duplicate data #4203
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
|
Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
| name='workspaceuserresourcepermission', | ||
| unique_together={('workspace_id', 'user', 'auth_target_type', 'target')}, | ||
| ), | ||
| ] |
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.
The provided code checks for duplicate entries in the WorkspaceUserResourcePermission model based on specific fields: workspace_id, user, auth_target_type, and target. Then deletes all but one occurrence of each duplicate. Here are some points to consider:
-
Duplicate Check: The query uses the
RowNumber()function fromdjango.db.models.functions.RowNumber(). This is correct for filtering out duplicates. -
Field Partitioning: The partitioning criteria are set by
F('workspace_id'),F('user'),F('auth_target_type'), andF('target'), which ensures that duplicates within these groups are identified. -
Order By and Filter: The ordering is done by
_time.desc()to ensure the latest entry remains, while duplicates with earlier creation times are filtered out. -
Run Python Operation: The
migrations.RunPythonoperation correctly calls theremove_duplicatesfunction during migration. -
Alter Unique Together Constraint: After removing duplicates, the unique-together constraint is added back with the same fields.
-
No Optimization Suggestions: Based on the current implementation, there aren't many areas where optimization can be applied directly. However, consider creating an index on the combination of
workspace_user_resource_permission_model._meta.fieldsto improve performance if the table grows significantly.
Overall, the code appears well-structured and should work as intended for deduplicating entries according to specified rules.
|
|
||
| class Meta: | ||
| db_table = "workspace_user_resource_permission" | ||
| unique_together = ('workspace_id', 'user', 'auth_target_type', 'target') |
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.
The provided code snippet is mostly correct for a Django model to manage permissions related to workspace user resources. However, there are a few suggestions for improvement:
-
Docstring: It's a good practice to add a docstring explaining the purpose of each attribute.
-
Meta.ordering (Optional): If you want certain orderings, you can specify them here without needing a separate
order_byqueryset method in views or elsewhere.
Here's improved version with the suggested changes:
from django.db import models
class WorkspaceUserResourcePermission(models.Model):
# Attributes
workspace = models.ForeignKey('Workspaces', on_delete=models.CASCADE)
user = models.ForeignKey('WorkspaceUsers', on_delete=models.CASCADE)
auth_target_type = models.CharField(max_length=100) # Assuming this should be specific like 'permission_group'
target = models.CharField(max_length=255)
# Metadata
class Meta:
db_table = "workspace_user_resource_permission"
unique_together = ('workspace_id', 'user', 'auth_target_type', 'target')
# Optional ordering
# ordering = ['-created_at'] # For example, sort by creation time
def __str__(self):
return f"{self.user} {self.auth_target_type}.{self.target}"Explanation:
-
Unique Togetherness: Ensures that no two records in the database have the same combination of
workspace_id,user,auth_target_type, andtarget. -
Ordering (Optional): Uncommenting
ordering = ['-created_at']adds an optional option to display these entries in descending order based on their creation date (created_at). This is useful for listing recent permissions or other chronological data. -
Str Representation: The
__str__()method provides a readable string representation of the object, which can improve debugability.
If you plan to include additional fields or methods in your WorkspaceUserResourcePermission model, make sure they align with the intended use case and adhere to best practices from Django's ORM design principles.
feat: Create a composite unique key to remove duplicate data