diff mbox

[v3,1/6] Fix failure to start with uninitalised database

Message ID 1470716861-6486-2-git-send-email-dja@axtens.net
State Accepted
Headers show

Commit Message

Daniel Axtens Aug. 9, 2016, 4:27 a.m. UTC
An OptionalModelChoiceField will attempt to query the database to get choices
in its __init__ method. This fails if the database hasn't been initialised
yet. So, put that in a try/catch block. This lets things work through the
migration and loading of data from fixtures.

Reviewed-by: Andrew Donnellan <andrew.donnellan@au1.ibm.com>
Signed-off-by: Daniel Axtens <dja@axtens.net>
---
 patchwork/forms.py | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

Comments

Stephen Finucane Aug. 13, 2016, 11:08 p.m. UTC | #1
On 09 Aug 14:27, Daniel Axtens wrote:
> An OptionalModelChoiceField will attempt to query the database to get choices
> in its __init__ method. This fails if the database hasn't been initialised
> yet. So, put that in a try/catch block. This lets things work through the
> migration and loading of data from fixtures.
> 
> Reviewed-by: Andrew Donnellan <andrew.donnellan@au1.ibm.com>
> Signed-off-by: Daniel Axtens <dja@axtens.net>

Thanks for taking the time to fix this.

Reviewed-by: Stephen Finucane <stephenfinucane@hotmail.com>
diff mbox

Patch

diff --git a/patchwork/forms.py b/patchwork/forms.py
index 3f876b7d6541..1897093cee8a 100644
--- a/patchwork/forms.py
+++ b/patchwork/forms.py
@@ -21,6 +21,7 @@  from __future__ import absolute_import
 
 from django.contrib.auth.models import User
 from django import forms
+from django.db.utils import ProgrammingError
 
 from patchwork.models import Patch, State, Bundle, UserProfile
 
@@ -165,8 +166,14 @@  class OptionalModelChoiceField(forms.ModelChoiceField):
             __init__(initial=self.no_change_choice[0], *args, **kwargs)
 
     def _get_choices(self):
-        choices = list(
-            super(OptionalModelChoiceField, self)._get_choices())
+        # _get_choices queries the database, which can fail if the db
+        # hasn't been initialised yet. catch that and give an empty
+        # set of choices for now.
+        try:
+            choices = list(
+                super(OptionalModelChoiceField, self)._get_choices())
+        except ProgrammingError:
+            choices = []
         choices.append(self.no_change_choice)
         return choices