From ee62f9bf9db8c34f012d3b99889b726451337760 Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Sun, 12 Feb 2017 18:33:12 +0100
Subject: [PATCH] Ensure the migration script of doom will never be run
 again...

---
 funkwhale_api/common/utils.py                 |  2 +-
 .../music/migrations/0011_rename_files.py     | 53 ++++++++++++-------
 funkwhale_api/music/models.py                 | 10 ++--
 requirements/base.txt                         |  1 +
 4 files changed, 42 insertions(+), 24 deletions(-)

diff --git a/funkwhale_api/common/utils.py b/funkwhale_api/common/utils.py
index 66fa8e8..838c15c 100644
--- a/funkwhale_api/common/utils.py
+++ b/funkwhale_api/common/utils.py
@@ -12,7 +12,7 @@ def rename_file(instance, field_name, new_name, allow_missing_file=False):
     except FileNotFoundError:
         if not allow_missing_file:
             raise
-
+        print('Skipped missing file', field.path)
     initial_path = os.path.dirname(field.name)
     field.name = os.path.join(initial_path, new_name_with_extension)
     instance.save()
diff --git a/funkwhale_api/music/migrations/0011_rename_files.py b/funkwhale_api/music/migrations/0011_rename_files.py
index 869575b..1c59535 100644
--- a/funkwhale_api/music/migrations/0011_rename_files.py
+++ b/funkwhale_api/music/migrations/0011_rename_files.py
@@ -5,27 +5,40 @@ import os
 from django.db import migrations, models
 from funkwhale_api.common.utils import rename_file
 
+
 def rename_files(apps, schema_editor):
-    TrackFile = apps.get_model("music", "TrackFile")
-    qs = TrackFile.objects.select_related(
-        'track__album__artist', 'track__artist')
-    for tf in qs:
-        try:
-            new_name = '{} - {} - {}'.format(
-                tf.track.artist.name,
-                tf.track.album.title,
-                tf.track.title,
-            )
-        except AttributeError:
-            new_name = '{} - {}'.format(
-                tf.track.artist.name,
-                tf.track.title,
-            )
-        rename_file(
-            instance=tf,
-            field_name='audio_file',
-            allow_missing_file=True,
-            new_name=new_name)
+    """
+    This migration script is utterly broken and made me redownload all my audio files.
+    So next time -> Write some actual tests before running a migration script
+    on thousand of tracks...
+    """
+    return
+    # TrackFile = apps.get_model("music", "TrackFile")
+    # qs = TrackFile.objects.select_related(
+    #     'track__album__artist', 'track__artist')
+    # total = len(qs)
+    #
+    #
+    # for i, tf in enumerate(qs):
+    #     try:
+    #         new_name = '{} - {} - {}'.format(
+    #             tf.track.artist.name,
+    #             tf.track.album.title,
+    #             tf.track.title,
+    #         )
+    #     except AttributeError:
+    #         new_name = '{} - {}'.format(
+    #             tf.track.artist.name,
+    #             tf.track.title,
+    #         )
+    #     rename_file(
+    #         instance=tf,
+    #         field_name='audio_file',
+    #         allow_missing_file=True,
+    #         new_name=new_name)
+    #     print('Renamed file {}/{} (new name: {})'.format(
+    #         i + 1, total, tf.audio_file.name
+    #     ))
 
 
 def rewind(apps, schema_editor):
diff --git a/funkwhale_api/music/models.py b/funkwhale_api/music/models.py
index 4fa7717..8a75be4 100644
--- a/funkwhale_api/music/models.py
+++ b/funkwhale_api/music/models.py
@@ -387,12 +387,16 @@ class ImportJob(models.Model):
     status = models.CharField(choices=STATUS_CHOICES, default='pending', max_length=30)
 
     @celery.app.task(name='ImportJob.run', filter=celery.task_method)
-    def run(self):
+    def run(self, replace=False):
         try:
             track, created = Track.get_or_create_from_api(mbid=self.mbid)
-            if track.files.count() > 0:
+            track_file = None
+            if replace:
+                track_file = track.files.first()
+            elif track.files.count() > 0:
                 return
-            track_file = TrackFile(track=track, source=self.source)
+
+            track_file = track_file or TrackFile(track=track, source=self.source)
             track_file.download_file()
             track_file.save()
             self.status = 'finished'
diff --git a/requirements/base.txt b/requirements/base.txt
index 659d0e4..2599948 100644
--- a/requirements/base.txt
+++ b/requirements/base.txt
@@ -57,3 +57,4 @@ django-cachalot
 django-rest-auth
 beautifulsoup4
 markdown
+ipython
-- 
GitLab