[PATCH] models: Add State.slug field

Andrew Donnellan ajd at linux.ibm.com
Tue Dec 10 15:43:19 AEDT 2019


On 9/12/19 3:00 am, Stephen Finucane wrote:
> --- /dev/null
> +++ b/patchwork/migrations/0038_state_slug.py
> @@ -0,0 +1,68 @@
> +# -*- coding: utf-8 -*-
> +
> +from __future__ import unicode_literals
> +
> +from django.db import migrations, models, transaction
> +from django.utils.text import slugify
> +
> +
> +def validate_uniqueness(apps, schema_editor):
> +    """Ensure all State.name entries are unique.
> +
> +    We need to do this before enforcing a uniqueness constraint.
> +    """
> +
> +    State = apps.get_model('patchwork', 'State')
> +
> +    unique_count = len(State.objects.order_by().values_list(
> +        'name', flat=True).distinct())
> +    total_count = State.objects.count()
> +
> +    if unique_count != total_count:
> +        raise Exception(
> +            'You have non-unique States entries that need to be combined '
> +            'before you can run this migration. This migration must be done '
> +            'by hand. If you need assistance, please contact '
> +            'patchwork at ozlabs.org')

I think the migration can still fail without printing a helpful 
exception message if two states have names that are different but still 
slugify to the same thing?

-- 
Andrew Donnellan              OzLabs, ADL Canberra
ajd at linux.ibm.com             IBM Australia Limited



More information about the Patchwork mailing list