From fdbc99dc28686883a988f7eed0a0b19366f5a4bd Mon Sep 17 00:00:00 2001 From: Pepe Barbe Date: Wed, 22 Aug 2012 12:20:51 -0500 Subject: [PATCH 1/2] Check for database ownership --- library/postgresql_db | 53 ++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/library/postgresql_db b/library/postgresql_db index 8bfdc9fd20e..ca5e8053180 100755 --- a/library/postgresql_db +++ b/library/postgresql_db @@ -27,26 +27,45 @@ else: # PostgreSQL module specific support methods. # +def set_owner(cursor, db, owner): + query = "ALTER DATABASE %s OWNER TO %s" % (db, owner) + cursor.execute(query) + return True + +def db_owned_by(cursor, db, user): + query = """SELECT count(*) FROM pg_database JOIN pg_user ON datdba = usesysid + WHERE usename = %(user)s and datname = %(db)s""" + cursor.execute(query, {'db':db, 'user':user}) + return cursor.rowcount == 1 + def db_exists(cursor, db): query = "SELECT * FROM pg_database WHERE datname=%(db)s" cursor.execute(query, {'db': db}) return cursor.rowcount == 1 def db_delete(cursor, db): - query = "DROP DATABASE %s" % db - cursor.execute(query) - return True + if db_exists(cursor, db): + query = "DROP DATABASE %s" % db + cursor.execute(query) + return True + else: + return False def db_create(cursor, db, owner, template, encoding): - if owner: - owner = " OWNER %s" % owner - if template: - template = " TEMPLATE %s" % template - if encoding: - encoding = " ENCODING '%s'" % encoding - query = "CREATE DATABASE %s%s%s%s" % (db, owner, template, encoding) - cursor.execute(query) - return True + if not db_exists(cursor, db): + if owner: + owner = " OWNER %s" % owner + if template: + template = " TEMPLATE %s" % template + if encoding: + encoding = " ENCODING '%s'" % encoding + query = "CREATE DATABASE %s%s%s%s" % (db, owner, template, encoding) + cursor.execute(query) + return True + elif owner and not db_owned_by(cursor, db, owner): + return set_owner(cursor, db, owner) + else: + return False # =========================================== # Module execution. @@ -100,12 +119,10 @@ def main(): module.fail_json(msg="unable to connect to database: %s" % e) try: - if db_exists(cursor, db): - if state == "absent": - changed = db_delete(cursor, db) - else: - if state == "present": - changed = db_create(cursor, db, owner, template, encoding) + if state == "absent": + changed = db_delete(cursor, db) + elif state == "present": + changed = db_create(cursor, db, owner, template, encoding) except Exception, e: module.fail_json(msg="Database query failed: %s" % e) From fdaf65282b4cd7d76758bc6ed4f6feda270fedd0 Mon Sep 17 00:00:00 2001 From: Pepe Barbe Date: Wed, 22 Aug 2012 13:34:24 -0500 Subject: [PATCH 2/2] bugfix in sql query --- library/postgresql_db | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/postgresql_db b/library/postgresql_db index ca5e8053180..32ee9205909 100755 --- a/library/postgresql_db +++ b/library/postgresql_db @@ -33,7 +33,7 @@ def set_owner(cursor, db, owner): return True def db_owned_by(cursor, db, user): - query = """SELECT count(*) FROM pg_database JOIN pg_user ON datdba = usesysid + query = """SELECT * FROM pg_database JOIN pg_user ON datdba = usesysid WHERE usename = %(user)s and datname = %(db)s""" cursor.execute(query, {'db':db, 'user':user}) return cursor.rowcount == 1