Add script() and set_user_password() to postgresql fablib

This commit is contained in:
Lance Edgar 2017-03-10 18:55:56 -06:00
parent 46ac996d96
commit bc7357e042

View file

@ -58,6 +58,13 @@ def sql(sql, database=''):
return sudo('sudo -u postgres psql --tuples-only --no-align --command="{0}" {1}'.format(sql, database), shell=False) return sudo('sudo -u postgres psql --tuples-only --no-align --command="{0}" {1}'.format(sql, database), shell=False)
def script(path, database=''):
"""
Execute a SQL script as the 'postgres' user
"""
return sudo('sudo -u postgres psql --file={} {}'.format(path, database), shell=False)
def user_exists(name): def user_exists(name):
""" """
Determine if a given PostgreSQL user exists. Determine if a given PostgreSQL user exists.
@ -75,8 +82,15 @@ def create_user(name, password=None, checkfirst=True, createdb=False):
sudo('sudo -u postgres createuser {createdb} --no-createrole --no-superuser {name}'.format( sudo('sudo -u postgres createuser {createdb} --no-createrole --no-superuser {name}'.format(
createdb=createdb, name=name)) createdb=createdb, name=name))
if password: if password:
with hide('running'): set_user_password(name, password)
sql("ALTER USER \\\"{0}\\\" PASSWORD '{1}';".format(name, password))
def set_user_password(name, password):
"""
Set the password for a PostgreSQL user account
"""
with hide('running'):
sql("ALTER USER \\\"{}\\\" PASSWORD '{}';".format(name, password))
def db_exists(name): def db_exists(name):