From 4d09e752628cba24befb6f8d1b20b56760aa031d Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Sat, 16 Dec 2017 00:36:06 +0100
Subject: [PATCH] Update models with on_cascade

---
 .../favorites/migrations/0001_initial.py      |  4 +-
 api/funkwhale_api/favorites/models.py         |  6 ++-
 .../history/migrations/0001_initial.py        |  4 +-
 api/funkwhale_api/history/models.py           | 10 ++++-
 .../music/migrations/0001_initial.py          | 12 +++---
 .../migrations/0009_auto_20160920_1614.py     |  4 +-
 api/funkwhale_api/music/models.py             | 42 +++++++++++++++----
 .../playlists/migrations/0001_initial.py      |  8 ++--
 api/funkwhale_api/playlists/models.py         | 18 ++++++--
 .../radios/migrations/0001_initial.py         |  6 +--
 .../migrations/0003_auto_20160521_1708.py     |  2 +-
 api/funkwhale_api/radios/models.py            | 19 +++++++--
 12 files changed, 94 insertions(+), 41 deletions(-)

diff --git a/api/funkwhale_api/favorites/migrations/0001_initial.py b/api/funkwhale_api/favorites/migrations/0001_initial.py
index 0a6f0e5f..c2bd0318 100644
--- a/api/funkwhale_api/favorites/migrations/0001_initial.py
+++ b/api/funkwhale_api/favorites/migrations/0001_initial.py
@@ -19,8 +19,8 @@ class Migration(migrations.Migration):
             fields=[
                 ('id', models.AutoField(serialize=False, auto_created=True, verbose_name='ID', primary_key=True)),
                 ('creation_date', models.DateTimeField(default=django.utils.timezone.now)),
-                ('track', models.ForeignKey(related_name='track_favorites', to='music.Track')),
-                ('user', models.ForeignKey(related_name='track_favorites', to=settings.AUTH_USER_MODEL)),
+                ('track', models.ForeignKey(related_name='track_favorites', to='music.Track', on_delete=models.CASCADE)),
+                ('user', models.ForeignKey(related_name='track_favorites', to=settings.AUTH_USER_MODEL, on_delete=models.CASCADE)),
             ],
             options={
                 'ordering': ('-creation_date',),
diff --git a/api/funkwhale_api/favorites/models.py b/api/funkwhale_api/favorites/models.py
index f9c6426e..899ed9cf 100644
--- a/api/funkwhale_api/favorites/models.py
+++ b/api/funkwhale_api/favorites/models.py
@@ -5,8 +5,10 @@ from funkwhale_api.music.models import Track
 
 class TrackFavorite(models.Model):
     creation_date = models.DateTimeField(default=timezone.now)
-    user = models.ForeignKey('users.User', related_name='track_favorites')
-    track = models.ForeignKey(Track, related_name='track_favorites')
+    user = models.ForeignKey(
+        'users.User', related_name='track_favorites', on_delete=models.CASCADE)
+    track = models.ForeignKey(
+        Track, related_name='track_favorites', on_delete=models.CASCADE)
 
     class Meta:
         unique_together = ('track', 'user')
diff --git a/api/funkwhale_api/history/migrations/0001_initial.py b/api/funkwhale_api/history/migrations/0001_initial.py
index 5ddfc26f..7b6f950e 100644
--- a/api/funkwhale_api/history/migrations/0001_initial.py
+++ b/api/funkwhale_api/history/migrations/0001_initial.py
@@ -20,8 +20,8 @@ class Migration(migrations.Migration):
                 ('id', models.AutoField(verbose_name='ID', primary_key=True, serialize=False, auto_created=True)),
                 ('end_date', models.DateTimeField(null=True, blank=True, default=django.utils.timezone.now)),
                 ('session_key', models.CharField(null=True, blank=True, max_length=100)),
-                ('track', models.ForeignKey(related_name='listenings', to='music.Track')),
-                ('user', models.ForeignKey(blank=True, null=True, related_name='listenings', to=settings.AUTH_USER_MODEL)),
+                ('track', models.ForeignKey(related_name='listenings', to='music.Track', on_delete=models.CASCADE)),
+                ('user', models.ForeignKey(blank=True, null=True, related_name='listenings', to=settings.AUTH_USER_MODEL, on_delete=models.CASCADE)),
             ],
             options={
                 'ordering': ('-end_date',),
diff --git a/api/funkwhale_api/history/models.py b/api/funkwhale_api/history/models.py
index 0810ecf8..f7f62de6 100644
--- a/api/funkwhale_api/history/models.py
+++ b/api/funkwhale_api/history/models.py
@@ -7,8 +7,14 @@ from funkwhale_api.music.models import Track
 
 class Listening(models.Model):
     end_date = models.DateTimeField(default=timezone.now, null=True, blank=True)
-    track = models.ForeignKey(Track, related_name="listenings")
-    user = models.ForeignKey('users.User', related_name="listenings", null=True, blank=True)
+    track = models.ForeignKey(
+        Track, related_name="listenings", on_delete=models.CASCADE)
+    user = models.ForeignKey(
+        'users.User',
+        related_name="listenings",
+        null=True,
+        blank=True,
+        on_delete=models.CASCADE)
     session_key = models.CharField(max_length=100, null=True, blank=True)
 
     class Meta:
diff --git a/api/funkwhale_api/music/migrations/0001_initial.py b/api/funkwhale_api/music/migrations/0001_initial.py
index e7864794..265b8157 100644
--- a/api/funkwhale_api/music/migrations/0001_initial.py
+++ b/api/funkwhale_api/music/migrations/0001_initial.py
@@ -44,7 +44,7 @@ class Migration(migrations.Migration):
             fields=[
                 ('id', models.AutoField(primary_key=True, auto_created=True, serialize=False, verbose_name='ID')),
                 ('creation_date', models.DateTimeField(default=django.utils.timezone.now)),
-                ('submitted_by', models.ForeignKey(related_name='imports', to=settings.AUTH_USER_MODEL)),
+                ('submitted_by', models.ForeignKey(related_name='imports', to=settings.AUTH_USER_MODEL, on_delete=models.CASCADE)),
             ],
         ),
         migrations.CreateModel(
@@ -54,7 +54,7 @@ class Migration(migrations.Migration):
                 ('source', models.URLField()),
                 ('mbid', models.UUIDField(editable=False)),
                 ('status', models.CharField(default='pending', choices=[('pending', 'Pending'), ('finished', 'finished')], max_length=30)),
-                ('batch', models.ForeignKey(related_name='jobs', to='music.ImportBatch')),
+                ('batch', models.ForeignKey(related_name='jobs', to='music.ImportBatch', on_delete=models.CASCADE)),
             ],
         ),
         migrations.CreateModel(
@@ -64,8 +64,8 @@ class Migration(migrations.Migration):
                 ('mbid', models.UUIDField(editable=False, blank=True, null=True)),
                 ('creation_date', models.DateTimeField(default=django.utils.timezone.now)),
                 ('title', models.CharField(max_length=255)),
-                ('album', models.ForeignKey(related_name='tracks', blank=True, null=True, to='music.Album')),
-                ('artist', models.ForeignKey(related_name='tracks', to='music.Artist')),
+                ('album', models.ForeignKey(related_name='tracks', blank=True, null=True, to='music.Album', on_delete=models.CASCADE)),
+                ('artist', models.ForeignKey(related_name='tracks', to='music.Artist', on_delete=models.CASCADE)),
             ],
             options={
                 'abstract': False,
@@ -78,12 +78,12 @@ class Migration(migrations.Migration):
                 ('audio_file', models.FileField(upload_to='tracks')),
                 ('source', models.URLField(blank=True, null=True)),
                 ('duration', models.IntegerField(blank=True, null=True)),
-                ('track', models.ForeignKey(related_name='files', to='music.Track')),
+                ('track', models.ForeignKey(related_name='files', to='music.Track', on_delete=models.CASCADE)),
             ],
         ),
         migrations.AddField(
             model_name='album',
             name='artist',
-            field=models.ForeignKey(related_name='albums', to='music.Artist'),
+            field=models.ForeignKey(related_name='albums', to='music.Artist', on_delete=models.CASCADE),
         ),
     ]
