Skip to content

Commit

Permalink
fix handling of postgres DBs/roles with hyphens in them
Browse files Browse the repository at this point in the history
closes #377
  • Loading branch information
trehn committed Dec 31, 2017
1 parent f767277 commit c006f17
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ python:
- 3.4
- 3.5
- 3.6
services:
- postgresql
install:
- pip install .
before_script:
Expand Down
2 changes: 1 addition & 1 deletion bundlewrap/items/postgres_dbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def get_databases(node):

def set_owner(node, name, owner):
return node.run(
"echo 'ALTER DATABASE {name} OWNER TO {owner}' | "
"echo 'ALTER DATABASE \"{name}\" OWNER TO \"{owner}\"' | "
"sudo -u postgres psql -nqw".format(
name=name,
owner=owner,
Expand Down
2 changes: 1 addition & 1 deletion bundlewrap/items/postgres_roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def delete_role(node, role):
def fix_role(node, role, attrs, create=False):
password = " PASSWORD '{}'".format(attrs['password_hash'])
node.run(
"echo \"{operation} ROLE {role} WITH LOGIN {superuser}SUPERUSER{password}\" "
"echo \"{operation} ROLE \\\"{role}\\\" WITH LOGIN {superuser}SUPERUSER{password}\" "
"| sudo -u postgres psql -nqw".format(
operation="CREATE" if create else "ALTER",
password="" if attrs['password_hash'] is None else password,
Expand Down
55 changes: 55 additions & 0 deletions tests/integration/bw_apply_postgres.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from json import loads
from os import environ

from bundlewrap.utils.testing import host_os, make_repo, run


if environ.get('TRAVIS') == "true":
def test_create(tmpdir):
make_repo(
tmpdir,
bundles={
"test": {
'postgres_dbs': {
"bw-test1": {
'owner': "bw-test1",
},
},
'postgres_roles': {
"bw-test1": {
'superuser': True,
'password': 'potato',
},
},
},
},
nodes={
"localhost": {
'bundles': ["test"],
'os': host_os(),
},
},
)

stdout, stderr, rcode = run("bw apply localhost", path=str(tmpdir))
assert rcode == 0

stdout, stderr, rcode = run("bw items --state localhost postgres_db:bw-test1", path=str(tmpdir))
assert rcode == 0
assert loads(stdout.decode()) == {'owner': "bw-test1"}

stdout, stderr, rcode = run("bw items --state localhost postgres_role:bw-test1", path=str(tmpdir))
assert rcode == 0
assert loads(stdout.decode()) == {
'can_login': True,
'password_hash': "md5ecba3aec62c5aabf6480de6352182004",
'superuser': True,
}

stdout, stderr, rcode = run("dropdb bw-test1", path=str(tmpdir))
assert rcode == 0
stdout, stderr, rcode = run("dropuser bw-test1", path=str(tmpdir))
assert rcode == 0

0 comments on commit c006f17

Please sign in to comment.