Add heroku compatibility

Recognize heroku with the environment variable DATABASE_URL, parse it and replace the others based on it.
This commit is contained in:
Thomas Letsch Groch
2020-06-01 18:41:39 -03:00
parent 1b344fb90c
commit b234ef2917
6 changed files with 173 additions and 2 deletions

View File

@@ -9,6 +9,9 @@ https://docs.djangoproject.com/en/2.2/ref/settings/
"""
import os
# import module sys to get the type of exception
import sys
import urllib.parse as urlparse
# Messages
from django.contrib.messages import constants as messages
@@ -112,6 +115,29 @@ else:
}
}
# Solution to removal of Heroku DB Injection
if 'DATABASE_URL' in os.environ:
if 'DATABASES' not in locals():
DATABASES = {}
url = urlparse.urlparse(os.environ['DATABASE_URL'])
# Ensure default database exists.
DATABASES['default'] = DATABASES.get('default', {})
# Update with environment configuration.
DATABASES['default'].update({
'NAME': url.path[1:],
'USER': url.username,
'PASSWORD': url.password,
'HOST': url.hostname,
'PORT': url.port,
})
if url.scheme == 'postgres':
DATABASES['default']['ENGINE'] = 'django.db.backends.postgresql_psycopg2'
if url.scheme == 'mysql':
DATABASES['default']['ENGINE'] = 'django.db.backends.mysql'
# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators