Skip to content

Commit

Permalink
Merge pull request #4947 from legiongis/4941_improve_pguser_createdb_…
Browse files Browse the repository at this point in the history
…chk_4.4.x

improve check for createdb privileges during setup_db re: #4941
  • Loading branch information
chiatt committed Jun 25, 2019
2 parents fef1c58 + e75ccc3 commit 85ab99d
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions arches/management/commands/setup_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,13 @@ def get_connection(self):
"re-run this same command.")
exit()

cursor = conn.cursor()
cursor.execute("SELECT rolcreatedb FROM pg_roles WHERE rolname = '{}'".format(username))
cancreate = cursor.fetchone()[0]

# autocommit false
conn.set_isolation_level(0)
return conn
return {"connection": conn, "can_create_db": cancreate}

def reset_db(self, cursor):

Expand Down Expand Up @@ -146,14 +150,12 @@ def setup_db(self):
WARNING: This will destroy data
"""

conn = self.get_connection()
cursor = conn.cursor()
conninfo = self.get_connection()
conn = conninfo['connection']
can_create_db = conninfo['can_create_db']

# figure out if this is a superuser or not
cursor.execute("SELECT current_setting('is_superuser')")
superuser = True if cursor.fetchone()[0] == "on" else False

if superuser:
cursor = conn.cursor()
if can_create_db is True:
self.drop_and_recreate_db(cursor)
else:
self.reset_db(cursor)
Expand Down

0 comments on commit 85ab99d

Please sign in to comment.