@@ -52,35 +52,40 @@ def get_dburl(outer={}):
5252 # outer may contain the locals from the calling function
5353 # nonlocal env, settings # Python 3 only
5454
55+ dburl = None
5556 if "WEB2PY_CONFIG" in environ :
5657 w2py_config = environ ["WEB2PY_CONFIG" ]
5758 if w2py_config == "development" :
58- return environ ["DEV_DBURL" ]
59+ dburl = environ ["DEV_DBURL" ]
5960
60- if w2py_config == "production" :
61- return environ ["DBURL" ]
61+ elif w2py_config == "production" :
62+ dburl = environ ["DBURL" ]
6263
63- if w2py_config == "test" :
64- return environ ["TEST_DBURL" ]
64+ elif w2py_config == "test" :
65+ dburl = environ ["TEST_DBURL" ]
6566
66- if "options" in outer :
67- return outer ["options" ].build .template_args ["dburl" ]
67+ elif "options" in outer :
68+ dburl = outer ["options" ].build .template_args ["dburl" ]
6869
69- if "env" in outer :
70- return outer ["env" ].config .html_context ["dburl" ]
70+ elif "env" in outer :
71+ dburl = outer ["env" ].config .html_context ["dburl" ]
7172
72- if "env" in globals ():
73- return globals ()["env" ].config .html_context ["dburl" ]
73+ elif "env" in globals ():
74+ dburl = globals ()["env" ].config .html_context ["dburl" ]
7475
75- ret = None
76- if "settings" in outer :
77- ret = outer ["settings" ].database_uri
76+ elif "settings" in outer :
77+ dburl = outer ["settings" ].database_uri
7878
79- if "settings" in globals ():
80- ret = globals ()["settings" ].database_uri . replace ( "postgres:" , "postgresql:" )
79+ elif "settings" in globals ():
80+ dburl = globals ()["settings" ].database_uri
8181
82- if ret :
83- return re .sub (r"postgres:.*/" , "postgresql:/" , ret )
82+ # We allow a dburl of "", hence the check against None.
83+ if dburl is not None :
84+ # DAL uses "postgres:", while SQLAlchemy (and the PostgreSQL spec) uses "postgresql:". Fix.
85+ remove_prefix = "postgres://"
86+ if dburl .startswith (remove_prefix ):
87+ dburl = "postgresql://" + dburl [len (remove_prefix ):]
88+ return dburl
8489
8590 raise RuntimeError ("Cannot configure a Database URL!" )
8691
0 commit comments