diff --git a/api/funkwhale_api/music/migrations/0009_auto_20160920_1614.py b/api/funkwhale_api/music/migrations/0009_auto_20160920_1614.py
index 2046a712..3a3d9398 100644
--- a/api/funkwhale_api/music/migrations/0009_auto_20160920_1614.py
+++ b/api/funkwhale_api/music/migrations/0009_auto_20160920_1614.py
@@ -39,11 +39,11 @@ class Migration(migrations.Migration):
         migrations.AddField(
             model_name='lyrics',
             name='work',
-            field=models.ForeignKey(related_name='lyrics', to='music.Work', blank=True, null=True),
+            field=models.ForeignKey(related_name='lyrics', to='music.Work', blank=True, null=True, on_delete=models.CASCADE),
         ),
         migrations.AddField(
             model_name='track',
             name='work',
-            field=models.ForeignKey(related_name='tracks', to='music.Work', blank=True, null=True),
+            field=models.ForeignKey(related_name='tracks', to='music.Work', blank=True, null=True, on_delete=models.CASCADE),
         ),
     ]
diff --git a/api/funkwhale_api/music/models.py b/api/funkwhale_api/music/models.py
index 95a47fd4..e5c87a60 100644
--- a/api/funkwhale_api/music/models.py
+++ b/api/funkwhale_api/music/models.py
@@ -108,7 +108,8 @@ def import_tracks(instance, cleaned_data, raw_data):
 
 class Album(APIModelMixin):
     title = models.CharField(max_length=255)
