From 33972f1f40d75f676cc0fc756bb1bce90fec0a76 Mon Sep 17 00:00:00 2001 From: Eliot Berriot Date: Sat, 7 Apr 2018 17:09:28 +0200 Subject: [PATCH] Fixed broken uuid migration --- .../migrations/0023_auto_20180407_1010.py | 17 ++-- .../music/migrations/0024_populate_uuid.py | 80 +++++++++++++++++++ 2 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 api/funkwhale_api/music/migrations/0024_populate_uuid.py diff --git a/api/funkwhale_api/music/migrations/0023_auto_20180407_1010.py b/api/funkwhale_api/music/migrations/0023_auto_20180407_1010.py index 0539d90f6..ed7404ac4 100644 --- a/api/funkwhale_api/music/migrations/0023_auto_20180407_1010.py +++ b/api/funkwhale_api/music/migrations/0023_auto_20180407_1010.py @@ -4,7 +4,6 @@ from django.conf import settings from django.db import migrations, models import django.db.models.deletion import django.utils.timezone -import uuid class Migration(migrations.Migration): @@ -18,17 +17,17 @@ class Migration(migrations.Migration): migrations.AddField( model_name='album', name='uuid', - field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True), + field=models.UUIDField(db_index=True, null=True, unique=True), ), migrations.AddField( model_name='artist', name='uuid', - field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True), + field=models.UUIDField(db_index=True, null=True, unique=True), ), migrations.AddField( model_name='importbatch', name='uuid', - field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True), + field=models.UUIDField(db_index=True, null=True, unique=True), ), migrations.AddField( model_name='importjob', @@ -38,17 +37,17 @@ class Migration(migrations.Migration): migrations.AddField( model_name='importjob', name='uuid', - field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True), + field=models.UUIDField(db_index=True, null=True, unique=True), ), migrations.AddField( model_name='lyrics', name='uuid', - field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True), + field=models.UUIDField(db_index=True, null=True, unique=True), ), migrations.AddField( model_name='track', name='uuid', - field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True), + field=models.UUIDField(db_index=True, null=True, unique=True), ), migrations.AddField( model_name='trackfile', @@ -68,12 +67,12 @@ class Migration(migrations.Migration): migrations.AddField( model_name='trackfile', name='uuid', - field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True), + field=models.UUIDField(db_index=True, null=True, unique=True), ), migrations.AddField( model_name='work', name='uuid', - field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True), + field=models.UUIDField(db_index=True, null=True, unique=True), ), migrations.AlterField( model_name='importbatch', diff --git a/api/funkwhale_api/music/migrations/0024_populate_uuid.py b/api/funkwhale_api/music/migrations/0024_populate_uuid.py new file mode 100644 index 000000000..10c78a3db --- /dev/null +++ b/api/funkwhale_api/music/migrations/0024_populate_uuid.py @@ -0,0 +1,80 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals +import os +import uuid +from django.db import migrations, models + + +def populate_uuids(apps, schema_editor): + models = [ + 'Album', + 'Artist', + 'Importbatch', + 'Importjob', + 'Lyrics', + 'Track', + 'Trackfile', + 'Work', + ] + for m in models: + kls = apps.get_model('music', m) + qs = kls.objects.filter(uuid__isnull=True).only('id') + print('Setting uuids for {} ({} objects)'.format(m, len(qs))) + for o in qs: + o.uuid = uuid.uuid4() + o.save(update_fields=['uuid']) + + +def rewind(apps, schema_editor): + pass + + +class Migration(migrations.Migration): + + dependencies = [ + ('music', '0023_auto_20180407_1010'), + ] + + operations = [ + migrations.RunPython(populate_uuids, rewind), + migrations.AlterField( + model_name='album', + name='uuid', + field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True), + ), + migrations.AlterField( + model_name='artist', + name='uuid', + field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True), + ), + migrations.AlterField( + model_name='importbatch', + name='uuid', + field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True), + ), + migrations.AlterField( + model_name='importjob', + name='uuid', + field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True), + ), + migrations.AlterField( + model_name='lyrics', + name='uuid', + field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True), + ), + migrations.AlterField( + model_name='track', + name='uuid', + field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True), + ), + migrations.AlterField( + model_name='trackfile', + name='uuid', + field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True), + ), + migrations.AlterField( + model_name='work', + name='uuid', + field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True), + ), + ]