Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support specific PgSQL version and db name #18

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/testing/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
SEARCH_PATHS = (['/usr/local/pgsql', '/usr/local'] +
glob('/usr/pgsql-*') + # for CentOS/RHEL
glob('/usr/lib/postgresql/*') + # for Debian/Ubuntu
glob('/opt/local/lib/postgresql*')) # for MacPorts

glob('/opt/local/lib/postgresql*') + # for MacPorts
glob('/usr/local/opt/postgresql*')) # for Homebrew

class Postgresql(Database):
DEFAULT_SETTINGS = dict(auto_start=2,
Expand All @@ -40,6 +40,8 @@ class Postgresql(Database):
initdb_args='-U postgres -A trust',
postgres=None,
postgres_args='-h 127.0.0.1 -F -c logging_collector=off',
postgres_version=None,
db_name='test',
pid=None,
port=None,
copy_data_from=None)
Expand All @@ -48,19 +50,19 @@ class Postgresql(Database):
def initialize(self):
self.initdb = self.settings.pop('initdb')
if self.initdb is None:
self.initdb = find_program('initdb', ['bin'])
self.initdb = find_program('initdb', ['bin'], self.settings['postgres_version'])

self.postgres = self.settings.pop('postgres')
if self.postgres is None:
self.postgres = find_program('postgres', ['bin'])
self.postgres = find_program('postgres', ['bin'], self.settings['postgres_version'])

def dsn(self, **kwargs):
# "database=test host=localhost user=postgres"
params = dict(kwargs)
params.setdefault('port', self.settings['port'])
params.setdefault('host', '127.0.0.1')
params.setdefault('user', 'postgres')
params.setdefault('database', 'test')
params.setdefault('database', self.settings['db_name'])

return params

Expand Down Expand Up @@ -99,9 +101,9 @@ def poststart(self):
with closing(pg8000.connect(**self.dsn(database='postgres'))) as conn:
conn.autocommit = True
with closing(conn.cursor()) as cursor:
cursor.execute("SELECT COUNT(*) FROM pg_database WHERE datname='test'")
cursor.execute("SELECT COUNT(*) FROM pg_database WHERE datname='{}'".format(self.settings['db_name']))
if cursor.fetchone()[0] <= 0:
cursor.execute('CREATE DATABASE test')
cursor.execute('CREATE DATABASE {}'.format(self.settings['db_name']))

def is_server_available(self):
try:
Expand Down Expand Up @@ -131,15 +133,17 @@ def search_server(self):
skipIfNotFound = skipIfNotInstalled = PostgresqlSkipIfNotInstalledDecorator()


def find_program(name, subdirs):
def find_program(name, subdirs, version=None):
path = get_path_of(name)
if path:
return path
if not version or version in path:
return path

for base_dir in SEARCH_PATHS:
for subdir in subdirs:
path = os.path.join(base_dir, subdir, name)
if os.path.exists(path):
return path
if not version or version in path:
return path

raise RuntimeError("command not found: %s" % name)