-    artist = models.ForeignKey(Artist, related_name='albums')
+    artist = models.ForeignKey(
+        Artist, related_name='albums', on_delete=models.CASCADE)
     release_date = models.DateField(null=True)
     release_group_id = models.UUIDField(null=True, blank=True)
     cover = VersatileImageField(upload_to='albums/covers/%Y/%m/%d', null=True, blank=True)
@@ -245,7 +246,12 @@ class Work(APIModelMixin):
 
 
 class Lyrics(models.Model):
-    work = models.ForeignKey(Work, related_name='lyrics', null=True, blank=True)
+    work = models.ForeignKey(
+        Work,
+        related_name='lyrics',
+        null=True,
+        blank=True,
+        on_delete=models.CASCADE)
     url = models.URLField(unique=True)
     content = models.TextField(null=True, blank=True)
 
@@ -268,10 +274,21 @@ class Lyrics(models.Model):
 
 class Track(APIModelMixin):
     title = models.CharField(max_length=255)
-    artist = models.ForeignKey(Artist, related_name='tracks')
+    artist = models.ForeignKey(
+        Artist, related_name='tracks', on_delete=models.CASCADE)
     position = models.PositiveIntegerField(null=True, blank=True)
-    album = models.ForeignKey(Album, related_name='tracks', null=True, blank=True)
-    work = models.ForeignKey(Work, related_name='tracks', null=True, blank=True)
+    album = models.ForeignKey(
+        Album,
+        related_name='tracks',
+        null=True,
+        blank=True,
+        on_delete=models.CASCADE)
+    work = models.ForeignKey(
+        Work,
+        related_name='tracks',
+        null=True,
+        blank=True,
+        on_delete=models.CASCADE)
 
     musicbrainz_model = 'recording'
     api = musicbrainz.api.recordings
@@ -340,7 +357,8 @@ class Track(APIModelMixin):
 
 
 class TrackFile(models.Model):
-    track = models.ForeignKey(Track, related_name='files')
+    track = models.ForeignKey(
+        Track, related_name='files', on_delete=models.CASCADE)
     audio_file = models.FileField(upload_to='tracks/%Y/%m/%d', max_length=255)
     source = models.URLField(null=True, blank=True)
     duration = models.IntegerField(null=True, blank=True)
@@ -376,7 +394,8 @@ class TrackFile(models.Model):
 
 class ImportBatch(models.Model):
     creation_date = models.DateTimeField(default=timezone.now)
-    submitted_by = models.ForeignKey('users.User', related_name='imports')
+    submitted_by = models.ForeignKey(
+        'users.User', related_name='imports', on_delete=models.CASCADE)
 
     class Meta:
         ordering = ['-creation_date']
@@ -392,9 +411,14 @@ class ImportBatch(models.Model):
         return 'finished'
 
 class ImportJob(models.Model):
-    batch = models.ForeignKey(ImportBatch, related_name='jobs')
+    batch = models.ForeignKey(
+        ImportBatch, related_name='jobs', on_delete=models.CASCADE)
     track_file = models.ForeignKey(
-        TrackFile, related_name='jobs', null=True, blank=True)
+        TrackFile,
+        related_name='jobs',
+        null=True,
+        blank=True,
+        on_delete=models.CASCADE)
     source = models.URLField()
     mbid = models.UUIDField(editable=False)
     STATUS_CHOICES = (
diff --git a/api/funkwhale_api/playlists/migrations/0001_initial.py b/api/funkwhale_api/playlists/migrations/0001_initial.py
index f42ca154..bc97d812 100644
--- a/api/funkwhale_api/playlists/migrations/0001_initial.py
+++ b/api/funkwhale_api/playlists/migrations/0001_initial.py
@@ -22,7 +22,7 @@ class Migration(migrations.Migration):
                 ('name', models.CharField(max_length=50)),
                 ('is_public', models.BooleanField(default=False)),
                 ('creation_date', models.DateTimeField(default=django.utils.timezone.now)),
-                ('user', models.ForeignKey(to=settings.AUTH_USER_MODEL, related_name='playlists')),
+                ('user', models.ForeignKey(to=settings.AUTH_USER_MODEL, related_name='playlists', on_delete=models.CASCADE)),
             ],
         ),
         migrations.CreateModel(
@@ -33,9 +33,9 @@ class Migration(migrations.Migration):
                 ('rght', models.PositiveIntegerField(db_index=True, editable=False)),
                 ('tree_id', models.PositiveIntegerField(db_index=True, editable=False)),
                 ('position', models.PositiveIntegerField(db_index=True, editable=False)),
-                ('playlist', models.ForeignKey(to='playlists.Playlist', related_name='playlist_tracks')),
-                ('previous', mptt.fields.TreeOneToOneField(null=True, to='playlists.PlaylistTrack', related_name='next', blank=True)),
-                ('track', models.ForeignKey(to='music.Track', related_name='playlist_tracks')),
+                ('playlist', models.ForeignKey(to='playlists.Playlist', related_name='playlist_tracks', on_delete=models.CASCADE)),
+                ('previous', mptt.fields.TreeOneToOneField(null=True, to='playlists.PlaylistTrack', related_name='next', blank=True, on_delete=models.CASCADE)),
+                ('track', models.ForeignKey(to='music.Track', related_name='playlist_tracks', on_delete=models.CASCADE)),
             ],
             options={
                 'ordering': ('-playlist', 'position'),
diff --git a/api/funkwhale_api/playlists/models.py b/api/funkwhale_api/playlists/models.py
index 35a30a70..e89dce81 100644
--- a/api/funkwhale_api/playlists/models.py
+++ b/api/funkwhale_api/playlists/models.py
@@ -7,7 +7,8 @@ from mptt.models import MPTTModel, TreeOneToOneField
 class Playlist(models.Model):
     name = models.CharField(max_length=50)
     is_public = models.BooleanField(default=False)
-    user = models.ForeignKey('users.User', related_name="playlists")
+    user = models.ForeignKey(
+        'users.User', related_name="playlists", on_delete=models.CASCADE)
     creation_date = models.DateTimeField(default=timezone.now)
 
     def __str__(self):
@@ -21,9 +22,18 @@ class Playlist(models.Model):
 
 
 class PlaylistTrack(MPTTModel):
-    track = models.ForeignKey('music.Track', related_name='playlist_tracks')
-    previous = TreeOneToOneField('self', blank=True, null=True, related_name='next')
-    playlist = models.ForeignKey(Playlist, related_name='playlist_tracks')
+    track = models.ForeignKey(
+        'music.Track',
+        related_name='playlist_tracks',
+        on_delete=models.CASCADE)
+    previous = TreeOneToOneField(
+        'self',
+        blank=True,
+        null=True,
+        related_name='next',
+        on_delete=models.CASCADE)
+    playlist = models.ForeignKey(
+        Playlist, related_name='playlist_tracks', on_delete=models.CASCADE)
 
     class MPTTMeta:
         level_attr = 'position'
diff --git a/api/funkwhale_api/radios/migrations/0001_initial.py b/api/funkwhale_api/radios/migrations/0001_initial.py
index 9ec25805..46faf749 100644
--- a/api/funkwhale_api/radios/migrations/0001_initial.py
+++ b/api/funkwhale_api/radios/migrations/0001_initial.py
@@ -20,7 +20,7 @@ class Migration(migrations.Migration):
                 ('id', models.AutoField(verbose_name='ID', primary_key=True, serialize=False, auto_created=True)),
                 ('radio_type', models.CharField(max_length=50)),
                 ('creation_date', models.DateTimeField(default=django.utils.timezone.now)),
-                ('user', models.ForeignKey(related_name='radio_sessions', blank=True, to=settings.AUTH_USER_MODEL, null=True)),
+                ('user', models.ForeignKey(related_name='radio_sessions', blank=True, to=settings.AUTH_USER_MODEL, null=True, on_delete=models.CASCADE)),
             ],
         ),
         migrations.CreateModel(
@@ -28,8 +28,8 @@ class Migration(migrations.Migration):
             fields=[
                 ('id', models.AutoField(verbose_name='ID', primary_key=True, serialize=False, auto_created=True)),
                 ('position', models.IntegerField(default=1)),
-                ('session', models.ForeignKey(to='radios.RadioSession', related_name='session_tracks')),
-                ('track', models.ForeignKey(to='music.Track', related_name='radio_session_tracks')),
+                ('session', models.ForeignKey(to='radios.RadioSession', related_name='session_tracks', on_delete=models.CASCADE)),
+                ('track', models.ForeignKey(to='music.Track', related_name='radio_session_tracks', on_delete=models.CASCADE)),
             ],
             options={
                 'ordering': ('session', 'position'),
diff --git a/api/funkwhale_api/radios/migrations/0003_auto_20160521_1708.py b/api/funkwhale_api/radios/migrations/0003_auto_20160521_1708.py
index 4629d68f..7c70abc2 100644
--- a/api/funkwhale_api/radios/migrations/0003_auto_20160521_1708.py
+++ b/api/funkwhale_api/radios/migrations/0003_auto_20160521_1708.py
@@ -15,7 +15,7 @@ class Migration(migrations.Migration):
         migrations.AddField(
             model_name='radiosession',
             name='related_object_content_type',
-            field=models.ForeignKey(null=True, to='contenttypes.ContentType', blank=True),
+            field=models.ForeignKey(null=True, to='contenttypes.ContentType', blank=True, on_delete=models.CASCADE),
         ),
         migrations.AddField(
             model_name='radiosession',
diff --git a/api/funkwhale_api/radios/models.py b/api/funkwhale_api/radios/models.py
index a3a35313..984b34a1 100644
--- a/api/funkwhale_api/radios/models.py
+++ b/api/funkwhale_api/radios/models.py
@@ -7,11 +7,20 @@ from django.contrib.contenttypes.models import ContentType
 from funkwhale_api.music.models import Track
 
 class RadioSession(models.Model):
-    user = models.ForeignKey('users.User', related_name='radio_sessions', null=True, blank=True)
+    user = models.ForeignKey(
+        'users.User',
+        related_name='radio_sessions',
+        null=True,
+        blank=True,
+        on_delete=models.CASCADE)
     session_key = models.CharField(max_length=100, null=True, blank=True)
     radio_type = models.CharField(max_length=50)
     creation_date = models.DateTimeField(default=timezone.now)
-    related_object_content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, blank=True, null=True)
+    related_object_content_type = models.ForeignKey(
+        ContentType,
+        blank=True,
+        null=True,
+        on_delete=models.CASCADE)
     related_object_id = models.PositiveIntegerField(blank=True, null=True)
     related_object = GenericForeignKey('related_object_content_type', 'related_object_id')
 
@@ -43,9 +52,11 @@ class RadioSession(models.Model):
         return registry[self.radio_type](session=self)
 
 class RadioSessionTrack(models.Model):
-    session = models.ForeignKey(RadioSession, related_name='session_tracks')
+    session = models.ForeignKey(
+        RadioSession, related_name='session_tracks', on_delete=models.CASCADE)
     position = models.IntegerField(default=1)
-    track = models.ForeignKey(Track, related_name='radio_session_tracks')
+    track = models.ForeignKey(
+        Track, related_name='radio_session_tracks', on_delete=models.CASCADE)
 
     class Meta:
         ordering = ('session', 'position')
-- 
GitLab