From 89a55460d6ae4954d05c30c60ee75c2f4ab94fc9 Mon Sep 17 00:00:00 2001 From: Alexey Evseev Date: Mon, 28 Nov 2016 15:49:26 +0300 Subject: [PATCH] Support schema definition for redshift In addition to postgres, redshift database is also support schema. Respect it by 'currentSchema' param. Also, we need to not include this param as in in Django's DATABASES settings. Otherwise, following error is raised: ``` django.db.utils.OperationalError: invalid connection option "currentSchema" ``` --- dj_database_url.py | 7 +++++-- test_dj_database_url.py | 7 ++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/dj_database_url.py b/dj_database_url.py index d5c3efc..e269e9e 100644 --- a/dj_database_url.py +++ b/dj_database_url.py @@ -114,8 +114,11 @@ def parse(url, engine=None, conn_max_age=0): options[key] = values[-1] # Support for Postgres Schema URLs - if 'currentSchema' in options and engine == 'django.db.backends.postgresql_psycopg2': - options['options'] = '-c search_path={0}'.format(options['currentSchema']) + if 'currentSchema' in options and engine in ( + 'django.db.backends.postgresql_psycopg2', + 'django_redshift_backend', + ): + options['options'] = '-c search_path={0}'.format(options.pop('currentSchema')) if options: config['OPTIONS'] = options diff --git a/test_dj_database_url.py b/test_dj_database_url.py index 7bba839..0882083 100644 --- a/test_dj_database_url.py +++ b/test_dj_database_url.py @@ -44,7 +44,7 @@ class DatabaseTestSuite(unittest.TestCase): assert url['PASSWORD'] == 'wegauwhgeuioweg' assert url['PORT'] == 5431 assert url['OPTIONS']['options'] == '-c search_path=otherschema' - + assert 'currentSchema' not in url['OPTIONS'] def test_postgres_parsing_with_special_characters(self): url = 'postgres://%23user:%23password@ec2-107-21-253-135.compute-1.amazonaws.com:5431/%23database' @@ -258,7 +258,7 @@ class DatabaseTestSuite(unittest.TestCase): assert url['PORT'] == '' def test_redshift_parsing(self): - url = 'redshift://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5439/d8r82722r2kuvn' + url = 'redshift://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5439/d8r82722r2kuvn?currentSchema=otherschema' url = dj_database_url.parse(url) assert url['ENGINE'] == 'django_redshift_backend' @@ -267,7 +267,8 @@ class DatabaseTestSuite(unittest.TestCase): assert url['USER'] == 'uf07k1i6d8ia0v' assert url['PASSWORD'] == 'wegauwhgeuioweg' assert url['PORT'] == 5439 - + assert url['OPTIONS']['options'] == '-c search_path=otherschema' + assert 'currentSchema' not in url['OPTIONS'] if __name__ == '__main__':