feat: Create a composite unique key to remove duplicate data
parent
5fdb6dc34b
commit
da74718b67
|
|
@ -0,0 +1,37 @@
|
||||||
|
# Generated by Django 5.2.7 on 2025-10-16 03:21
|
||||||
|
|
||||||
|
from django.db import migrations
|
||||||
|
from django.db.models.functions import RowNumber
|
||||||
|
|
||||||
|
|
||||||
|
def remove_duplicates(apps, schema_editor):
|
||||||
|
from django.db.models import Window, F
|
||||||
|
workspace_user_resource_permission_model = apps.get_model('system_manage', 'WorkspaceUserResourcePermission')
|
||||||
|
|
||||||
|
duplicates = workspace_user_resource_permission_model.objects.annotate(
|
||||||
|
row_num=Window(
|
||||||
|
expression=RowNumber(),
|
||||||
|
partition_by=[F('workspace_id'), F('user'), F('auth_target_type'), F('target')],
|
||||||
|
order_by=[F('create_time').desc()],
|
||||||
|
)
|
||||||
|
).filter(row_num__gt=1)
|
||||||
|
|
||||||
|
ids_to_delete = list(duplicates.values_list('id', flat=True))
|
||||||
|
if ids_to_delete:
|
||||||
|
workspace_user_resource_permission_model.objects.filter(id__in=ids_to_delete).delete()
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('system_manage', '0003_alter_workspaceuserresourcepermission_target'),
|
||||||
|
('users', '0001_initial'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RunPython(remove_duplicates,
|
||||||
|
),
|
||||||
|
migrations.AlterUniqueTogether(
|
||||||
|
name='workspaceuserresourcepermission',
|
||||||
|
unique_together={('workspace_id', 'user', 'auth_target_type', 'target')},
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
@ -57,3 +57,4 @@ class WorkspaceUserResourcePermission(models.Model):
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
db_table = "workspace_user_resource_permission"
|
db_table = "workspace_user_resource_permission"
|
||||||
|
unique_together = ('workspace_id', 'user', 'auth_target_type', 'target')
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,8 @@ FROM (
|
||||||
user_id,
|
user_id,
|
||||||
workspace_id,
|
workspace_id,
|
||||||
icon,
|
icon,
|
||||||
folder_id
|
folder_id,
|
||||||
|
create_time
|
||||||
FROM application
|
FROM application
|
||||||
${query_set}
|
${query_set}
|
||||||
UNION
|
UNION
|
||||||
|
|
@ -22,7 +23,8 @@ FROM (
|
||||||
application_folder."user_id",
|
application_folder."user_id",
|
||||||
application_folder."workspace_id",
|
application_folder."workspace_id",
|
||||||
NULL AS "icon",
|
NULL AS "icon",
|
||||||
application_folder."parent_id" AS "folder_id"
|
application_folder."parent_id" AS "folder_id",
|
||||||
|
application_folder."create_time"
|
||||||
FROM application_folder
|
FROM application_folder
|
||||||
${folder_query_set}
|
${folder_query_set}
|
||||||
) resource_or_folder
|
) resource_or_folder
|
||||||
|
|
@ -42,3 +44,4 @@ LEFT JOIN (
|
||||||
) wurp
|
) wurp
|
||||||
ON wurp.target::text = resource_or_folder.id
|
ON wurp.target::text = resource_or_folder.id
|
||||||
${resource_query_set}
|
${resource_query_set}
|
||||||
|
ORDER BY resource_or_folder.create_time DESC
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,8 @@ FROM (
|
||||||
user_id,
|
user_id,
|
||||||
workspace_id,
|
workspace_id,
|
||||||
"type"::varchar AS "icon",
|
"type"::varchar AS "icon",
|
||||||
folder_id
|
folder_id,
|
||||||
|
create_time
|
||||||
FROM knowledge
|
FROM knowledge
|
||||||
${query_set}
|
${query_set}
|
||||||
UNION
|
UNION
|
||||||
|
|
@ -23,7 +24,8 @@ FROM (
|
||||||
knowledge_folder."user_id",
|
knowledge_folder."user_id",
|
||||||
knowledge_folder."workspace_id",
|
knowledge_folder."workspace_id",
|
||||||
NULL AS "icon",
|
NULL AS "icon",
|
||||||
knowledge_folder."parent_id" AS "folder_id"
|
knowledge_folder."parent_id" AS "folder_id",
|
||||||
|
knowledge_folder."create_time"
|
||||||
FROM knowledge_folder
|
FROM knowledge_folder
|
||||||
${folder_query_set}
|
${folder_query_set}
|
||||||
) resource_or_folder
|
) resource_or_folder
|
||||||
|
|
@ -44,4 +46,5 @@ LEFT JOIN (
|
||||||
${workspace_user_resource_permission_query_set}
|
${workspace_user_resource_permission_query_set}
|
||||||
) wurp
|
) wurp
|
||||||
ON wurp.target::text = resource_or_folder.id
|
ON wurp.target::text = resource_or_folder.id
|
||||||
${resource_query_set}
|
${resource_query_set}
|
||||||
|
ORDER BY resource_or_folder.create_time DESC
|
||||||
|
|
@ -14,7 +14,8 @@ FROM (
|
||||||
user_id,
|
user_id,
|
||||||
workspace_id,
|
workspace_id,
|
||||||
provider as icon,
|
provider as icon,
|
||||||
'default' as folder_id
|
'default' as folder_id,
|
||||||
|
create_time
|
||||||
FROM
|
FROM
|
||||||
model
|
model
|
||||||
${query_set}
|
${query_set}
|
||||||
|
|
@ -27,7 +28,8 @@ FROM (
|
||||||
user_id,
|
user_id,
|
||||||
workspace_id,
|
workspace_id,
|
||||||
provider as icon,
|
provider as icon,
|
||||||
'default' as folder_id
|
'default' as folder_id,
|
||||||
|
create_time
|
||||||
FROM model
|
FROM model
|
||||||
${folder_query_set}
|
${folder_query_set}
|
||||||
AND 1=0
|
AND 1=0
|
||||||
|
|
@ -49,4 +51,5 @@ LEFT JOIN (
|
||||||
${workspace_user_resource_permission_query_set}
|
${workspace_user_resource_permission_query_set}
|
||||||
) wurp
|
) wurp
|
||||||
ON wurp.target = resource_or_folder."id"
|
ON wurp.target = resource_or_folder."id"
|
||||||
${resource_query_set}
|
${resource_query_set}
|
||||||
|
ORDER BY resource_or_folder.create_time DESC
|
||||||
|
|
@ -12,7 +12,8 @@ FROM (
|
||||||
workspace_id,
|
workspace_id,
|
||||||
icon,
|
icon,
|
||||||
folder_id,
|
folder_id,
|
||||||
tool_type
|
tool_type,
|
||||||
|
create_time
|
||||||
FROM tool
|
FROM tool
|
||||||
${query_set}
|
${query_set}
|
||||||
UNION
|
UNION
|
||||||
|
|
@ -24,7 +25,8 @@ FROM (
|
||||||
tool_folder."workspace_id",
|
tool_folder."workspace_id",
|
||||||
NULL AS "icon",
|
NULL AS "icon",
|
||||||
tool_folder."parent_id" AS "folder_id",
|
tool_folder."parent_id" AS "folder_id",
|
||||||
NULL AS "tool_type"
|
NULL AS "tool_type",
|
||||||
|
tool_folder."create_time"
|
||||||
FROM tool_folder
|
FROM tool_folder
|
||||||
${folder_query_set}
|
${folder_query_set}
|
||||||
) resource_or_folder
|
) resource_or_folder
|
||||||
|
|
@ -45,4 +47,5 @@ LEFT JOIN (
|
||||||
) wurp
|
) wurp
|
||||||
ON wurp.target::text = resource_or_folder."id"
|
ON wurp.target::text = resource_or_folder."id"
|
||||||
${resource_query_set}
|
${resource_query_set}
|
||||||
|
ORDER BY resource_or_folder.create_time DESC
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue