def setupdb(): # If we're using PostgreSQL, set up the db once if USE_POSTGRES_FOR_TESTS: # create a PostgresEngine db_engine = create_engine({"name": "psycopg2", "args": {}}) # connect to postgres to create the base database. db_conn = db_engine.module.connect( user=POSTGRES_USER, host=POSTGRES_HOST, password=POSTGRES_PASSWORD, dbname=POSTGRES_DBNAME_FOR_INITIAL_CREATE, ) db_conn.autocommit = True cur = db_conn.cursor() cur.execute("DROP DATABASE IF EXISTS %s;" % (POSTGRES_BASE_DB, )) cur.execute("CREATE DATABASE %s;" % (POSTGRES_BASE_DB, )) cur.close() db_conn.close() # Set up in the db db_conn = db_engine.module.connect( database=POSTGRES_BASE_DB, user=POSTGRES_USER, host=POSTGRES_HOST, password=POSTGRES_PASSWORD, ) cur = db_conn.cursor() _get_or_create_schema_state(cur, db_engine) _setup_new_database(cur, db_engine) db_conn.commit() cur.close() db_conn.close() def _cleanup(): db_conn = db_engine.module.connect( user=POSTGRES_USER, host=POSTGRES_HOST, password=POSTGRES_PASSWORD, dbname=POSTGRES_DBNAME_FOR_INITIAL_CREATE, ) db_conn.autocommit = True cur = db_conn.cursor() cur.execute("DROP DATABASE IF EXISTS %s;" % (POSTGRES_BASE_DB, )) cur.close() db_conn.close() atexit.register(_cleanup)
def setupdb(): # If we're using PostgreSQL, set up the db once if USE_POSTGRES_FOR_TESTS: # create a PostgresEngine db_engine = create_engine({"name": "psycopg2", "args": {}}) # connect to postgres to create the base database. db_conn = db_engine.module.connect( user=POSTGRES_USER, host=POSTGRES_HOST, password=POSTGRES_PASSWORD, dbname=POSTGRES_DBNAME_FOR_INITIAL_CREATE, ) db_conn.autocommit = True cur = db_conn.cursor() cur.execute("DROP DATABASE IF EXISTS %s;" % (POSTGRES_BASE_DB,)) cur.execute("CREATE DATABASE %s;" % (POSTGRES_BASE_DB,)) cur.close() db_conn.close() # Set up in the db db_conn = db_engine.module.connect( database=POSTGRES_BASE_DB, user=POSTGRES_USER, host=POSTGRES_HOST, password=POSTGRES_PASSWORD, ) cur = db_conn.cursor() _get_or_create_schema_state(cur, db_engine) _setup_new_database(cur, db_engine) db_conn.commit() cur.close() db_conn.close() def _cleanup(): db_conn = db_engine.module.connect( user=POSTGRES_USER, host=POSTGRES_HOST, password=POSTGRES_PASSWORD, dbname=POSTGRES_DBNAME_FOR_INITIAL_CREATE, ) db_conn.autocommit = True cur = db_conn.cursor() cur.execute("DROP DATABASE IF EXISTS %s;" % (POSTGRES_BASE_DB,)) cur.close() db_conn.close() atexit.register(_cleanup)
def setupdb(): # If we're using PostgreSQL, set up the db once if USE_POSTGRES_FOR_TESTS: pgconfig = { "name": "psycopg2", "args": { "database": POSTGRES_BASE_DB, "user": POSTGRES_USER, "cp_min": 1, "cp_max": 5, }, } config = Mock() config.password_providers = [] config.database_config = pgconfig db_engine = create_engine(pgconfig) db_conn = db_engine.module.connect(user=POSTGRES_USER) db_conn.autocommit = True cur = db_conn.cursor() cur.execute("DROP DATABASE IF EXISTS %s;" % (POSTGRES_BASE_DB,)) cur.execute("CREATE DATABASE %s;" % (POSTGRES_BASE_DB,)) cur.close() db_conn.close() # Set up in the db db_conn = db_engine.module.connect( database=POSTGRES_BASE_DB, user=POSTGRES_USER ) cur = db_conn.cursor() _get_or_create_schema_state(cur, db_engine) _setup_new_database(cur, db_engine) db_conn.commit() cur.close() db_conn.close() def _cleanup(): db_conn = db_engine.module.connect(user=POSTGRES_USER) db_conn.autocommit = True cur = db_conn.cursor() cur.execute("DROP DATABASE IF EXISTS %s;" % (POSTGRES_BASE_DB,)) cur.close() db_conn.close() atexit.register(_cleanup)
def setupdb(): # If we're using PostgreSQL, set up the db once if USE_POSTGRES_FOR_TESTS: pgconfig = { "name": "psycopg2", "args": { "database": POSTGRES_BASE_DB, "user": POSTGRES_USER, "cp_min": 1, "cp_max": 5, }, } config = Mock() config.password_providers = [] config.database_config = pgconfig db_engine = create_engine(pgconfig) db_conn = db_engine.module.connect(user=POSTGRES_USER) db_conn.autocommit = True cur = db_conn.cursor() cur.execute("DROP DATABASE IF EXISTS %s;" % (POSTGRES_BASE_DB,)) cur.execute("CREATE DATABASE %s;" % (POSTGRES_BASE_DB,)) cur.close() db_conn.close() # Set up in the db db_conn = db_engine.module.connect( database=POSTGRES_BASE_DB, user=POSTGRES_USER ) cur = db_conn.cursor() _get_or_create_schema_state(cur, db_engine) _setup_new_database(cur, db_engine) db_conn.commit() cur.close() db_conn.close() def _cleanup(): db_conn = db_engine.module.connect(user=POSTGRES_USER) db_conn.autocommit = True cur = db_conn.cursor() cur.execute("DROP DATABASE IF EXISTS %s;" % (POSTGRES_BASE_DB,)) cur.close() db_conn.close() atexit.register(_cleanup)