Skip to content

Commit

Permalink
Merge pull request #94 from josenavas/issue_86
Browse files Browse the repository at this point in the history
Issue 86
  • Loading branch information
teravest committed Jun 18, 2014
2 parents d08c7dc + aacd8de commit cc3b832
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 6 deletions.
3 changes: 2 additions & 1 deletion qiita_core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------
from qiita_db.sql_connection import SQLConnectionHandler
from qiita_db.make_environment import (LAYOUT_FP, INITIALIZE_FP, POPULATE_FP)
from qiita_db.environment_manager import (LAYOUT_FP, INITIALIZE_FP,
POPULATE_FP)
from qiita_core.qiita_settings import qiita_config


Expand Down
59 changes: 58 additions & 1 deletion qiita_db/make_environment.py → qiita_db/environment_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@


def make_test_environment(base_data_dir, base_work_dir, user, password, host):
"""Creates a test database environment.
r"""Creates a test database environment.
Creates a new database called `qiita_test` tailored for testing purposes
and initializes the `settings` table of such database
Expand Down Expand Up @@ -62,6 +62,63 @@ def make_test_environment(base_data_dir, base_work_dir, user, password, host):
conn.close()


def clean_test_environment(user, password, host):
r"""Cleans the test database environment.
In case that the test database is dirty (i.e. the 'qiita' schema is
present), this cleans it up by dropping the 'qiita' schema.
Parameters
----------
user : str
The postgres user to connect to the server
password : str
The password of the user
host : str
The host where the postgres server is running
"""
# Connect to the postgres server
conn = connect(user=user, host=host, password=password,
database='qiita_test')
# Get the cursor
cur = conn.cursor()
# Drop the qiita schema
cur.execute("DROP SCHEMA qiita CASCADE")
# Commit the changes
conn.commit()
# Close cursor and connections
cur.close()
conn.close()


def drop_test_environment(user, password, host):
r"""Drops the test environment.
If the `settings` table is modified, the test database environment should
be rebuilt. This command allows to drop the old one.
Parameters
----------
user : str
The postgres user to connect to the server
password : str
The password of the user
host : str
The host where the postgres server is running
"""
# Connect to the postgres server
conn = connect(user=user, host=host, password=password)
# Set the isolation level to AUTOCOMMIT so we can execute a
# drop database sql query
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
# Drop the database
cur = conn.cursor()
cur.execute('DROP DATABASE qiita_test')
# Close cursor and connection
cur.close()
conn.close()


def make_production_environment():
"""TODO: Not implemented"""
raise NotImplementedError()
40 changes: 36 additions & 4 deletions scripts/qiita_db
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@

import click

from qiita_db.make_environment import (make_test_environment,
make_production_environment,
DFLT_BASE_DATA_FOLDER,
DFLT_BASE_WORK_FOLDER)
from qiita_db.environment_manager import (make_test_environment,
make_production_environment,
drop_test_environment,
clean_test_environment,
DFLT_BASE_DATA_FOLDER,
DFLT_BASE_WORK_FOLDER)


@click.group()
Expand All @@ -39,12 +41,42 @@ def make_test_env(base_data_folder, base_work_folder, user, host):
make_test_environment(base_data_folder, base_work_folder, user, None, host)


@click.command()
@click.option('--user', default='postgres',
help="The database user to connect to the database")
@click.option('--host', default='localhost',
help='The host where the database lives')
def clean_test_env(user, host):
"""Cleans the test database environment.
In case that the test database is dirty (i.e. the 'qiita' schema is
present), this cleans it up by dropping the 'qiita' schema.
"""
clean_test_environment(user, None, host)


@click.command()
@click.option('--user', default='postgres',
help="The database user to connect to the database")
@click.option('--host', default='localhost',
help='The host where the database lives')
def drop_test_env(user, host):
"""Drops the test database environment.
If the `settings` table is modified, the test database environment should
be rebuilt. This command allows to drop the old one.
"""
drop_test_environment(user, None, host)


@click.command()
def make_production_env():
"""TODO: Not implemented"""
make_production_environment()

qiita_db.add_command(make_test_env)
qiita_db.add_command(clean_test_env)
qiita_db.add_command(drop_test_env)
qiita_db.add_command(make_production_env)

if __name__ == '__main__':
Expand Down

0 comments on commit cc3b832

Please sign in to comment.