Fix postgresql_user bug

If I create a database from scratch and assign permissions by doing:

      - name: ensure database is created
        action: postgresql_db db=$dbname

      - name: ensure django user has access
        action: postgresql_user db=$dbname user=$dbuser priv=ALL password=$dbpassword

Then it fails with the error:

  File "/tmp/ansible-1347048449.32-29998829936529/postgresql_user", line 565, in <module>
    main()
  File "/tmp/ansible-1347048449.32-29998829936529/postgresql_user", line 273, in main
    changed = grant_privileges(cursor, user, privs) or changed
  File "/tmp/ansible-1347048449.32-29998829936529/postgresql_user", line 174, in grant_privileges
    changed = grant_func(cursor, user, name, privilege)\
  File "/tmp/ansible-1347048449.32-29998829936529/postgresql_user", line 132, in grant_database_privilege
    prev_priv = get_database_privileges(cursor, user, db)
  File "/tmp/ansible-1347048449.32-29998829936529/postgresql_user", line 118, in get_database_privileges
    r = re.search('%s=(C?T?c?)/[a-z]+\,?' % user, datacl)
  File "/usr/lib/python2.7/re.py", line 142, in search
    return _compile(pattern, flags).search(string)
TypeError: expected string or buffer

This fix fixes the problem by not executing the regex if the
db query on pg_database returns None.
reviewable/pr18780/r1
Lorin Hochstein 12 years ago
parent 3df57c699e
commit 9eb12a2b81

@ -115,6 +115,8 @@ def get_database_privileges(cursor, user, db):
query = 'SELECT datacl FROM pg_database WHERE datname = %s' query = 'SELECT datacl FROM pg_database WHERE datname = %s'
cursor.execute(query, (db,)) cursor.execute(query, (db,))
datacl = cursor.fetchone()[0] datacl = cursor.fetchone()[0]
if datacl is None:
return []
r = re.search('%s=(C?T?c?)/[a-z]+\,?' % user, datacl) r = re.search('%s=(C?T?c?)/[a-z]+\,?' % user, datacl)
if r is None: if r is None:
return [] return []

Loading…
Cancel
Save