Skip to content

Commit

Permalink
Merge pull request #334 from iflare3g/fix/incorrect-parsing-database-…
Browse files Browse the repository at this point in the history
…url-google-cloud-mysql

Fix incorrect parsing of DATABASES_URL for Google Cloud MySQL
  • Loading branch information
sergeyklay committed Oct 9, 2021
2 parents ef15435 + f216327 commit 0b60e16
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion environ/environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ class Env:
"xapian": "haystack.backends.xapian_backend.XapianEngine",
"simple": "haystack.backends.simple_backend.SimpleEngine",
}
CLOUDSQL = 'cloudsql'

def __init__(self, **scheme):
self.smart_cast = True
Expand Down Expand Up @@ -502,7 +503,10 @@ def db_url_config(cls, url, engine=None):
'PORT': _cast_int(url.port) or '',
})

if url.scheme in cls.POSTGRES_FAMILY and path.startswith('/'):
if (
url.scheme in cls.POSTGRES_FAMILY and path.startswith('/')
or cls.CLOUDSQL in path and path.startswith('/')
):
config['HOST'], config['NAME'] = path.rsplit('/', 1)

if url.scheme == 'oracle' and path == '':
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class FakeEnv:
URL = 'http://www.google.com/'
POSTGRES = 'postgres://uf07k1:wegauwhg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722'
MYSQL = 'mysql://bea6eb0:69772142@us-cdbr-east.cleardb.com/heroku_97681?reconnect=true'
MYSQL_CLOUDSQL_URL = 'mysql://djuser:hidden-password@//cloudsql/arvore-codelab:us-central1:mysqlinstance/mydatabase'
MYSQLGIS = 'mysqlgis://user:password@127.0.0.1/some_database'
SQLITE = 'sqlite:////full/path/to/your/database/file.sqlite'
ORACLE_TNS = 'oracle://user:password@sid/'
Expand Down Expand Up @@ -67,6 +68,7 @@ def generate_data(cls):
DATABASE_ORACLE_TNS_URL=cls.ORACLE_TNS,
DATABASE_REDSHIFT_URL=cls.REDSHIFT,
DATABASE_CUSTOM_BACKEND_URL=cls.CUSTOM_BACKEND,
DATABASE_MYSQL_CLOUDSQL_URL=cls.MYSQL_CLOUDSQL_URL,
CACHE_URL=cls.MEMCACHE,
CACHE_REDIS=cls.REDIS,
EMAIL_URL=cls.EMAIL,
Expand Down
3 changes: 3 additions & 0 deletions tests/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ def test_url_encoded_parts(self):
'/full/path/to/your/database/file.sqlite', '', '', '', ''),
('DATABASE_CUSTOM_BACKEND_URL', 'custom.backend', 'database',
'example.com', 'user', 'password', 5430),
('DATABASE_MYSQL_CLOUDSQL_URL', 'django.db.backends.mysql', 'mydatabase',
'/cloudsql/arvore-codelab:us-central1:mysqlinstance', 'djuser', 'hidden-password', ''),
],
ids=[
'postgres',
Expand All @@ -228,6 +230,7 @@ def test_url_encoded_parts(self):
'redshift',
'sqlite',
'custom',
'cloudsql',
],
)
def test_db_url_value(self, var, engine, name, host, user, passwd, port):
Expand Down
1 change: 1 addition & 0 deletions tests/test_env.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
DICT_VAR=foo=bar,test=on
DATABASE_MYSQL_URL=mysql://bea6eb0:69772142@us-cdbr-east.cleardb.com/heroku_97681?reconnect=true
DATABASE_MYSQL_CLOUDSQL_URL=mysql://djuser:hidden-password@//cloudsql/arvore-codelab:us-central1:mysqlinstance/mydatabase
DATABASE_MYSQL_GIS_URL=mysqlgis://user:password@127.0.0.1/some_database
CACHE_URL=memcache://127.0.0.1:11211
CACHE_REDIS=rediscache://127.0.0.1:6379/1?client_class=django_redis.client.DefaultClient&password=secret
Expand Down

0 comments on commit 0b60e16

Please sign in to comment.