Repository: netbox-community/netbox

Issue:
v2.2.0 fails to run; returns ImproperlyConfigured
### Issue type
[ ] Feature request <!-- Requesting the implementation of a new feature -->
[x] Bug report      <!-- Reporting unexpected or erroneous behavior -->
[ ] Documentation   <!-- Proposing a modification to the documentation -->


### Environment
* Python version:  3.4.3
* NetBox version:  2.2.0


### Description
The NetBox WSGI service fails to start, returning the following error:

```
ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
```

This issue has been traced to code intended to verify the PostgreSQL database version during initialization. However, the required `settings` variable is not available at the time of execution.

This issues does not affect the development server (`manage.py runserver`).


Discussion:

Resolving patch:
diff --git a/netbox/extras/migrations/0008_reports.py b/netbox/extras/migrations/0008_reports.py
--- a/netbox/extras/migrations/0008_reports.py
+++ b/netbox/extras/migrations/0008_reports.py
@@ -1,11 +1,30 @@
 # -*- coding: utf-8 -*-
 # Generated by Django 1.11.4 on 2017-09-26 21:25
 from __future__ import unicode_literals
+from distutils.version import StrictVersion
 
 from django.conf import settings
 import django.contrib.postgres.fields.jsonb
-from django.db import migrations, models
+from django.db import connection, migrations, models
 import django.db.models.deletion
+from django.db.utils import OperationalError
+
+
+def verify_postgresql_version(apps, schema_editor):
+    """
+    Verify that PostgreSQL is version 9.4 or higher.
+    """
+    try:
+        with connection.cursor() as cursor:
+            cursor.execute("SELECT VERSION()")
+            row = cursor.fetchone()
+            pg_version = row[0].split()[1]
+            if StrictVersion(pg_version) < StrictVersion('9.4.0'):
+                raise Exception("PostgreSQL 9.4.0 or higher is required ({} found). Upgrade PostgreSQL and then run migrations again.".format(pg_version))
+
+    # Skip if the database is missing (e.g. for CI testing) or misconfigured.
+    except OperationalError:
+        pass
 
 
 class Migration(migrations.Migration):
@@ -16,6 +35,7 @@ class Migration(migrations.Migration):
     ]
 
     operations = [
+        migrations.RunPython(verify_postgresql_version),
         migrations.CreateModel(
             name='ReportResult',
             fields=[
diff --git a/netbox/netbox/__init__.py b/netbox/netbox/__init__.py
--- a/netbox/netbox/__init__.py
+++ b/netbox/netbox/__init__.py
@@ -1,18 +0,0 @@
-from distutils.version import StrictVersion
-
-from django.db import connection
-from django.db.utils import OperationalError
-
-
-# NetBox v2.2 and later requires PostgreSQL 9.4 or higher.
-try:
-    with connection.cursor() as cursor:
-        cursor.execute("SELECT VERSION()")
-        row = cursor.fetchone()
-        pg_version = row[0].split()[1]
-        if StrictVersion(pg_version) < StrictVersion('9.4.0'):
-            raise Exception("PostgreSQL 9.4.0 or higher is required. ({} found)".format(pg_version))
-
-# Skip if the database is missing (e.g. for CI testing) or misconfigured.
-except OperationalError:
-    pass
diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py
--- a/netbox/netbox/settings.py
+++ b/netbox/netbox/settings.py
@@ -13,7 +13,7 @@
     )
 
 
-VERSION = '2.2.0'
+VERSION = '2.2.1'
 
 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 


Test patch: