Python sqlalchemy.create_engine() Examples

The following are code examples for showing how to use sqlalchemy.create_engine(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: hydrus   Author: HTTP-APIs   File: test_crud.py    MIT License 8 votes vote down vote up
def setUpClass(self):
        """Database setup before the CRUD tests."""
        print("Creating a temporary datatbsse...")
        engine = create_engine('sqlite:///:memory:')
        Base.metadata.create_all(engine)
        session = scoped_session(sessionmaker(bind=engine))
        self.API_NAME = "demoapi"
        self.HYDRUS_SERVER_URL = "http://hydrus.com/"
        self.session = session

        self.doc = doc_maker.create_doc(
            doc, self.HYDRUS_SERVER_URL, self.API_NAME)

        test_classes = doc_parse.get_classes(self.doc.generate())

        # Getting list of classes from APIDoc
        self.doc_collection_classes = [
            self.doc.collections[i]["collection"].class_.title for i in self.doc.collections]
        print(self.doc_collection_classes)
        print(random.choice(self.doc_collection_classes))
        test_properties = doc_parse.get_all_properties(test_classes)
        doc_parse.insert_classes(test_classes, self.session)
        doc_parse.insert_properties(test_properties, self.session)
        print("Classes and properties added successfully.")
        print("Setup done, running tests...") 
Example 2
Project: drydock   Author: airshipit   File: state.py    Apache License 2.0 8 votes vote down vote up
def connect_db(self):
        """Connect the state manager to the persistent DB."""
        self.db_engine = create_engine(
            config.config_mgr.conf.database.database_connect_string,
            pool_size=config.config_mgr.conf.database.pool_size,
            pool_pre_ping=config.config_mgr.conf.database.pool_pre_ping,
            max_overflow=config.config_mgr.conf.database.pool_overflow,
            pool_timeout=config.config_mgr.conf.database.pool_timeout,
            pool_recycle=config.config_mgr.conf.database.connection_recycle)
        self.db_metadata = MetaData(bind=self.db_engine)

        self.tasks_tbl = tables.Tasks(self.db_metadata)
        self.result_message_tbl = tables.ResultMessage(self.db_metadata)
        self.active_instance_tbl = tables.ActiveInstance(self.db_metadata)
        self.boot_action_tbl = tables.BootAction(self.db_metadata)
        self.ba_status_tbl = tables.BootActionStatus(self.db_metadata)
        self.build_data_tbl = tables.BuildData(self.db_metadata)
        return 
Example 3
Project: StructEngPy   Author: zhuoju36   File: db.py    MIT License 8 votes vote down vote up
def open(self,database):
    """
    params:
        database: str. Database to be opered. The path should be included
    """
    assert(database[-4:]=='.mdo')
    if not os.path.exists(database):
        self._create(database)
    operate_db=database[:-4]+'.op'
    shutil.copy(database,operate_db)
#        engine=create_engine('sqlite:///:memory:')
    engine=create_engine('sqlite:///'+operate_db) #should be run in memory in the future
    Session=o.sessionmaker(bind=engine)
    self.session=Session()
    self.__operate_db=operate_db
    self.__storage_db=database 
Example 4
Project: flasky   Author: RoseOu   File: schema.py    MIT License 8 votes vote down vote up
def bind(self):
        """An :class:`.Engine` or :class:`.Connection` to which this
        :class:`.MetaData` is bound.

        Typically, a :class:`.Engine` is assigned to this attribute
        so that "implicit execution" may be used, or alternatively
        as a means of providing engine binding information to an
        ORM :class:`.Session` object::

            engine = create_engine("someurl://")
            metadata.bind = engine

        .. seealso::

           :ref:`dbengine_implicit` - background on "bound metadata"

        """
        return self._bind 
Example 5
Project: flasky   Author: RoseOu   File: schema.py    MIT License 8 votes vote down vote up
def _bind_to(self, url, bind):
        """Bind to a Connectable in the caller's thread."""

        if isinstance(bind, util.string_types + (url.URL, )):
            try:
                self.context._engine = self.__engines[bind]
            except KeyError:
                e = sqlalchemy.create_engine(bind)
                self.__engines[bind] = e
                self.context._engine = e
        else:
            # TODO: this is squirrely.  we shouldn't have to hold onto engines
            # in a case like this
            if bind not in self.__engines:
                self.__engines[bind] = bind
            self.context._engine = bind 
Example 6
Project: flasky   Author: RoseOu   File: __init__.py    MIT License 8 votes vote down vote up
def get_engine(self):
        with self._lock:
            uri = self.get_uri()
            echo = self._app.config['SQLALCHEMY_ECHO']
            if (uri, echo) == self._connected_for:
                return self._engine
            info = make_url(uri)
            options = {'convert_unicode': True}
            self._sa.apply_pool_defaults(self._app, options)
            self._sa.apply_driver_hacks(self._app, info, options)
            if echo:
                options['echo'] = True
            self._engine = rv = sqlalchemy.create_engine(info, **options)
            if _record_queries(self._app):
                _EngineDebuggingSignalEvents(self._engine,
                                             self._app.import_name).register()
            self._connected_for = (uri, echo)
            return rv 
Example 7
Project: rowgenerators   Author: Metatab   File: sql.py    MIT License 8 votes vote down vote up
def __iter__(self):

        from sqlalchemy import create_engine
        from sqlalchemy.exc import  DatabaseError

        try:
            engine = create_engine(self.ref.dsn)
            connection = engine.connect()
        except DatabaseError as e:
            raise RowGeneratorError("Database connection failed for dsn '{}' : {} ".format(self.ref.dsn,str(e)))

        try:
            r = connection.execute(self.ref.sql)
        except DatabaseError as e:
            raise RowGeneratorError("Database query failed for dsn '{}' : {} ".format(self.ref.dsn,str(e) ))

        yield r.keys()

        yield from r 
Example 8
Project: vk-channelify   Author: reo7sp   File: env.py    MIT License 8 votes vote down vote up
def run_migrations_online():
    """Run migrations in 'online' mode.

    In this scenario we need to create an Engine
    and associate a connection with the context.

    """
    connectable = create_engine(os.getenv('DATABASE_URL'))

    with connectable.connect() as connection:
        context.configure(
            connection=connection,
            target_metadata=target_metadata
        )

        with context.begin_transaction():
            context.run_migrations() 
Example 9
Project: RTX   Author: RTXteam   File: KGNodeIndex.py    MIT License 8 votes vote down vote up
def createDatabase(self):
    if self.engine_type == "sqlite":
      if os.path.exists(self.databaseLocation + "/" + self.databaseName):
        if DEBUG is True: print("INFO: Removing previous database "+self.databaseLocation + "/" + self.databaseName)
        os.remove(self.databaseLocation + "/" + self.databaseName)

    if DEBUG is True: print("INFO: Creating database "+self.databaseName)
    if self.engine_type == "sqlite":
      engine = create_engine("sqlite:///"+self.databaseLocation + "/" + self.databaseName)
    else:
      rtxConfig = RTXConfiguration()
      engine = create_engine("mysql+pymysql://" + rtxConfig.mysql_feedback_username + ":" + rtxConfig.mysql_feedback_password + "@" + rtxConfig.mysql_feedback_host + "/" + self.databaseName)

    Base.metadata.create_all(engine)


  #### Create and store a database connection 
Example 10
Project: RTX   Author: RTXteam   File: KGNodeIndex.py    MIT License 8 votes vote down vote up
def connect(self):
    if self.session is not None: return
    if self.engine_type == "sqlite":
      if not os.path.isfile(self.databaseLocation + "/" + self.databaseName):
        self.createDatabase()

    #### Create an engine object
    if DEBUG is True: print("INFO: Connecting to database")
    if self.engine_type == "sqlite":
      engine = create_engine("sqlite:///"+self.databaseLocation+"/"+self.databaseName)
    else:
      rtxConfig = RTXConfiguration()
      engine = create_engine("mysql+pymysql://" + rtxConfig.mysql_feedback_username + ":" + rtxConfig.mysql_feedback_password + "@" + rtxConfig.mysql_feedback_host + "/" + self.databaseName)

    #### Create the session. This is weird syntax
    DBSession = sessionmaker(bind=engine)
    session = DBSession()

    #### Save these for later retrieval
    self.session = session
    self.engine = engine


  #### Destroy the database connection 
Example 11
Project: RTX   Author: RTXteam   File: sqlalchemy_ericTest1.py    MIT License 6 votes vote down vote up
def main():
  print("Creating database")
  engine = create_engine('sqlite:///sqlalchemy_example.db')
  Base.metadata.create_all(engine)

  print("Writing to database")
  DBSession = sessionmaker(bind=engine)
  session = DBSession()
  new_person = Person(name='Eric')
  session.add(new_person)
  session.commit()
  new_address = Address(post_code='98008', person=new_person)
  session.add(new_address)
  session.commit()

  print("Querying database")
  for person in session.query(Person).all():
    print(person)
    print("id="+str(person.id)+"  name="+person.name+"\n") 
Example 12
Project: Flask_Blog   Author: sugarguo   File: base.py    GNU General Public License v3.0 6 votes vote down vote up
def update_execution_options(self, **opt):
        """Update the default execution_options dictionary
        of this :class:`.Engine`.

        The given keys/values in \**opt are added to the
        default execution options that will be used for
        all connections.  The initial contents of this dictionary
        can be sent via the ``execution_options`` parameter
        to :func:`.create_engine`.

        .. seealso::

            :meth:`.Connection.execution_options`

            :meth:`.Engine.execution_options`

        """
        self._execution_options = \
            self._execution_options.union(opt)
        self.dispatch.set_engine_execution_options(self, opt)
        self.dialect.set_engine_execution_options(self, opt) 
Example 13
Project: Flask_Blog   Author: sugarguo   File: schema.py    GNU General Public License v3.0 6 votes vote down vote up
def bind(self):
        """An :class:`.Engine` or :class:`.Connection` to which this
        :class:`.MetaData` is bound.

        Typically, a :class:`.Engine` is assigned to this attribute
        so that "implicit execution" may be used, or alternatively
        as a means of providing engine binding information to an
        ORM :class:`.Session` object::

            engine = create_engine("someurl://")
            metadata.bind = engine

        .. seealso::

           :ref:`dbengine_implicit` - background on "bound metadata"

        """
        return self._bind 
Example 14
Project: Flask_Blog   Author: sugarguo   File: schema.py    GNU General Public License v3.0 6 votes vote down vote up
def _bind_to(self, url, bind):
        """Bind to a Connectable in the caller's thread."""

        if isinstance(bind, util.string_types + (url.URL, )):
            try:
                self.context._engine = self.__engines[bind]
            except KeyError:
                e = sqlalchemy.create_engine(bind)
                self.__engines[bind] = e
                self.context._engine = e
        else:
            # TODO: this is squirrely.  we shouldn't have to hold onto engines
            # in a case like this
            if bind not in self.__engines:
                self.__engines[bind] = bind
            self.context._engine = bind 
Example 15
Project: Flask_Blog   Author: sugarguo   File: __init__.py    GNU General Public License v3.0 6 votes vote down vote up
def get_engine(self):
        with self._lock:
            uri = self.get_uri()
            echo = self._app.config['SQLALCHEMY_ECHO']
            if (uri, echo) == self._connected_for:
                return self._engine
            info = make_url(uri)
            options = {'convert_unicode': True}
            self._sa.apply_pool_defaults(self._app, options)
            self._sa.apply_driver_hacks(self._app, info, options)
            if echo:
                options['echo'] = True
            self._engine = rv = sqlalchemy.create_engine(info, **options)
            if _record_queries(self._app):
                _EngineDebuggingSignalEvents(self._engine,
                                             self._app.import_name).register()
            self._connected_for = (uri, echo)
            return rv 
Example 16
Project: Pansidong   Author: lightless233   File: SaveData.py    GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, results_queue, thread_pool, use_file=True, use_database=True, filename="proxy-ip-list.csv"):
        self.use_file = use_file
        self.use_database = use_database
        self.filename = filename
        self.results_queue = results_queue
        self.thread_pool = thread_pool

        if use_database:
            try:
                cf = ConfigParser.ConfigParser()
                cf.read("config.ini")
                db_name = cf.get("Pansidong", "database")
                username = cf.get(db_name, "username")
                password = cf.get(db_name, "password")
                host = cf.get(db_name, "host")
                database = cf.get(db_name, "database")
            except AttributeError, e:
                logger.fatal(e.message)
                sys.exit(1)
            self.engine = create_engine("mysql://" + username + ":" + password + "@" +
                                        host + "/" + database + "?charset=utf8")
            self.db_session = sessionmaker(bind=self.engine)
            self.session = self.db_session() 
Example 17
Project: hydrus   Author: HTTP-APIs   File: test_app.py    MIT License 5 votes vote down vote up
def setUpClass(self):
        """Database setup before the tests."""
        print("Creating a temporary database...")
        engine = create_engine('sqlite:///:memory:')
        Base.metadata.create_all(engine)
        session = scoped_session(sessionmaker(bind=engine))

        self.session = session
        self.API_NAME = "demoapi"
        self.page_size = 1
        self.HYDRUS_SERVER_URL = "http://hydrus.com/"

        self.app = app_factory(self.API_NAME)
        self.socketio = create_socket(self.app, self.session)
        print("going for create doc")

        self.doc = doc_maker.create_doc(
            doc_writer_sample.api_doc.generate(),
            self.HYDRUS_SERVER_URL,
            self.API_NAME)
        test_classes = doc_parse.get_classes(self.doc.generate())
        test_properties = doc_parse.get_all_properties(test_classes)
        doc_parse.insert_classes(test_classes, self.session)
        doc_parse.insert_properties(test_properties, self.session)

        print("Classes and properties added successfully.")

        print("Setting up hydrus utilities... ")
        self.api_name_util = set_api_name(self.app, self.API_NAME)
        self.session_util = set_session(self.app, self.session)
        self.doc_util = set_doc(self.app, self.doc)
        self.page_size_util = set_page_size(self.app, self.page_size)
        self.client = self.app.test_client()

        print("Creating utilities context... ")
        self.api_name_util.__enter__()
        self.session_util.__enter__()
        self.doc_util.__enter__()
        self.client.__enter__()

        print("Setup done, running tests...") 
Example 18
Project: hydrus   Author: HTTP-APIs   File: test_cli.py    MIT License 5 votes vote down vote up
def setUpClass(self):
        """Database setup before the tests."""
        print("Creating a temporary database...")
        engine = create_engine('sqlite:///:memory:')
        Base.metadata.create_all(engine)
        session = scoped_session(sessionmaker(bind=engine))

        self.session = session
        self.API_NAME = "demoapi"
        self.HYDRUS_SERVER_URL = "http://hydrus.com/"

        self.app = app_factory(self.API_NAME)
        print("going for create doc")

        self.doc = doc_maker.create_doc(
            doc_writer_sample.api_doc.generate(),
            self.HYDRUS_SERVER_URL,
            self.API_NAME)
        test_classes = doc_parse.get_classes(self.doc.generate())
        test_properties = doc_parse.get_all_properties(test_classes)
        doc_parse.insert_classes(test_classes, self.session)
        doc_parse.insert_properties(test_properties, self.session)

        print("Classes and properties added successfully.")

        print("Setting up Hydrus utilities... ")
        self.api_name_util = set_api_name(self.app, self.API_NAME)
        self.session_util = set_session(self.app, self.session)
        self.doc_util = set_doc(self.app, self.doc)
        self.client = self.app.test_client()

        print("Creating utilities context... ")
        self.api_name_util.__enter__()
        self.session_util.__enter__()
        self.doc_util.__enter__()
        self.client.__enter__()

        print("Setup done, running tests...") 
Example 19
Project: BASS   Author: Cisco-Talos   File: server.py    GNU General Public License v2.0 5 votes vote down vote up
def main(args, env):
    global Session

    if args.verbose >= 1:
        app.config['DEBUG'] = True
    sys.stderr.write("connecting to DB server {:s}\n".format(args.db))
    connection_succeeded = False
    while not connection_succeeded:
        try:
            engine = create_engine(args.db)
            Session = sessionmaker(bind = engine)
            Base.metadata.create_all(engine)
            sys.stderr.write("connection succeeded!\n")
            connection_succeeded = True
            app.run(debug = args.verbose >= 1, host = "0.0.0.0", port = 80)
        except OperationalError as err:
            if "Connection refused" in str(err):
                connection_succeeded = False
                time.sleep(10)
            else:
                raise 
Example 20
Project: GeoLibs-Dator   Author: GeographicaGS   File: postgresql.py    MIT License 5 votes vote down vote up
def _connect(self):
        # Gets the postgresql options and creates the engine
        engine = create_engine(
            'postgresql://{user}:{password}@{host}:{port}/{db}'
            .format(**self.options['credentials'])
        )

        # Checks the schema and creates it if it doesn't exist
        with engine.connect() as connection:
            if not connection.dialect.has_schema(connection, self.options['data']['schema']):
                connection.execute(CreateSchema(self.options['data']['schema']))

        return engine 
Example 21
Project: StructEngPy   Author: zhuoju36   File: db.py    MIT License 5 votes vote down vote up
def create(self,database):
    """
    params:
        database: str. Database to be created. The path should be included
    """
    assert(database[-4:]=='.mdo')
    #initialize
    engine=create_engine('sqlite:///'+database)
    Base.metadata.create_all(engine)
    Session=o.sessionmaker(bind=engine)
    self.session=Session()
    
    #configurations
    config=Config()
    config.project_name='dev'
    config.author='HZJ'
    config.program_version='0.0.1'
    config.unit='N_m_C'
    config.create_time=datetime.now()                 
    self.session.add(config)
    self.session.commit()
    
    #default material and sections
    self.add_material('Q345',7849,'isotropic_elastic',E=2e11,mu=0.3)
    self.add_material('C35',2500,'isotropic_elastic',E=2e10,mu=0.2)
    self.add_frame_section('1-L-H400x200x14x20','Q345','I',[0.4,0.2,0.014,0.02])
    self.add_area_section('A-M120','C35','m',0.12)
    
    #default loadcase
    self.add_loadcase('S','static-linear',1.)
    self.session.commit()
    self.session.close() 
Example 22
Project: flasky   Author: RoseOu   File: base.py    MIT License 5 votes vote down vote up
def get_isolation_level(self):
        """Return the current isolation level assigned to this
        :class:`.Connection`.

        This will typically be the default isolation level as determined
        by the dialect, unless if the
        :paramref:`.Connection.execution_options.isolation_level`
        feature has been used to alter the isolation level on a
        per-:class:`.Connection` basis.

        This attribute will typically perform a live SQL operation in order
        to procure the current isolation level, so the value returned is the
        actual level on the underlying DBAPI connection regardless of how
        this state was set.  Compare to the
        :attr:`.Connection.default_isolation_level` accessor
        which returns the dialect-level setting without performing a SQL
        query.

        .. versionadded:: 0.9.9

        .. seealso::

            :attr:`.Connection.default_isolation_level` - view default level

            :paramref:`.create_engine.isolation_level`
            - set per :class:`.Engine` isolation level

            :paramref:`.Connection.execution_options.isolation_level`
            - set per :class:`.Connection` isolation level

        """
        try:
            return self.dialect.get_isolation_level(self.connection)
        except Exception as e:
            self._handle_dbapi_exception(e, None, None, None, None) 
Example 23
Project: flasky   Author: RoseOu   File: base.py    MIT License 5 votes vote down vote up
def default_isolation_level(self):
        """The default isolation level assigned to this :class:`.Connection`.

        This is the isolation level setting that the :class:`.Connection`
        has when first procured via the :meth:`.Engine.connect` method.
        This level stays in place until the
        :paramref:`.Connection.execution_options.isolation_level` is used
        to change the setting on a per-:class:`.Connection` basis.

        Unlike :meth:`.Connection.get_isolation_level`, this attribute is set
        ahead of time from the first connection procured by the dialect,
        so SQL query is not invoked when this accessor is called.

        .. versionadded:: 0.9.9

        .. seealso::

            :meth:`.Connection.get_isolation_level` - view current level

            :paramref:`.create_engine.isolation_level`
            - set per :class:`.Engine` isolation level

            :paramref:`.Connection.execution_options.isolation_level`
            - set per :class:`.Connection` isolation level

        """
        return self.dialect.default_isolation_level 
Example 24
Project: flasky   Author: RoseOu   File: base.py    MIT License 5 votes vote down vote up
def update_execution_options(self, **opt):
        """Update the default execution_options dictionary
        of this :class:`.Engine`.

        The given keys/values in \**opt are added to the
        default execution options that will be used for
        all connections.  The initial contents of this dictionary
        can be sent via the ``execution_options`` parameter
        to :func:`.create_engine`.

        .. seealso::

            :meth:`.Connection.execution_options`

            :meth:`.Engine.execution_options`

        """
        self._execution_options = \
            self._execution_options.union(opt)
        self.dispatch.set_engine_execution_options(self, opt)
        self.dialect.set_engine_execution_options(self, opt) 
Example 25
Project: flasky   Author: RoseOu   File: schema.py    MIT License 5 votes vote down vote up
def _bind_to(self, url, bind):
        """Bind this MetaData to an Engine, Connection, string or URL."""

        if isinstance(bind, util.string_types + (url.URL, )):
            self._bind = sqlalchemy.create_engine(bind)
        else:
            self._bind = bind 
Example 26
Project: flasky   Author: RoseOu   File: engines.py    MIT License 5 votes vote down vote up
def testing_engine(url=None, options=None):
    """Produce an engine configured by --options with optional overrides."""

    from sqlalchemy import create_engine
    from .assertsql import asserter

    if not options:
        use_reaper = True
    else:
        use_reaper = options.pop('use_reaper', True)

    url = url or config.db.url
    if options is None:
        options = config.db_opts

    engine = create_engine(url, **options)
    if isinstance(engine.pool, pool.QueuePool):
        engine.pool._timeout = 0
        engine.pool._max_overflow = 0
    event.listen(engine, 'after_execute', asserter.execute)
    event.listen(engine, 'after_cursor_execute', asserter.cursor_execute)
    if use_reaper:
        event.listen(engine.pool, 'connect', testing_reaper.connect)
        event.listen(engine.pool, 'checkout', testing_reaper.checkout)
        event.listen(engine.pool, 'invalidate', testing_reaper.invalidate)
        testing_reaper.add_engine(engine)

    return engine 
Example 27
Project: flasky   Author: RoseOu   File: engines.py    MIT License 5 votes vote down vote up
def mock_engine(dialect_name=None):
    """Provides a mocking engine based on the current testing.db.

    This is normally used to test DDL generation flow as emitted
    by an Engine.

    It should not be used in other cases, as assert_compile() and
    assert_sql_execution() are much better choices with fewer
    moving parts.

    """

    from sqlalchemy import create_engine

    if not dialect_name:
        dialect_name = config.db.name

    buffer = []

    def executor(sql, *a, **kw):
        buffer.append(sql)

    def assert_sql(stmts):
        recv = [re.sub(r'[\n\t]', '', str(s)) for s in buffer]
        assert recv == stmts, recv

    def print_sql():
        d = engine.dialect
        return "\n".join(
            str(s.compile(dialect=d))
            for s in engine.mock
        )

    engine = create_engine(dialect_name + '://',
                           strategy='mock', executor=executor)
    assert not hasattr(engine, 'mock')
    engine.mock = buffer
    engine.assert_sql = assert_sql
    engine.print_sql = print_sql
    return engine 
Example 28
Project: swaggerit   Author: dutradda   File: conftest.py    MIT License 5 votes vote down vote up
def engine(variables, pymysql_conn):
    if variables['database']['password']:
        url = 'mysql+pymysql://{user}:{password}'\
            '@{host}:{port}/{database}'.format(**variables['database'])
    else:
        variables['database'].pop('password')
        url = 'mysql+pymysql://{user}'\
            '@{host}:{port}/{database}'.format(**variables['database'])
        variables['database']['password'] = None

    return create_engine(url) 
Example 29
Project: MiEIBot   Author: claudioap   File: database.py    GNU General Public License v3.0 5 votes vote down vote up
def create_session_factory():
    engine = sa.create_engine('sqlite:////var/MiEIBot/database.db', echo=False)
    session_factory = sa.orm.sessionmaker(bind=engine)
    return sa.orm.scoped_session(session_factory)


# Models 
Example 30
Project: flask-request-logger   Author: BbsonLin   File: cli.py    MIT License 5 votes vote down vote up
def init_db(app, req_logger):
    from sqlalchemy import create_engine
    from flask_request_logger.database import Base
    from flask_request_logger.models import RequestLog, ResponseLog

    def create_db(db_engine, db_name):
        try:
            db_engine.execute('''CREATE DATABASE IF NOT EXISTS {} charset utf8mb4
                              COLLATE utf8mb4_general_ci'''.format(db_name))
            click.secho('Successfully create db "{}" ...'.format(db_name), fg='green')
        except Exception as e:
            click.secho('Failed to create db, Error: {}'.format(e), fg='red')
            exit(3)

    _req_logger = import_application(app, req_logger)

    click.secho('You\'re using "{}" engine. Your db name is "{}"'.format(
                    _req_logger.db_info.drivername, _req_logger.db_info.database), fg='blue')
    if 'mysql' in _req_logger.db_info.drivername:
        engine = create_engine(
            "{db_info.drivername}://{db_info.username}:{db_info.password}@{db_info.host}/".format(
                db_info=_req_logger.db_info), convert_unicode=True)
        create_db(engine, _req_logger.db_info.database)

    try:
        engine = create_engine(_req_logger.db_info, convert_unicode=True)
        Base.metadata.create_all(bind=engine)
        click.secho('Successfully initialize db ...', fg='green')
    except Exception as e:
        click.secho('Failed to initialize db, Error: {}'.format(e), fg='red')
        exit(3) 
Example 31
Project: flask-request-logger   Author: BbsonLin   File: request_logger.py    MIT License 5 votes vote down vote up
def _create_db_session(self, app):
        from sqlalchemy import create_engine
        from sqlalchemy.orm import scoped_session, sessionmaker
        from flask_request_logger.database import Base

        engine = create_engine(app.config['REQUEST_LOGGER_DB_URI'], convert_unicode=True)
        db_session = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=engine))
        Base.query = db_session.query_property()
        return db_session 
Example 32
Project: cis   Author: mozilla-iam   File: vault.py    Mozilla Public License 2.0 5 votes vote down vote up
def session(self):
        return create_engine(self._db_string()) 
Example 33
Project: cis   Author: mozilla-iam   File: vault.py    Mozilla Public License 2.0 5 votes vote down vote up
def find_or_create(self):
        try:
            self.table()
        except OperationalError:
            create_database(create_engine(self._db_string()).url)
            self.create()
            self.table()
        return self.table() 
Example 34
Project: testing.postgresql   Author: tk0miya   File: test_postgresql.py    Apache License 2.0 5 votes vote down vote up
def test_basic(self):
        try:
            # start postgresql server
            pgsql = testing.postgresql.Postgresql()
            self.assertIsNotNone(pgsql)
            params = pgsql.dsn()
            self.assertEqual('test', params['database'])
            self.assertEqual('127.0.0.1', params['host'])
            self.assertEqual(pgsql.settings['port'], params['port'])
            self.assertEqual('postgres', params['user'])

            # connect to postgresql (w/ psycopg2)
            conn = psycopg2.connect(**pgsql.dsn())
            self.assertIsNotNone(conn)
            self.assertRegexpMatches(pgsql.read_bootlog(), 'is ready to accept connections')
            conn.close()

            # connect to postgresql (w/ sqlalchemy)
            engine = sqlalchemy.create_engine(pgsql.url())
            self.assertIsNotNone(engine)

            # connect to postgresql (w/ pg8000)
            conn = pg8000.connect(**pgsql.dsn())
            self.assertIsNotNone(conn)
            self.assertRegexpMatches(pgsql.read_bootlog(), 'is ready to accept connections')
            conn.close()
        finally:
            # shutting down
            pid = pgsql.server_pid
            self.assertTrue(pgsql.is_alive())

            pgsql.stop()
            sleep(1)

            self.assertFalse(pgsql.is_alive())
            with self.assertRaises(OSError):
                os.kill(pid, 0)  # process is down 
Example 35
Project: botovod   Author: OlegYurchik   File: sqlalchemy.py    MIT License 5 votes vote down vote up
def connect(cls, engine: str, database: str, host: Optional[Union[str, int]]=None,
                username: Optional[str]=None, password: Optional[str]=None, debug: bool=False):

        dsn = f"{engine}://"
        if username is not None and password is not None:
            dsn += f"{username}:{password}@"
        if host is not None:
            dsn += f"{host}/"
        dsn += database
        cls.engine = create_engine(dsn, echo=debug)
        cls.metadata = Base.metadata
        cls.metadata.create_all(cls.engine)
        Session = sessionmaker()
        Session.configure(bind=cls.engine)
        cls.session = Session() 
Example 36
Project: openhatch   Author: campbe13   File: session.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def get_engine(dburi, **kwargs):
    if dburi not in _ENGINES:
        _ENGINES[dburi] = create_engine(dburi, **kwargs)
    return _ENGINES[dburi] 
Example 37
Project: hoergewohnheiten   Author: mymindwentblvnk   File: models.py    MIT License 5 votes vote down vote up
def __init__(self):
        if POSTGRES_ENVIRON_KEY in os.environ:
            self.engine = create_engine(os.environ[POSTGRES_ENVIRON_KEY])
        else:
            import settings
            self.engine = create_engine(settings.POSTGRES_CONNECTION_STRING)
        self.session = sessionmaker(autoflush=False)(bind=self.engine) 
Example 38
Project: windflow   Author: hartym   File: db.py    Apache License 2.0 5 votes vote down vote up
def __init__(self):
        if not self.dsn:
            raise AttributeError('DSN is required.')
        self.engine = self.create_engine()
        self.sessionmaker = self.create_sessionmaker(self.engine)
        self.load() 
Example 39
Project: windflow   Author: hartym   File: db.py    Apache License 2.0 5 votes vote down vote up
def create_engine(self):
        return self.engine_factory(self.dsn, **self.engine_factory_options) 
Example 40
Project: adh6   Author: bonnetn   File: database.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, db_settings, testing=False):
        self.testing = testing

        self.engine = create_engine(
            URL(**db_settings),
            isolation_level="SERIALIZABLE",  # As we don't have a lot of IO, we can afford the highest isolation.
            echo=False,  # Echo the SQL queries if testing. NOTE: removed, this breaks the tests
            pool_pre_ping=True,  # Make sure the connection is OK before using it.
        )

        # noinspection PyArgumentEqualDefault
        self.session_maker = sessionmaker(
            bind=self.engine,
            autoflush=True,  # Flush at each query so that we can see our changes in the current session.
            autocommit=False,  # Never auto-commit, we want proper transactions!
        )

        if testing:
            self.session_maker = scoped_session(self.session_maker)

            # If testing, drop all tables & recreate them
            Base.metadata.drop_all(self.engine)
            Base.metadata.create_all(self.engine)

        else:
            # Create NainA table if not exists. (The other tables should already exist.)
            for retries in range(Database.RETRY_COUNT):
                try:
                    LOG.info("connecting_to_database")
                    Base.metadata.create_all(
                        self.engine,
                        tables=[NainA.__table__]
                    )
                    LOG.info("connection_established")
                    LOG.info("table_created")
                except OperationalError as e:
                    LOG.warning("could_not_connect_to_database", extra={'exception': e})
                    if retries + 1 == Database.RETRY_COUNT:
                        raise
                    time.sleep(Database.RETRY_INTERVAL) 
Example 41
Project: password_pwncheck   Author: CboeSecurity   File: pwnpass.py    MIT License 5 votes vote down vote up
def __init__(self,cfg,debug=False):
        dburl =  vars(cfg).get('dburl',None)
        if dburl == None:
            dburl = 'sqlite:///%s'%(cfg.dbpath)
        engine = create_engine(dburl, convert_unicode=True)
        self.dbsession = scoped_session(sessionmaker(autocommit=False,
                                                 autoflush=False,
                                                 bind=engine))
        Base.metadata.reflect(bind=engine)
        Base.query = self.dbsession.query_property()    
        Base.metadata.create_all(engine)
        self.debug = debug 
Example 42
Project: vk-channelify   Author: reo7sp   File: __init__.py    MIT License 5 votes vote down vote up
def make_session_maker(url):
    engine = create_engine(url)
    return sessionmaker(bind=engine) 
Example 43
Project: telethon-session-sqlalchemy   Author: tulir   File: sqlalchemy.py    MIT License 5 votes vote down vote up
def __init__(self, engine: Union[sql.engine.Engine, str] = None,
                 session: Optional[Union[orm.Session, scoped_session, bool]] = None,
                 table_prefix: str = "", table_base: Optional[declarative_base] = None,
                 manage_tables: bool = True) -> None:
        if isinstance(engine, str):
            engine = sql.create_engine(engine)

        self.db_engine = engine
        if session is None:
            db_factory = orm.sessionmaker(bind=self.db_engine)
            self.db = orm.scoping.scoped_session(db_factory)
        elif not session:
            self.db = None
        else:
            self.db = session

        table_base = table_base or declarative_base()
        (self.Version, self.Session, self.Entity,
         self.SentFile, self.UpdateState) = self.create_table_classes(self.db, table_prefix,
                                                                      table_base)
        self.alchemy_session_class = AlchemySession
        if not self.db:
            # Implicit core mode if there's no ORM session.
            self.core_mode = True

        if manage_tables:
            if not self.db:
                raise ValueError("Can't manage tables without an ORM session.")
            table_base.metadata.bind = self.db_engine
            if not self.db_engine.dialect.has_table(self.db_engine,
                                                    self.Version.__tablename__):
                table_base.metadata.create_all()
                self.db.add(self.Version(version=LATEST_VERSION))
                self.db.commit()
            else:
                self.check_and_upgrade_database() 
Example 44
Project: wok   Author: bbglab   File: db.py    GNU General Public License v3.0 5 votes vote down vote up
def create_engine(uri, drop_tables=False):
	engine = sqlalchemy.create_engine(uri, connect_args=dict(timeout=18000, check_same_thread=False))
	Session.configure(bind=engine)
	if drop_tables:
		Base.metadata.drop_all(engine)
	Base.metadata.create_all(engine)
	return engine

# Model -------------------------------------------------------------- 
Example 45
Project: wok   Author: bbglab   File: db.py    GNU General Public License v3.0 5 votes vote down vote up
def create_engine(uri, drop_tables=False):
	engine = sqlalchemy.create_engine(uri, convert_unicode=True,
						   connect_args=dict(timeout=6, check_same_thread=False))
	Session.configure(bind=engine)
	Base.query = Session.query_property()
	if drop_tables:
		Base.metadata.drop_all(engine)
	Base.metadata.create_all(engine)
	return engine 
Example 46
Project: wok   Author: bbglab   File: db.py    GNU General Public License v3.0 5 votes vote down vote up
def create_engine(uri):
	engine = sqlalchemy.create_engine(uri, connect_args=dict(timeout=1800, check_same_thread=False))
	#Session.configure(bind=engine)
	Base.metadata.create_all(engine)
	return engine 
Example 47
Project: RTX   Author: RTXteam   File: RTXFeedback.py    MIT License 5 votes vote down vote up
def createDatabase(self):
    print("Creating database")
    #if os.path.exists(self.databaseName):
    #  os.remove(self.databaseName)
    #engine = create_engine("sqlite:///"+self.databaseName)
    rtxConfig = RTXConfiguration()
    engine = create_engine("mysql+pymysql://" + rtxConfig.mysql_feedback_username + ":" + rtxConfig.mysql_feedback_password + "@" + rtxConfig.mysql_feedback_host + "/" + self.databaseName)
    Base.metadata.create_all(engine)
    self.connect()

  #### Create and store a database connection 
Example 48
Project: RTX   Author: RTXteam   File: RTXFeedback.py    MIT License 5 votes vote down vote up
def connect(self):
    #engine = create_engine("sqlite:///"+self.databaseName)
    rtxConfig = RTXConfiguration()
    engine = create_engine("mysql+pymysql://" + rtxConfig.mysql_feedback_username + ":" + rtxConfig.mysql_feedback_password + "@" + rtxConfig.mysql_feedback_host + "/" + self.databaseName)
    DBSession = sessionmaker(bind=engine)
    session = DBSession()
    self.session = session
    self.engine = engine

  #### Create and store a database connection 
Example 49
Project: zun   Author: openstack   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _is_backend_avail(backend, user, passwd, database):
    try:
        connect_uri = _get_connect_string(backend, user, passwd, database)
        engine = sqlalchemy.create_engine(connect_uri)
        connection = engine.connect()
    except Exception:
        # intentionally catch all to handle exceptions even if we don't
        # have any backend code loaded.
        return False
    else:
        connection.close()
        engine.dispose()
        return True 
Example 50
Project: hatteberg   Author: Shinichi-Nakagawa   File: retrosheet_controller.py    MIT License 5 votes vote down vote up
def __init__(self, config_file='config.ini', database_engine='mysql'):
        config = ConfigParser()
        config.read(config_file)
        params = dict(config[database_engine])
        connection = "{dialect}+{driver}://{user}:{password}@{host}:{port}/{database}".format(**params)
        encoding = params.get('encoding')
        self.engine = create_engine(connection, encoding=encoding)
        Session = sessionmaker()
        Session.configure(bind=self.engine)
        self.session = Session()
        self.conn = self.engine.connect() 
Example 51
Project: Python-RESTful-API   Author: HawkEleven   File: pymysql01.py    Apache License 2.0 5 votes vote down vote up
def connectMysql(self):
        engine = create_engine('mysql+mysqlconnector://root:123@localhost:3306/home')
        # 创建DBSession类型:
        DBSession = sessionmaker(bind=engine)
        # 创建session对象:
        self.session = DBSession()
        self.insert_id = 0 
Example 52
Project: RedditImageDownloader   Author: sarvothaman   File: database.py    GNU General Public License v3.0 5 votes vote down vote up
def setup_database(self):
        try :
            #self.engine.execute("CREATE DATABASE IF NOT EXISTS "+ self.dbparam.database)
            self.status=3
        except DatabaseError as e:
            messageString=e.orig
            messageString=format(messageString)
            messageList=messageString.split(':',1)
            if messageList[0] == "1007":
                self.status=3
            else:
                self.status=-3
                return self.status
    #self.url=URL(self.dbparam.driver,self.dbparam.database+".db")
        try:
            self.engine=create_engine(self.url)
        except DatabaseError as e:
            self.status=-4
            return self.status
        self.status=4
        try:
            model.tables.Base.metadata.create_all(self.engine)
        except DatabaseError as e:
            self.status=-5
            return self.status
        self.status=5
        self.sessionHolder=sessionmaker(bind=self.engine)
        self.session=self.sessionHolder()
        return self.status 
Example 53
Project: RedditImageDownloader   Author: sarvothaman   File: database.py    GNU General Public License v3.0 5 votes vote down vote up
def connect_db(self):

        self.url=URL(self.dbparam.driver,database=self.dbparam.database)
        try:
            self.engine = create_engine(self.url)
        except Exception as e:
            self.status=-2
        self.status=2 
Example 54
Project: zamia-prolog   Author: gooofy   File: logicdb.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def __init__(self, db_url, echo=False):

        self.engine  = create_engine(db_url, echo=echo)
        self.Session = sessionmaker(bind=self.engine)
        self.session = self.Session()
        model.Base.metadata.create_all(self.engine)
        self.cache = {} 
Example 55
Project: vault   Author: gabfl   File: base.py    MIT License 5 votes vote down vote up
def get_engine(encrypted=True):
    """
        return SQLAlchemy engine
    """

    if global_scope['db_file'] is None:
        raise RuntimeError('`db_file` is not defined in the global scope')

    if encrypted:
        return create_engine('sqlite+pysqlcipher://:' + get_db_key() + '@' + get_slashes() + global_scope['db_file'])
    else:
        return create_engine('sqlite:' + get_slashes() + global_scope['db_file']) 
Example 56
Project: DRCOG_Urbansim   Author: apdjustino   File: database_server.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def open(self, creating_base_database = False):

        if not creating_base_database:
            self.protocol_manager.create_default_database_if_absent(self.config)
            connect_string = self.get_connection_string()
        else:
            connect_string = self.get_connection_string(get_base_db = True)
            
        self.engine = create_engine(connect_string, connect_args={})
        self.metadata = MetaData(
            bind = self.engine
        ) 
Example 57
Project: DRCOG_Urbansim   Author: apdjustino   File: opus_database.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def open(self):
        self.protocol_manager.create_default_database_if_absent(self.database_server_config)

        self.engine = create_engine(self.get_connection_string())
        self.metadata = MetaData(
            bind = self.engine
        )  
        
        self.reflect(recurse = False) 
Example 58
Project: dino   Author: thenetcircle   File: dbman.py    Apache License 2.0 5 votes vote down vote up
def db_connect(self):
        """
        Performs database connection using database settings from settings.py.
        Returns sqlalchemy engine instance
        """
        domain = ConfigKeys.DATABASE
        params = {
            'drivername': self.driver,
        }

        host = self.env.config.get(ConfigKeys.HOST, default=None, domain=domain)
        port = self.env.config.get(ConfigKeys.PORT, default=None, domain=domain)
        username = self.env.config.get(ConfigKeys.USER, default=None, domain=domain)
        password = self.env.config.get(ConfigKeys.PASSWORD, default=None, domain=domain)
        database = self.env.config.get(ConfigKeys.DB, default=None, domain=domain)
        pool_size = self.env.config.get(ConfigKeys.POOL_SIZE, default=75, domain=domain)

        if host is not None and host != '':
            params['host'] = host
        if port is not None and port != '':
            params['port'] = port
        if username is not None and username != '':
            params['username'] = username
        if password is not None and password != '':
            params['password'] = password
        if database is not None and database != '':
            params['database'] = database

        return create_engine(URL(**params), pool_recycle=280, pool_size=pool_size) 
Example 59
Project: Octojobs   Author: OctoJobs   File: pipelines.py    MIT License 5 votes vote down vote up
def db_connect():
    """Connect to database; return sqlalchemy engine instance."""
    return create_engine(URL(**DATABASE)) 
Example 60
Project: scrapy-proxy-spiders   Author: MartiONE   File: models.py    GNU General Public License v3.0 5 votes vote down vote up
def db_connect():
    return create_engine(URL(**ProxyFetcher.settings.DATABASE)) 
Example 61
Project: pugsql   Author: mcfunley   File: compiler.py    Apache License 2.0 5 votes vote down vote up
def connect(self, connstr):
        """
        Sets the connection string for SQL functions on this module.

        See https://docs.sqlalchemy.org/en/13/core/engines.html for examples of
        legal connection strings for different databases.
        """
        self.set_engine(create_engine(connstr)) 
Example 62
Project: Flask_Blog   Author: sugarguo   File: base.py    GNU General Public License v3.0 5 votes vote down vote up
def get_isolation_level(self):
        """Return the current isolation level assigned to this
        :class:`.Connection`.

        This will typically be the default isolation level as determined
        by the dialect, unless if the
        :paramref:`.Connection.execution_options.isolation_level`
        feature has been used to alter the isolation level on a
        per-:class:`.Connection` basis.

        This attribute will typically perform a live SQL operation in order
        to procure the current isolation level, so the value returned is the
        actual level on the underlying DBAPI connection regardless of how
        this state was set.  Compare to the
        :attr:`.Connection.default_isolation_level` accessor
        which returns the dialect-level setting without performing a SQL
        query.

        .. versionadded:: 0.9.9

        .. seealso::

            :attr:`.Connection.default_isolation_level` - view default level

            :paramref:`.create_engine.isolation_level`
            - set per :class:`.Engine` isolation level

            :paramref:`.Connection.execution_options.isolation_level`
            - set per :class:`.Connection` isolation level

        """
        try:
            return self.dialect.get_isolation_level(self.connection)
        except Exception as e:
            self._handle_dbapi_exception(e, None, None, None, None) 
Example 63
Project: Flask_Blog   Author: sugarguo   File: base.py    GNU General Public License v3.0 5 votes vote down vote up
def default_isolation_level(self):
        """The default isolation level assigned to this :class:`.Connection`.

        This is the isolation level setting that the :class:`.Connection`
        has when first procured via the :meth:`.Engine.connect` method.
        This level stays in place until the
        :paramref:`.Connection.execution_options.isolation_level` is used
        to change the setting on a per-:class:`.Connection` basis.

        Unlike :meth:`.Connection.get_isolation_level`, this attribute is set
        ahead of time from the first connection procured by the dialect,
        so SQL query is not invoked when this accessor is called.

        .. versionadded:: 0.9.9

        .. seealso::

            :meth:`.Connection.get_isolation_level` - view current level

            :paramref:`.create_engine.isolation_level`
            - set per :class:`.Engine` isolation level

            :paramref:`.Connection.execution_options.isolation_level`
            - set per :class:`.Connection` isolation level

        """
        return self.dialect.default_isolation_level 
Example 64
Project: Flask_Blog   Author: sugarguo   File: schema.py    GNU General Public License v3.0 5 votes vote down vote up
def _bind_to(self, url, bind):
        """Bind this MetaData to an Engine, Connection, string or URL."""

        if isinstance(bind, util.string_types + (url.URL, )):
            self._bind = sqlalchemy.create_engine(bind)
        else:
            self._bind = bind 
Example 65
Project: Flask_Blog   Author: sugarguo   File: engines.py    GNU General Public License v3.0 5 votes vote down vote up
def testing_engine(url=None, options=None):
    """Produce an engine configured by --options with optional overrides."""

    from sqlalchemy import create_engine
    from sqlalchemy.engine.url import make_url

    if not options:
        use_reaper = True
    else:
        use_reaper = options.pop('use_reaper', True)

    url = url or config.db.url

    url = make_url(url)
    if options is None:
        if config.db is None or url.drivername == config.db.url.drivername:
            options = config.db_opts
        else:
            options = {}

    engine = create_engine(url, **options)
    engine._has_events = True   # enable event blocks, helps with profiling

    if isinstance(engine.pool, pool.QueuePool):
        engine.pool._timeout = 0
        engine.pool._max_overflow = 0
    if use_reaper:
        event.listen(engine.pool, 'connect', testing_reaper.connect)
        event.listen(engine.pool, 'checkout', testing_reaper.checkout)
        event.listen(engine.pool, 'invalidate', testing_reaper.invalidate)
        testing_reaper.add_engine(engine)

    return engine 
Example 66
Project: Flask_Blog   Author: sugarguo   File: engines.py    GNU General Public License v3.0 5 votes vote down vote up
def mock_engine(dialect_name=None):
    """Provides a mocking engine based on the current testing.db.

    This is normally used to test DDL generation flow as emitted
    by an Engine.

    It should not be used in other cases, as assert_compile() and
    assert_sql_execution() are much better choices with fewer
    moving parts.

    """

    from sqlalchemy import create_engine

    if not dialect_name:
        dialect_name = config.db.name

    buffer = []

    def executor(sql, *a, **kw):
        buffer.append(sql)

    def assert_sql(stmts):
        recv = [re.sub(r'[\n\t]', '', str(s)) for s in buffer]
        assert recv == stmts, recv

    def print_sql():
        d = engine.dialect
        return "\n".join(
            str(s.compile(dialect=d))
            for s in engine.mock
        )

    engine = create_engine(dialect_name + '://',
                           strategy='mock', executor=executor)
    assert not hasattr(engine, 'mock')
    engine.mock = buffer
    engine.assert_sql = assert_sql
    engine.print_sql = print_sql
    return engine 
Example 67
Project: Flask_Blog   Author: sugarguo   File: replay_fixture.py    GNU General Public License v3.0 5 votes vote down vote up
def test_invocation(self):

        dbapi_session = ReplayableSession()
        creator = config.db.pool._creator
        recorder = lambda: dbapi_session.recorder(creator())
        engine = create_engine(
            config.db.url, creator=recorder,
            use_native_hstore=False)
        self.metadata = MetaData(engine)
        self.engine = engine
        self.session = Session(engine)

        self.setup_engine()
        try:
            self._run_steps(ctx=self._dummy_ctx)
        finally:
            self.teardown_engine()
            engine.dispose()

        player = lambda: dbapi_session.player()
        engine = create_engine(
            config.db.url, creator=player,
            use_native_hstore=False)

        self.metadata = MetaData(engine)
        self.engine = engine
        self.session = Session(engine)

        self.setup_engine()
        try:
            self._run_steps(ctx=profiling.count_functions)
        finally:
            self.session.close()
            engine.dispose() 
Example 68
Project: Flask_Blog   Author: sugarguo   File: fixtures.py    GNU General Public License v3.0 5 votes vote down vote up
def capture_db():
    buf = []

    def dump(sql, *multiparams, **params):
        buf.append(str(sql.compile(dialect=engine.dialect)))
    engine = create_engine("postgresql://", strategy="mock", executor=dump)
    return engine, buf 
Example 69
Project: Flask_Blog   Author: sugarguo   File: engines.py    GNU General Public License v3.0 5 votes vote down vote up
def testing_engine(url=None, options=None):
    """Produce an engine configured by --options with optional overrides."""

    from sqlalchemy import create_engine

    url = url or config.db.url
    if options is None:
        options = config.db_opts

    engine = create_engine(url, **options)

    return engine 
Example 70
Project: Pansidong   Author: lightless233   File: DBConnection.py    GNU General Public License v3.0 5 votes vote down vote up
def connect(self):
        if self._type.upper() == DB_TYPE.MYSQL:
            self._engine = create_engine(
                "mysql://" + self._username + ":" + self._password + "@" +
                self._host + "/" + self._database + "?charset=utf8"
            )
            self._db_session = sessionmaker(bind=self._engine)
            self.session = self._db_session()
        else:
            logger.fatal("Unsupported database type.")
            sys.exit(1) 
Example 71
Project: firebase-alchemy   Author: newpro   File: conftest.py    MIT License 5 votes vote down vote up
def engine(request):
    DATABASE_URL=DB_URL
    engine = create_engine(DATABASE_URL)
    def teardown():
        from _util import drop_db
        drop_db(DATABASE_URL)
    request.addfinalizer(teardown)
    return engine 
Example 72
Project: insights_connexion   Author: RedHatInsights   File: sqlalchemy_base.py    MIT License 5 votes vote down vote up
def init():
    global session

    engine = create_engine(
        'postgresql://{}:{}@{}:{}/{}'.format(config.db_user, config.db_password,
                                             config.db_host, config.db_port, config.db_name),
        pool_size=int(config.db_pool_size),
        max_overflow=int(config.db_max_overflow))

    session = scoped_session(sessionmaker(
        autocommit=False, autoflush=False, bind=engine))
    Base.query = session.query_property()
    Base.metadata.create_all(engine)
    return session 
Example 73
Project: hydrus   Author: HTTP-APIs   File: test_auth.py    MIT License 4 votes vote down vote up
def setUpClass(self):
        """Database setup before the tests."""
        print("Creating a temporary datatbase...")
        engine = create_engine('sqlite:///:memory:')
        Base.metadata.create_all(engine)
        session = scoped_session(sessionmaker(bind=engine))

        self.session = session
        self.API_NAME = "demoapi"
        self.HYDRUS_SERVER_URL = "http://hydrus.com/"
        self.app = app_factory(self.API_NAME)
        self.doc = doc_maker.create_doc(
            doc_writer_sample.api_doc.generate(), self.HYDRUS_SERVER_URL, self.API_NAME)

        test_classes = doc_parse.get_classes(self.doc.generate())
        test_properties = doc_parse.get_all_properties(test_classes)
        doc_parse.insert_classes(test_classes, self.session)
        doc_parse.insert_properties(test_properties, self.session)
        add_user(1, "test", self.session)
        self.auth_header = {"X-Authentication": "",
                            "Authorization": "Basic {}".format(
                                b64encode(b"1:test").decode("ascii"))}
        self.wrong_id = {"X-Authentication": "",
                         "Authorization": "Basic {}".format(b64encode(b"2:test").decode("ascii"))}
        self.wrong_pass = {"X-Authentication": "",
                           "Authorization": "Basic {}".format(
                               b64encode(b"1:test2").decode("ascii"))}
        print("Classes, Properties and Users added successfully.")

        print("Setting up Hydrus utilities... ")
        self.api_name_util = set_api_name(self.app, self.API_NAME)
        self.session_util = set_session(self.app, self.session)
        self.doc_util = set_doc(self.app, self.doc)
        self.auth_util = set_authentication(self.app, True)
        self.token_util = set_token(self.app, False)
        self.client = self.app.test_client()

        print("Creating utilities context... ")
        self.api_name_util.__enter__()
        self.session_util.__enter__()
        self.doc_util.__enter__()
        self.auth_util.__enter__()
        self.token_util.__enter__()
        self.client.__enter__()

        print("Setup done, running tests...") 
Example 74
Project: hydrus   Author: HTTP-APIs   File: test_app.py    MIT License 4 votes vote down vote up
def setUpClass(self):
        """Database setup before the tests."""
        print("Creating a temporary database...")
        engine = create_engine('sqlite:///:memory:')
        Base.metadata.create_all(engine)
        session = scoped_session(sessionmaker(bind=engine))

        self.session = session
        self.API_NAME = "demoapi"
        self.page_size = 1
        self.HYDRUS_SERVER_URL = "http://hydrus.com/"

        self.app = app_factory(self.API_NAME)
        self.socketio = create_socket(self.app, self.session)
        print("going for create doc")

        self.doc = doc_maker.create_doc(
            doc_writer_sample.api_doc.generate(),
            self.HYDRUS_SERVER_URL,
            self.API_NAME)
        test_classes = doc_parse.get_classes(self.doc.generate())
        test_properties = doc_parse.get_all_properties(test_classes)
        doc_parse.insert_classes(test_classes, self.session)
        doc_parse.insert_properties(test_properties, self.session)

        print("Classes and properties added successfully.")

        print("Setting up hydrus utilities... ")
        self.api_name_util = set_api_name(self.app, self.API_NAME)
        self.session_util = set_session(self.app, self.session)
        self.doc_util = set_doc(self.app, self.doc)
        self.page_size_util = set_page_size(self.app, self.page_size)
        self.client = self.app.test_client()
        self.socketio_client = self.socketio.test_client(self.app, namespace='/sync')

        print("Creating utilities context... ")
        self.api_name_util.__enter__()
        self.session_util.__enter__()
        self.doc_util.__enter__()
        self.client.__enter__()

        print("Setup done, running tests...") 
Example 75
Project: flasky   Author: RoseOu   File: __init__.py    MIT License 4 votes vote down vote up
def apply_driver_hacks(self, app, info, options):
        """This method is called before engine creation and used to inject
        driver specific hacks into the options.  The `options` parameter is
        a dictionary of keyword arguments that will then be used to call
        the :func:`sqlalchemy.create_engine` function.

        The default implementation provides some saner defaults for things
        like pool sizes for MySQL and sqlite.  Also it injects the setting of
        `SQLALCHEMY_NATIVE_UNICODE`.
        """
        if info.drivername.startswith('mysql'):
            info.query.setdefault('charset', 'utf8')
            if info.drivername != 'mysql+gaerdbms':
                options.setdefault('pool_size', 10)
                options.setdefault('pool_recycle', 7200)
        elif info.drivername == 'sqlite':
            pool_size = options.get('pool_size')
            detected_in_memory = False
            # we go to memory and the pool size was explicitly set to 0
            # which is fail.  Let the user know that
            if info.database in (None, '', ':memory:'):
                detected_in_memory = True
                if pool_size == 0:
                    raise RuntimeError('SQLite in memory database with an '
                                       'empty queue not possible due to data '
                                       'loss.')
            # if pool size is None or explicitly set to 0 we assume the
            # user did not want a queue for this sqlite connection and
            # hook in the null pool.
            elif not pool_size:
                from sqlalchemy.pool import NullPool
                options['poolclass'] = NullPool

            # if it's not an in memory database we make the path absolute.
            if not detected_in_memory:
                info.database = os.path.join(app.root_path, info.database)

        unu = app.config['SQLALCHEMY_NATIVE_UNICODE']
        if unu is None:
            unu = self.use_native_unicode
        if not unu:
            options['use_native_unicode'] = False 
Example 76
Project: restful-ben   Author: CityOfPhiladelphia   File: app_fixtures.py    MIT License 4 votes vote down vote up
def app():
    connection_string = os.getenv('SQLALCHEMY_DATABASE_URI', 'postgresql://localhost/restful_ben_test')

    app = flask.Flask(__name__)
    app.config['DEBUG'] = True
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    app.config['SQLALCHEMY_DATABASE_URI'] = connection_string

    db.init_app(app)
    api = Api(app)

    ## hack to prevent loading the Token and AuthLogEntry models multiple times. Only an issue for tests
    token_model = None
    auth_log_entry_model = None
    for cls in BaseModel._decl_class_registry.values():
        if hasattr(cls, '__name__'):
            if cls.__name__ == 'Token':
                token_model = cls
            elif cls.__name__ == 'AuthLogEntry':
                auth_log_entry_model = cls

    auth = AuthStandalone(
                app=app,
                session=db.session,
                base_model=BaseModel,
                token_model=token_model,
                auth_log_entry_model=auth_log_entry_model,
                user_model=User,
                token_secret=Fernet.generate_key())

    with app.app_context():
        db.create_all()

        ## seed users
        db.session.add(User(active=True, email='amadonna@example.com', password='foo', role='admin'))
        db.session.add(User(active=True, email='jdoe@example.com', password='icecream', role='normal'))
        db.session.add(User(active=True, email='kclarkson@example.com', password='icecream', role='normal'))
        db.session.add(User(active=True, email='whouston@example.com', password='icecream', role='admin'))
        db.session.commit()

        ## seed cats
        db.session.add(Cat(name='Ada', pattern='Tabby', age=5))
        db.session.add(Cat(name='Leo', pattern='Tabby', age=2))
        db.session.add(Cat(name='Wilhelmina', pattern='Calico', age=4))
        db.session.commit()

        api.add_resource(UserListResource, '/users')
        api.add_resource(UserResource, '/users/<int:instance_id>')
        api.add_resource(auth.session_resource, '/session')
        api.add_resource(CatListResource, '/cats')
        api.add_resource(CatResource, '/cats/<int:instance_id>')

    yield app

    engine = create_engine(connection_string)
    BaseModel.metadata.drop_all(engine) 
Example 77
Project: neutron   Author: kaloom   File: test_plugin.py    Apache License 2.0 4 votes vote down vote up
def setUp(self):
        super(KaloomL3ServicePluginTestCase, self).setUp()
        self.mysqld = testing.mysqld.Mysqld(my_cnf={'skip-networking': None})
        URL = create_engine(self.mysqld.url()).url

        oslo_cfg.CONF.set_override('connection', URL, group='database')

        #"neutron-db-manage upgrade head" equivalent
        alembic_cfg = migration_cli.get_neutron_config()
        alembic_cfg.neutron_config = oslo_cfg.CONF
        test_helper.upgrade(alembic_cfg)

        #"neutron-kaloom-db-manage upgrade head" equivalent
        script_location = 'networking_kaloom.ml2.drivers.kaloom.db.migration:alembic_migrations'
        alembic_cfg_kaloom = alembic_config.Config()
        alembic_cfg_kaloom.set_main_option("script_location", script_location)
        alembic_cfg_kaloom.neutron_config = oslo_cfg.CONF
        test_helper.upgrade(alembic_cfg_kaloom)

        with patch.object(neutron_context.db_api,'_CTX_MANAGER', test_helper.get_context_manager(URL)):
           self.ctx = neutron_context.get_admin_context()
           self.ctx.session
        #print self.ctx.session.connection().engine

        self.setup_coreplugin(load_plugins=False)
        oslo_cfg.CONF.set_override("core_plugin", test_db_base_plugin_v2.DB_PLUGIN_KLASS)
        oslo_cfg.CONF.set_override("service_plugins", ['segments'])
        manager.init()
        self.l2_plugin = directory.get_plugin()
        self.segments_plugin = importutils.import_object('neutron.services.segments.plugin.Plugin')

        #patch
        plugin.db_api._CTX_MANAGER = test_helper.get_context_manager(URL)
        plugin.kaloom_db.db_api.context_manager = test_helper.get_context_manager(URL)
        db_api.context_manager = test_helper.get_context_manager(URL)

        #fix l3_port_check issue
        def delete_port(context, id, l3_port_check=True):
            return original_delete_port(context, id)

        original_delete_port = self.l2_plugin.delete_port
        self.l2_plugin.delete_port = Mock(side_effect = delete_port) 
Example 78
Project: DRCOG_Urbansim   Author: apdjustino   File: postgis_create_spatial_index.py    GNU Affero General Public License v3.0 4 votes vote down vote up
def opusRun(progressCB,logCB,params):
    param_dict = {}
    for key, val in params.iteritems():
        param_dict[str(key)] = str(val)
        
    # TODO:
    #    - automatically get geometry column name (probably requires custom type in sqlalchemy)
    #    - more error checking and messages
    
    # get parameter values
    database_name = param_dict['database_name']
    schema = param_dict['schema']
    table_name = param_dict['table_name']
    geometry_column_name = param_dict['geometry_column_name']
    run_vacuum_analyze = param_dict['run_vacuum_analyze']
    index_name = table_name + '_geom_indx'
    database_server_connection = param_dict['database_server_connection']

    # create engine and connection
    logCB("Openeing database connection\n")
    dbs_config = DatabaseServerConfiguration(database_configuration=database_server_connection)
    connection_string = str(dbs_config) + '/%s' % (database_name) 
    engine = create_engine(connection_string)
    connection = engine.connect()
    
    # set up queries
    # drop index if exists
    query1 = '''DROP INDEX IF EXISTS %s.%s;''' % (schema, index_name)
    # create the new index
    query2 = '''CREATE INDEX %s ON %s.%s USING GIST (%s);''' % (index_name, schema, table_name, geometry_column_name)
    
    queries = [query1, query2]
    
    # execute queries
    for query in queries:
        logCB("Running query:\n")
        logCB("%s\n" % query)
        connection.execute(query)
    
    # update database statistics
    if run_vacuum_analyze == 'True':
        logCB("Running vacuum\n")
        import psycopg2.extensions
        connection.connection.connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
        query = '''VACUUM ANALYZE;'''
        connection.execute(query)
    
    #close connection
    logCB("Closing database connection\n")
    connection.close()
    logCB('Finished creating spatial index on %s\n' % table_name) 
Example 79
Project: firebase-alchemy   Author: newpro   File: conftest.py    MIT License 4 votes vote down vote up
def _schema(engine):
    """setup test schema, this is only available for other fixtures
    """
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.orm import relationship
    from sqlalchemy import (
        Column,
        Integer,
        String,
        ForeignKey,
        Table,
        UniqueConstraint,
        create_engine
    )
    from firebase_alchemy.mixin import FireMix
    Base = declarative_base()
    user_chats = Table('chat_map',
                       Base.metadata,
                       Column('index', Integer, primary_key=True),
                       Column('user_fk', Integer, ForeignKey('users.id', ondelete='SET NULL')),
                       Column('chat_fk', Integer, ForeignKey('chats.id', ondelete='SET NULL')))

    class User(Base):
        __tablename__ = 'users'
        id = Column(Integer, primary_key=True)
        name = Column(String, unique=True)
        chats = relationship('Chat',
                             secondary=user_chats,
                             lazy='dynamic')

    class Chat(Base, FireMix):
        __tablename__ = 'chats'
        id = Column(Integer, primary_key=True)
        name = Column(String)
        users = relationship('User',
                             secondary=user_chats,
                             lazy='dynamic')

    class Dummy(Base, FireMix):
        __tablename__ = 'dummys'
        id = Column(Integer, primary_key=True)
        sql_data = Column(String)

    # setup
    Base.metadata.create_all(engine)
    return {'user': User, 'chat': Chat, 'dummy': Dummy} 
Example 80
Project: firebase-alchemy   Author: newpro   File: _util.py    MIT License 4 votes vote down vote up
def drop_db(url):
    # -- setup --
    from sqlalchemy import create_engine
    engine = create_engine(url)

    # -- drop --
    """
    For current version of sql-alchemy, have two issues with Postgres: 
    Postgres Lock When call drop_all, and also does not drop in cascade order.
    This function is a workaround.
    
    Credit http://www.sqlalchemy.org/trac/wiki/UsageRecipes/DropEverything
    """
    print '---- DROP START ----'
    from sqlalchemy.engine import reflection
    from sqlalchemy.schema import (
        MetaData,
        Table,
        DropTable,
        ForeignKeyConstraint,
        DropConstraint,
    )
    conn=engine.connect()
    trans = conn.begin()
    inspector = reflection.Inspector.from_engine(engine)
    metadata = MetaData()
    tbs = []
    all_fks = []
    for table_name in inspector.get_table_names():
        fks = []
        for fk in inspector.get_foreign_keys(table_name):
            if not fk['name']:
                continue
            fks.append(
                ForeignKeyConstraint((),(),name=fk['name'])
            )
        t = Table(table_name,metadata,*fks)
        tbs.append(t)
        all_fks.extend(fks)

    for fkc in all_fks:
        conn.execute(DropConstraint(fkc))
    for table in tbs:
        conn.execute(DropTable(table))
    trans.commit()
    print ' -- DROP END --'