Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b6b3820

Browse files
[fabfile][s]: Fabfile sqlalchemy url now copes with port and no password.
1 parent 6b8842e commit b6b3820

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

fabfile.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,8 @@ def backup():
389389
assert exists(env.config_ini_filename), "Can't find config file: %s/%s" % (env.instance_path, env.config_ini_filename)
390390
db_details = _get_db_config()
391391
assert db_details['db_type'] == 'postgres'
392-
run('export PGPASSWORD=%s&&pg_dump -U %s -h %s %s > %s' % (db_details['db_pass'], db_details['db_user'], db_details['db_host'], db_details['db_name'], pg_dump_filepath), shell=False)
392+
port_option = '-p %s' % db_details['db_port'] if db_details['db_port'] else ''
393+
run('export PGPASSWORD=%s&&pg_dump -U %s -h %s %s %s > %s' % (db_details['db_pass'], db_details['db_user'], db_details['db_host'], port_option, db_details['db_name'], pg_dump_filepath), shell=False)
393394
assert exists(pg_dump_filepath)
394395
run('ls -l %s' % pg_dump_filepath)
395396
# copy backup locally
@@ -436,7 +437,8 @@ def restore(pg_dump_filepath):
436437
with cd(env.instance_path):
437438
_run_in_pyenv('paster --plugin ckan db clean --config %s' % env.config_ini_filename)
438439
assert db_details['db_type'] == 'postgres'
439-
run('export PGPASSWORD=%s&&psql -U %s -d %s -h %s -f %s' % (db_details['db_pass'], db_details['db_user'], db_details['db_name'], db_details['db_host'], pg_dump_filepath), shell=False)
440+
port_option = '-p %s' % db_details['db_port'] if db_details['db_port'] else ''
441+
run('export PGPASSWORD=%s&&psql -U %s -d %s -h %s %s -f %s' % (db_details['db_pass'], db_details['db_user'], db_details['db_name'], db_details['db_host'], port_option, pg_dump_filepath), shell=False)
440442
with cd(env.instance_path):
441443
_run_in_pyenv('paster --plugin ckan db upgrade --config %s' % env.config_ini_filename)
442444
_run_in_pyenv('paster --plugin ckan db init --config %s' % env.config_ini_filename)
@@ -580,7 +582,10 @@ def _get_ini_value(key, ini_filepath=None):
580582
def _get_db_config():
581583
url = _get_ini_value('sqlalchemy.url')
582584
# e.g. 'postgres://tester:pass@localhost/ckantest3'
583-
db_details = re.match('^\s*(?P<db_type>\w*)://(?P<db_user>\w*):(?P<db_pass>[^@]*)@(?P<db_host>[\w\.]*)/(?P<db_name>[\w.-]*)', url).groupdict()
585+
db_details_match = re.match('^\s*(?P<db_type>\w*)://(?P<db_user>\w*):?(?P<db_pass>[^@]*)@(?P<db_host>[^/:]*):?(?P<db_port>[^/]*)/(?P<db_name>[\w.-]*)', url)
586+
if not db_details_match:
587+
raise Exception('Could not extract db details from url: %r' % url)
588+
db_details = db_details_match.groupdict()
584589
return db_details
585590

586591
def _get_ckan_pyenv_dict():

0 commit comments

Comments
 (0)