diff --git a/bigchaindb/db/utils.py b/bigchaindb/db/utils.py index 7b2939e6..8e34fd99 100644 --- a/bigchaindb/db/utils.py +++ b/bigchaindb/db/utils.py @@ -161,10 +161,7 @@ def create_votes_secondary_index(conn, dbname): r.db(dbname).table('votes').index_wait().run(conn) -def init(): - # Try to access the keypair, throws an exception if it does not exist - b = bigchaindb.Bigchain() - +def init_database(): conn = get_conn() dbname = get_database_name() create_database(conn, dbname) @@ -172,10 +169,18 @@ def init(): table_names = ['bigchain', 'backlog', 'votes'] for table_name in table_names: create_table(conn, dbname, table_name) + create_bigchain_secondary_index(conn, dbname) create_backlog_secondary_index(conn, dbname) create_votes_secondary_index(conn, dbname) + +def init(): + # Try to access the keypair, throws an exception if it does not exist + b = bigchaindb.Bigchain() + + init_database() + logger.info('Create genesis block.') b.create_genesis_block() logger.info('Done, have fun!') @@ -184,9 +189,9 @@ def init(): def drop(assume_yes=False): conn = get_conn() dbname = bigchaindb.config['database']['name'] - if assume_yes: response = 'y' + else: response = input('Do you want to drop `{}` database? [y/n]: '.format(dbname)) @@ -197,5 +202,6 @@ def drop(assume_yes=False): logger.info('Done.') except r.ReqlOpFailedError: raise exceptions.DatabaseDoesNotExist('Database `{}` does not exist'.format(dbname)) + else: logger.info('Drop aborted') diff --git a/tests/db/conftest.py b/tests/db/conftest.py index f55a4c34..fe4508b7 100644 --- a/tests/db/conftest.py +++ b/tests/db/conftest.py @@ -10,11 +10,13 @@ import pytest import rethinkdb as r from bigchaindb import Bigchain -from bigchaindb.db import get_conn +from bigchaindb.db import get_conn, init_database from bigchaindb.common import crypto +from bigchaindb.common.exceptions import DatabaseAlreadyExists USER2_SK, USER2_VK = crypto.generate_key_pair() + @pytest.fixture(autouse=True) def restore_config(request, node_config): from bigchaindb import config_utils @@ -25,53 +27,17 @@ def restore_config(request, node_config): def setup_database(request, node_config): print('Initializing test db') db_name = node_config['database']['name'] - get_conn().repl() + conn = get_conn() + + if r.db_list().contains(db_name).run(conn): + r.db_drop(db_name).run(conn) + try: - r.db_create(db_name).run() - except r.ReqlOpFailedError as e: - if e.message == 'Database `{}` already exists.'.format(db_name): - r.db_drop(db_name).run() - r.db_create(db_name).run() - else: - raise + init_database() + except DatabaseAlreadyExists: + print('Database already exists.') - print('Finished initializing test db') - - # setup tables - r.db(db_name).table_create('bigchain').run() - r.db(db_name).table_create('backlog').run() - r.db(db_name).table_create('votes').run() - - # create the secondary indexes - # to order blocks by timestamp - r.db(db_name).table('bigchain').index_create('block_timestamp', r.row['block']['timestamp']).run() - # to order blocks by block number - r.db(db_name).table('bigchain').index_create('block_number', r.row['block']['block_number']).run() - # to order transactions by timestamp - r.db(db_name).table('backlog').index_create('transaction_timestamp', r.row['transaction']['timestamp']).run() - # to query by payload uuid - r.db(db_name).table('bigchain').index_create( - 'metadata_id', - r.row['block']['transactions']['transaction']['metadata']['id'], - multi=True, - ).run() - # compound index to read transactions from the backlog per assignee - r.db(db_name).table('backlog')\ - .index_create('assignee__transaction_timestamp', [r.row['assignee'], r.row['transaction']['timestamp']])\ - .run() - # compound index to order votes by block id and node - r.db(db_name).table('votes').index_create('block_and_voter', - [r.row['vote']['voting_for_block'], r.row['node_pubkey']]).run() - # secondary index for asset uuid - r.db(db_name).table('bigchain')\ - .index_create('asset_id', - r.row['block']['transactions']['transaction']['asset']['id'], multi=True)\ - .run() - # order transactions by id - r.db(db_name).table('bigchain').index_create('transaction_id', r.row['block']['transactions']['id'], - multi=True).run() - - r.db(db_name).table('bigchain').index_wait('transaction_id').run() + print('Finishing init database') def fin(): print('Deleting `{}` database'.format(db_name)) @@ -81,7 +47,6 @@ def setup_database(request, node_config): except r.ReqlOpFailedError as e: if e.message != 'Database `{}` does not exist.'.format(db_name): raise - print('Finished deleting `{}`'.format(db_name)) request.addfinalizer(fin)