Exemple #1
0
def drop_database(url):
    url = copy(sqlalchemy.engine.url.make_url(url))
    url.database = 'postgres'
    engine = sqlalchemy.create_engine(url)
    text = 'DROP DATABASE {0}'.format(orm.quote(engine, 'powerscan_scenario'))
    cnx = engine.connect()
    cnx.execute("ROLLBACK")
    cnx.execute(text)
    cnx.execute("commit")
    cnx.close()
Exemple #2
0
def create_database(url, encoding='utf8'):
    """Issue the appropriate CREATE DATABASE statement.

    This is a modification of sqlalchemy_utils.functions.database.create_database
    since the latter one did not correctly work with SQLAlchemy and PostgreSQL.

    :param url: A SQLAlchemy engine URL.
    :param encoding: The encoding to create the database as.


    It currently supports only PostgreSQL and the psycopg2 driver.
    """

    from sqlalchemy.engine.url import make_url
    from sqlalchemy_utils.functions.orm import quote
    from copy import copy
    import sqlalchemy as sa

    url = copy(make_url(url))

    database = url.database

    # A default PostgreSQL database to connect
    url.database = 'template1'

    engine = sa.create_engine(url)

    try:
        if engine.dialect.name == 'postgresql' and engine.driver == 'psycopg2':
            from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
            engine.raw_connection().set_isolation_level(
                ISOLATION_LEVEL_AUTOCOMMIT
            )

            text = "CREATE DATABASE {0} ENCODING '{1}'".format(
                quote(engine, database),
                encoding
            )

            engine.execute(text)

        else:
            raise Exception('Only PostgreSQL with the psycopg2 driver is '
                            'supported.')
    finally:
        engine.dispose()
Exemple #3
0
def drop_database(url):
    url = copy(sqlalchemy.engine.url.make_url(url))
    database = url.database
    if url.drivername.startswith('postgresql'):
        url.database = 'postgres'
    elif not url.drivername.startswith('sqlite'):
        url.database = None

    engine = sqlalchemy.create_engine(url)
    if engine.dialect.name == 'sqlite' and url.database != ':memory:':
        os.remove(url.database)
    else:
        text = 'DROP DATABASE {0}'.format(orm.quote(engine, database))
        cnx = engine.connect()
        cnx.execute("ROLLBACK")
        cnx.execute(text)
        cnx.execute("commit")
        cnx.close()
Exemple #4
0
def drop_database(url):
    url = copy(sqlalchemy.engine.url.make_url(url))
    database = url.database
    if url.drivername.startswith('postgresql'):
        url.database = 'postgres'
    elif not url.drivername.startswith('sqlite'):
        url.database = None

    engine = sqlalchemy.create_engine(url)
    if engine.dialect.name == 'sqlite' and url.database != ':memory:':
        os.remove(url.database)
    else:
        text = 'DROP DATABASE {0}'.format(orm.quote(engine, database))
        cnx = engine.connect()
        cnx.execute("ROLLBACK")
        cnx.execute(text)
        cnx.execute("commit")
        cnx.close()
def compile_asterisk(element, compiler, **kw):
    return '%s.*' % quote(compiler.dialect, element.selectable.name)
def compile_asterisk(element, compiler, **kw):
    return '%s.*' % quote(compiler.dialect, element.selectable.name)