|
| 1 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 2 | +# http://nexb.com and https://github.com/nexB/vulnerablecode/ |
| 3 | +# The VulnerableCode software is licensed under the Apache License version 2.0. |
| 4 | +# Data generated with VulnerableCode require an acknowledgment. |
| 5 | +# |
| 6 | +# You may not use this software except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 |
| 8 | +# Unless required by applicable law or agreed to in writing, software distributed |
| 9 | +# under the License is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES OR |
| 10 | +# CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | +# specific language governing permissions and limitations under the License. |
| 12 | +# |
| 13 | +# When you publish or redistribute any data created with VulnerableCode or any VulnerableCode |
| 14 | +# derivative work, you must accompany this data with the following acknowledgment: |
| 15 | +# |
| 16 | +# Generated with VulnerableCode and provided on an 'AS IS' BASIS, WITHOUT WARRANTIES |
| 17 | +# OR CONDITIONS OF ANY KIND, either express or implied. No content created from |
| 18 | +# VulnerableCode should be considered or used as legal advice. Consult an Attorney |
| 19 | +# for any legal advice. |
| 20 | +# VulnerableCode is a free software code scanning tool from nexB Inc. and others. |
| 21 | +# Visit https://github.com/nexB/vulnerablecode/ for support and download. |
| 22 | + |
| 23 | +from django.db import migrations |
| 24 | +from django.db.models import Count |
| 25 | +from django.db.models import Max |
| 26 | + |
| 27 | + |
| 28 | +class Migration(migrations.Migration): |
| 29 | + |
| 30 | + dependencies = [ |
| 31 | + ("vulnerabilities", "0013_auto_20220503_0941"), |
| 32 | + ] |
| 33 | + |
| 34 | + def remove_duplicate_rows(apps, schema_editor): |
| 35 | + """ |
| 36 | + Find all duplicate rows and remove all of them except the latest one. |
| 37 | + """ |
| 38 | + unique_fields = [ |
| 39 | + "reference", |
| 40 | + "scoring_system", |
| 41 | + "value", |
| 42 | + ] |
| 43 | + Severities = apps.get_model("vulnerabilities", "VulnerabilitySeverity") |
| 44 | + # Get all duplicates according to the unique_fields |
| 45 | + duplicates = ( |
| 46 | + Severities.objects.values(*unique_fields) |
| 47 | + .order_by() |
| 48 | + .annotate(max_id=Max("id"), count_id=Count("id")) |
| 49 | + .filter(count_id__gt=1) |
| 50 | + ) |
| 51 | + for duplicate in duplicates: |
| 52 | + unique_fields_data = { |
| 53 | + uniqe_field: duplicate[uniqe_field] for uniqe_field in unique_fields |
| 54 | + } |
| 55 | + # Get all rows with the same unique_fields_data |
| 56 | + # exclude the latest one |
| 57 | + # and delete rest of them |
| 58 | + ( |
| 59 | + Severities.objects.filter(**unique_fields_data) |
| 60 | + .exclude(id=duplicate["max_id"]) |
| 61 | + .delete() |
| 62 | + ) |
| 63 | + |
| 64 | + operations = [migrations.RunPython(remove_duplicate_rows)] |
0 commit comments