Skip to content

Commit

Permalink
add copy_db to postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
DropD committed Oct 19, 2017
1 parent 40b27b0 commit 6856823
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .pylintrc
Expand Up @@ -375,7 +375,7 @@ known-third-party=enchant
max-args=6

# Maximum number of attributes for a class (see R0902).
max-attributes=7
max-attributes=12

# Maximum number of boolean expressions in a if statement
max-bool-expr=5
Expand Down
5 changes: 5 additions & 0 deletions aiida/control/postgres.py
Expand Up @@ -13,6 +13,7 @@
_GRANT_PRIV_COMMAND = 'GRANT ALL PRIVILEGES ON DATABASE "{}" TO "{}"'
_GET_USERS_COMMAND = "SELECT usename FROM pg_user WHERE usename='{}'"
_CHECK_DB_EXISTS_COMMAND = "SELECT datname FROM pg_database WHERE datname='{}'"
_COPY_DB_COMMAND = 'CREATE DATABASE "{}" WITH TEMPLATE "{}" OWNER "{}"'


class Postgres(object):
Expand Down Expand Up @@ -157,6 +158,10 @@ def drop_db(self, dbname):
"""
self.pg_execute(_DROP_DB_COMMAND.format(dbname), **self.dbinfo)

def copy_db(self, src_db, dest_db, dbuser):
self.pg_execute(
_COPY_DB_COMMAND.format(dest_db, src_db, dbuser), **self.dbinfo)

def db_exists(self, dbname):
"""
Check wether a postgres database with dbname exists
Expand Down
4 changes: 4 additions & 0 deletions aiida/utils/fixtures.py
Expand Up @@ -54,6 +54,8 @@ def __init__(self):
}
self.pg_cluster = None
self.postgres = None
self.__is_running_on_test_db = False
self.__is_running_on_test_profile = False

def create_db_cluster(self):
self.pg_cluster = PGTest()
Expand All @@ -76,6 +78,7 @@ def create_aiida_db(self):
'Could not connect to the test postgres instance')
self.postgres.create_dbuser(self.db_user, self.db_pass)
self.postgres.create_db(self.db_user, self.db_name)
self.__is_running_on_test_db = True

def create_root_dir(self):
self.root_dir = tempfile.mkdtemp()
Expand Down Expand Up @@ -104,6 +107,7 @@ def create_profile(self):
**self.profile)
aiida_cfg.set_default_profile('verdi', profile_name)
aiida_cfg.set_default_profile('daemon', profile_name)
self.__is_running_on_test_profile = True

@property
def profile(self):
Expand Down

0 comments on commit 6856823

Please sign in to comment.