Skip to content

Conversation

@shaohuzhang1
Copy link
Contributor

feat: Create a composite unique key to remove duplicate data

@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Oct 16, 2025

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.

Details

Instructions 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.

@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Oct 16, 2025

[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.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

name='workspaceuserresourcepermission',
unique_together={('workspace_id', 'user', 'auth_target_type', 'target')},
),
]
Copy link
Contributor Author

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:

  1. Duplicate Check: The query uses the RowNumber() function from django.db.models.functions.RowNumber(). This is correct for filtering out duplicates.

  2. Field Partitioning: The partitioning criteria are set by F('workspace_id'), F('user'), F('auth_target_type'), and F('target'), which ensures that duplicates within these groups are identified.

  3. 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.

  4. Run Python Operation: The migrations.RunPython operation correctly calls the remove_duplicates function during migration.

  5. Alter Unique Together Constraint: After removing duplicates, the unique-together constraint is added back with the same fields.

  6. 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.fields to 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')
Copy link
Contributor Author

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:

  1. Docstring: It's a good practice to add a docstring explaining the purpose of each attribute.

  2. Meta.ordering (Optional): If you want certain orderings, you can specify them here without needing a separate order_by queryset 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:

  1. Unique Togetherness: Ensures that no two records in the database have the same combination of workspace_id, user, auth_target_type, and target.

  2. 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.

  3. 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.

@zhanweizhang7 zhanweizhang7 merged commit da74718 into v2 Oct 16, 2025
4 of 6 checks passed
@zhanweizhang7 zhanweizhang7 deleted the pr@v2@feat_create_composite_unique_key branch October 16, 2025 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants