Skip to content

Instantly share code, notes, and snippets.

@Tinitto
Created March 9, 2018 06:43
Show Gist options
  • Save Tinitto/17e0c685dd48c5335ecef3dece8f0823 to your computer and use it in GitHub Desktop.
Save Tinitto/17e0c685dd48c5335ecef3dece8f0823 to your computer and use it in GitHub Desktop.
Utility functions to help integrate Sqlalchemy with django
"""Common utility functions sqlalchemy in django"""
def dj_pg_db_url_to_sqlachemy(dj_pg_db_url):
"""
transforms a dj_database_url postgres url to
an sqlalchemy onereplacing 'postgres://...' with 'posgresql://...'
"""
try:
return 'postgresql%s' % (dj_pg_db_url[dj_pg_db_url.find('://'):])
except Exception as exp:
print(str(exp))
def dj_test_db_url(dj_db_url):
"""
Converts the url the normal database
to the url for the test database
"""
try:
last_slash_position = dj_db_url.rfind('/') + 1
test_db_name = 'test_%s' % (dj_db_url[last_slash_position:],)
return '%s%s' % (dj_db_url[:last_slash_position], test_db_name,)
except Exception as exp:
print(str(exp))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment