Пример #1
0
def create_db(connection):
    """Creates the test database tables."""
    driver = connection.engine.name
    if driver == 'sqlite':
        return create_sqlite_db(connection)

    # Create the test database and connect to it. We need to autocommit
    # if the database supports it because PostgreSQL doesn't allow
    # CREATE/DROP DATABASE statements within transactions.
    def _execute(sql, *args, **kwargs):
        engine = _get_admin_connection(connection)
        engine.execute(
            text(sql).execution_options(autocommit=True), *args, **kwargs)

    test_database_name = connection.engine.url.database

    try:
        _execute('CREATE DATABASE %s' % test_database_name)
    except Exception as exc:
        logger.warning('Got an error creating the test database: %s\n' % exc)
        logger.info('Destroying old test database "%s"...' %
                    unicode(connection.engine.url))
        try:
            _execute('DROP DATABASE %s' % test_database_name)
            _execute('CREATE DATABASE %s' % test_database_name)
        except Exception as exc:
            logger.warning('Got an error recreating the test database: %s\n' %
                           exc)

    return test_database_name
Пример #2
0
def create_db(connection):
    """Creates the test database tables."""
    driver = connection.engine.name
    if driver == 'sqlite':
        return create_sqlite_db(connection)

    # Create the test database and connect to it. We need to autocommit
    # if the database supports it because PostgreSQL doesn't allow
    # CREATE/DROP DATABASE statements within transactions.
    def _execute(sql, *args, **kwargs):
        engine = _get_admin_connection(connection)
        engine.execute(text(sql).execution_options(autocommit=True), *args, **kwargs)

    test_database_name = connection.engine.url.database

    try:
        _execute('CREATE DATABASE %s' % test_database_name)
    except Exception as exc:
        logger.warning('Got an error creating the test database: %s\n' % exc)
        logger.info('Destroying old test database "%s"...' % unicode(connection.engine.url))
        try:
            _execute('DROP DATABASE %s' % test_database_name)
            _execute('CREATE DATABASE %s' % test_database_name)
        except Exception as exc:
            logger.warning('Got an error recreating the test database: %s\n' % exc)

    return test_database_name
Пример #3
0
def destroy_sqlite_db(connection):
    test_database_name = connection.engine.url.database
    if test_database_name and test_database_name != ":memory:":
        # Remove the SQLite database file
        if os.access(test_database_name, os.F_OK):
            try:
                os.remove(test_database_name)
            except Exception as exc:
                logger.warning('Got an error deleting the test database: %s\n' % exc)
    return test_database_name
Пример #4
0
def destroy_sqlite_db(connection):
    test_database_name = connection.engine.url.database
    if test_database_name and test_database_name != ":memory:":
        # Remove the SQLite database file
        if os.access(test_database_name, os.F_OK):
            try:
                os.remove(test_database_name)
            except Exception as exc:
                logger.warning(
                    'Got an error deleting the test database: %s\n' % exc)
    return test_database_name
Пример #5
0
def create_sqlite_db(connection):
    test_database_name = connection.engine.url.database
    if not test_database_name:
        test_database_name = ':memory:'

    if test_database_name != ':memory:':
        # Erase the old test database
        logger.info('Destroying old test database "%s"...' % unicode(connection.engine.url))
        if os.access(test_database_name, os.F_OK):
            try:
                os.remove(test_database_name)
            except Exception as exc:
                logger.warning('Got an error deleting the old test database: %s\n' % exc)
    return test_database_name
Пример #6
0
def create_sqlite_db(connection):
    test_database_name = connection.engine.url.database
    if not test_database_name:
        test_database_name = ':memory:'

    if test_database_name != ':memory:':
        # Erase the old test database
        logger.info('Destroying old test database "%s"...' %
                    unicode(connection.engine.url))
        if os.access(test_database_name, os.F_OK):
            try:
                os.remove(test_database_name)
            except Exception as exc:
                logger.warning(
                    'Got an error deleting the old test database: %s\n' % exc)
    return test_database_name