From 02006270cd725a670b6d4f42326695087d322776 Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Mon, 1 Oct 2018 19:14:09 +0200
Subject: [PATCH] See #542: fixed issue with empty actor during upgrade script

---
 .../common/scripts/migrate_to_user_libraries.py        | 10 +++++++++-
 api/funkwhale_api/users/models.py                      |  2 +-
 docs/upgrading/0.17.rst                                |  4 ++++
 3 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/api/funkwhale_api/common/scripts/migrate_to_user_libraries.py b/api/funkwhale_api/common/scripts/migrate_to_user_libraries.py
index 54ba5d50..537794b6 100644
--- a/api/funkwhale_api/common/scripts/migrate_to_user_libraries.py
+++ b/api/funkwhale_api/common/scripts/migrate_to_user_libraries.py
@@ -71,7 +71,15 @@ def update_uploads(libraries_by_user, stdout):
 
 def update_orphan_uploads(open_api, stdout):
     privacy_level = "everyone" if open_api else "instance"
-    first_superuser = User.objects.filter(is_superuser=True).order_by("pk").first()
+    first_superuser = (
+        User.objects.filter(is_superuser=True)
+        .exclude(actor=None)
+        .order_by("pk")
+        .first()
+    )
+    if not first_superuser:
+        stdout.write("* No superuser found, skipping update orphan uploads")
+        return
     library, _ = models.Library.objects.get_or_create(
         name="default",
         actor=first_superuser.actor,
diff --git a/api/funkwhale_api/users/models.py b/api/funkwhale_api/users/models.py
index 197ef0dc..2bc87588 100644
--- a/api/funkwhale_api/users/models.py
+++ b/api/funkwhale_api/users/models.py
@@ -285,7 +285,7 @@ def create_actor(user):
     args["private_key"] = private.decode("utf-8")
     args["public_key"] = public.decode("utf-8")
 
-    return federation_models.Actor.objects.create(**args)
+    return federation_models.Actor.objects.create(user=user, **args)
 
 
 @receiver(ldap_populate_user)
diff --git a/docs/upgrading/0.17.rst b/docs/upgrading/0.17.rst
index 4a7093b1..1414b563 100644
--- a/docs/upgrading/0.17.rst
+++ b/docs/upgrading/0.17.rst
@@ -189,10 +189,14 @@ then run the migrations script.
 
 On docker-setups::
 
+    # if you missed this one from a previous upgrade
+    docker-compose run --rm api python manage.py script create_actors --no-input
     docker-compose run --rm api python manage.py script migrate_to_user_libraries --no-input
 
 On non docker-setups::
 
+    # if you missed this one from a previous upgrade
+    python api/manage.py script create_actors --no-input
     python api/manage.py script migrate_to_user_libraries --no-input
 
 If the scripts ends without errors, you're instance should be updated and ready to use :)
-- 
GitLab