From 263ef4449f52e005da47ccb4bb411b9b8cc79f7a Mon Sep 17 00:00:00 2001
From: Christophe Henry <contact@c-henry.fr>
Date: Tue, 3 Dec 2019 11:46:21 +0100
Subject: [PATCH] bugfix: Prevent automatic source creation be performed if DB
 was not migrated yet (startinblox/djangoldp-packages/djangoldp#187)

---
 djangoldp/apps.py | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/djangoldp/apps.py b/djangoldp/apps.py
index 8811cf84..8d6e4569 100644
--- a/djangoldp/apps.py
+++ b/djangoldp/apps.py
@@ -1,6 +1,5 @@
 from django.apps import AppConfig
-from django.db import OperationalError
-
+from django.db import OperationalError, connection
 
 class DjangoldpConfig(AppConfig):
     name = 'djangoldp'
@@ -11,7 +10,17 @@ class DjangoldpConfig(AppConfig):
     def create_local_source(self):
         from djangoldp.models import LDPSource, Model
 
-        model_classes = {cls.__name__: cls for cls in Model.__subclasses__()}
+        model_classes = {}
+        db_tables = []
+
+        for cls in Model.__subclasses__():
+            model_classes[cls.__name__] = cls
+            db_tables.append(LDPSource.get_meta(cls, "db_table"))
+
+        # Check that all model's table already exists
+        existing_tables = connection.introspection.table_names()
+        if not all(db_table in existing_tables for db_table in db_tables):
+            return
 
         for class_name in model_classes:
             model_class = model_classes[class_name]
@@ -19,7 +28,7 @@ class DjangoldpConfig(AppConfig):
                 continue
             path = model_class.get_container_path().strip("/")
             try:
-                existing_source = LDPSource.objects.get(federation=path)
+                LDPSource.objects.get(federation=path)
             except LDPSource.DoesNotExist:
                 LDPSource.objects.create(federation=path, urlid=Model.absolute_url(model_class))
             except OperationalError:
-- 
GitLab