Python sqlalchemy.engine.url.make_url() Examples

The following are 30 code examples of sqlalchemy.engine.url.make_url(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sqlalchemy.engine.url , or try the search function .
Example #1
Source File: strategies.py    From jbox with MIT License 8 votes vote down vote up
def create(self, name_or_url, executor, **kwargs):
        # create url.URL object
        u = url.make_url(name_or_url)

        dialect_cls = u.get_dialect()

        dialect_args = {}
        # consume dialect arguments from kwargs
        for k in util.get_cls_kwargs(dialect_cls):
            if k in kwargs:
                dialect_args[k] = kwargs.pop(k)

        # create dialect
        dialect = dialect_cls(**dialect_args)

        return MockEngineStrategy.MockConnection(dialect, executor) 
Example #2
Source File: _db.py    From assembly with MIT License 6 votes vote down vote up
def connect__(self, uri, app):
        self.uri = uri
        self.info = sa_make_url(uri)
        self.options = self._cleanup_options(
            echo=False,
            pool_size=None,
            pool_timeout=None,
            pool_recycle=None,
            convert_unicode=True,
        )

        self._initialized = True
        self._IS_OK_ = True
        self.connector = None
        self._engine_lock = active_alchemy.threading.Lock()
        self.session = active_alchemy._create_scoped_session(self, query_cls=active_alchemy.BaseQuery)
        self.Model.db, self.BaseModel.db = self, self
        self.Model._query, self.BaseModel._query = self.session.query, self.session.query
        self.init_app(app)


# ------------------------------------------------------------------------------
# StorageObjectType 
Example #3
Source File: test_base.py    From a10-neutron-lbaas with Apache License 2.0 6 votes vote down vote up
def setUp(self):
        self._undo = []
        url = os.getenv("PIFPAF_URL")
        if not url:
            self.skipTest("No database URL set")

        sa_url = make_url(url)

        if sa_url.drivername == 'mysql':
            sa_url.drivername = 'mysql+pymysql'

        initial_engine = sqlalchemy.create_engine(str(sa_url))
        initial_connection = initial_engine.connect()
        self._undo.append(initial_connection.close)
        initial_connection.execute("CREATE DATABASE a10_test_db")
        self._undo.append(lambda: initial_engine.execute("DROP DATABASE a10_test_db"))

        sa_url.database = 'a10_test_db'
        self.engine = sqlalchemy.create_engine(str(sa_url))
        self.connection = self.engine.connect()
        self._undo.append(self.connection.close) 
Example #4
Source File: __init__.py    From planespotter with MIT License 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'] = echo
            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 #5
Source File: provision.py    From oslo.db with Apache License 2.0 6 votes vote down vote up
def provisioned_database_url(self, base_url, ident):
        """Return a provisioned database URL.

        Given the URL of a particular database backend and the string
        name of a particular 'database' within that backend, return
        an URL which refers directly to the named database.

        For hostname-based URLs, this typically involves switching just the
        'database' portion of the URL with the given name and creating
        an engine.

        For URLs that instead deal with DSNs, the rules may be more custom;
        for example, the engine may need to connect to the root URL and
        then emit a command to switch to the named database.

        """

        url = sa_url.make_url(str(base_url))
        url.database = ident
        return url 
Example #6
Source File: config.py    From AnyBlok with Mozilla Public License 2.0 6 votes vote down vote up
def get_db_name():
    """Return an sqlalchemy name of the database from configuration

    db_name or db_url

    :rtype: name of the database
    :exception: ConfigurationException
    """
    url = Configuration.get('db_url', None)
    db_name = Configuration.get('db_name', None)

    if db_name is not None:
        return db_name

    if url:
        url = make_url(url)
        if url.database:
            return url.database

    raise ConfigurationException("No database name defined") 
Example #7
Source File: strategies.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def create(self, name_or_url, executor, **kwargs):
        # create url.URL object
        u = url.make_url(name_or_url)

        dialect_cls = u.get_dialect()

        dialect_args = {}
        # consume dialect arguments from kwargs
        for k in util.get_cls_kwargs(dialect_cls):
            if k in kwargs:
                dialect_args[k] = kwargs.pop(k)

        # create dialect
        dialect = dialect_cls(**dialect_args)

        return MockEngineStrategy.MockConnection(dialect, executor) 
Example #8
Source File: database.py    From quay with Apache License 2.0 6 votes vote down vote up
def validate_database_precondition(url, db_kwargs, connect_timeout=5):
    """
    Validates that we can connect to the given database URL and the database meets our precondition.

    Raises an exception if the validation fails.
    """
    db_kwargs = db_kwargs.copy()
    try:
        driver = _db_from_url(
            url, db_kwargs, connect_timeout=connect_timeout, allow_retry=False, allow_pooling=False
        )
        driver.connect()
        pre_condition_check = PRECONDITION_VALIDATION.get(make_url(url).drivername)
        if pre_condition_check:
            pre_condition_check(driver)

    finally:
        try:
            driver.close()
        except:
            pass 
Example #9
Source File: __init__.py    From jbox with MIT License 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 #10
Source File: test_sqlalchemy.py    From oslo.db with Apache License 2.0 6 votes vote down vote up
def test_sqlite_memory_pool_args(self):
        for _url in ("sqlite://", "sqlite:///:memory:"):
            engines._init_connection_args(
                url.make_url(_url), self.args,
                max_pool_size=10, max_overflow=10)

            # queuepool arguments are not peresnet
            self.assertNotIn(
                'pool_size', self.args)
            self.assertNotIn(
                'max_overflow', self.args)

            self.assertEqual(False,
                             self.args['connect_args']['check_same_thread'])

            # due to memory connection
            self.assertIn('poolclass', self.args) 
Example #11
Source File: plugin.py    From daf-recipes with GNU General Public License v3.0 6 votes vote down vote up
def _read_connection_has_correct_privileges(self):
        ''' Returns True if the right permissions are set for the read
        only user. A table is created by the write user to test the
        read only user.
        '''
        write_connection = db._get_engine(
            {'connection_url': self.write_url}).connect()
        read_connection_user = sa_url.make_url(self.read_url).username

        drop_foo_sql = u'DROP TABLE IF EXISTS _foo'

        write_connection.execute(drop_foo_sql)

        try:
            write_connection.execute(u'CREATE TEMP TABLE _foo ()')
            for privilege in ['INSERT', 'UPDATE', 'DELETE']:
                test_privilege_sql = u"SELECT has_table_privilege(%s, '_foo', %s)"
                have_privilege = write_connection.execute(
                    test_privilege_sql, (read_connection_user, privilege)).first()[0]
                if have_privilege:
                    return False
        finally:
            write_connection.execute(drop_foo_sql)
            write_connection.close()
        return True 
Example #12
Source File: strategies.py    From pyRevit with GNU General Public License v3.0 6 votes vote down vote up
def create(self, name_or_url, executor, **kwargs):
        # create url.URL object
        u = url.make_url(name_or_url)

        dialect_cls = u.get_dialect()

        dialect_args = {}
        # consume dialect arguments from kwargs
        for k in util.get_cls_kwargs(dialect_cls):
            if k in kwargs:
                dialect_args[k] = kwargs.pop(k)

        # create dialect
        dialect = dialect_cls(**dialect_args)

        return MockEngineStrategy.MockConnection(dialect, executor) 
Example #13
Source File: strategies.py    From planespotter with MIT License 6 votes vote down vote up
def create(self, name_or_url, executor, **kwargs):
        # create url.URL object
        u = url.make_url(name_or_url)

        dialect_cls = u.get_dialect()

        dialect_args = {}
        # consume dialect arguments from kwargs
        for k in util.get_cls_kwargs(dialect_cls):
            if k in kwargs:
                dialect_args[k] = kwargs.pop(k)

        # create dialect
        dialect = dialect_cls(**dialect_args)

        return MockEngineStrategy.MockConnection(dialect, executor) 
Example #14
Source File: base.py    From geomancer with MIT License 6 votes vote down vote up
def __init__(self, dburl, options=None):
        """Initialize the database core

        Parameters
        ----------
        dburl : str
            Database url used to configure backend connection
        options : :class:`geomancer.backend.settings.Config`, optional
            Specify configuration for interacting with the database backend.
            Auto-detected if not set.
        """
        self.dburl = make_url(dburl)
        if not options:
            options = {"bigquery": BQConfig(), "sqlite": SQLiteConfig()}[
                self.dburl.get_backend_name()
            ]
        self.options = options 
Example #15
Source File: base.py    From geomancer with MIT License 6 votes vote down vote up
def get_core(self, dburl):
        """Instantiates an appropriate core based on given database url

        Parameters
        ----------
        dburl : str
            Database url used to configure backend connection

        Returns
        -------
        core : :code:`geomancer.backend.cores.DBCore`
            DBCore instance to access DB-specific methods
        """
        name = make_url(dburl).get_backend_name()
        Core = CORES[name]
        return Core(dburl, self.options) 
Example #16
Source File: strategies.py    From stdm with GNU General Public License v2.0 6 votes vote down vote up
def create(self, name_or_url, executor, **kwargs):
        # create url.URL object
        u = url.make_url(name_or_url)

        dialect_cls = u.get_dialect()

        dialect_args = {}
        # consume dialect arguments from kwargs
        for k in util.get_cls_kwargs(dialect_cls):
            if k in kwargs:
                dialect_args[k] = kwargs.pop(k)

        # create dialect
        dialect = dialect_cls(**dialect_args)

        return MockEngineStrategy.MockConnection(dialect, executor) 
Example #17
Source File: engines.py    From pyRevit with 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 = {}
    elif config.db is not None and url.drivername == config.db.url.drivername:
        default_opt = config.db_opts.copy()
        default_opt.update(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 #18
Source File: test_sqlalchemy.py    From oslo.db with Apache License 2.0 5 votes vote down vote up
def test_mysql_mysqldb_connect_args_default(self):
        engines._init_connection_args(
            url.make_url("mysql+mysqldb://u:p@host/test"), self.args)
        self._test_mysql_connect_args_default(self.args['connect_args']) 
Example #19
Source File: test_sqlalchemy.py    From oslo.db with Apache License 2.0 5 votes vote down vote up
def test_sqlite_file_pool_args(self):
        engines._init_connection_args(
            url.make_url("sqlite:///somefile.db"), self.args,
            max_pool_size=10, max_overflow=10)

        # queuepool arguments are not peresnet
        self.assertNotIn('pool_size', self.args)
        self.assertNotIn(
            'max_overflow', self.args)

        self.assertFalse(self.args['connect_args'])

        # NullPool is the default for file based connections,
        # no need to specify this
        self.assertNotIn('poolclass', self.args) 
Example #20
Source File: test_sqlalchemy.py    From oslo.db with Apache License 2.0 5 votes vote down vote up
def test_mysql_pymysql_connect_args_default(self):
        engines._init_connection_args(
            url.make_url("mysql+pymysql://u:p@host/test"), self.args)
        self.assertEqual({'charset': 'utf8'}, self.args['connect_args']) 
Example #21
Source File: test_sqlalchemy.py    From oslo.db with Apache License 2.0 5 votes vote down vote up
def test_mysql_connect_args_default(self):
        engines._init_connection_args(
            url.make_url("mysql://u:p@host/test"), self.args)
        self._test_mysql_connect_args_default(self.args['connect_args']) 
Example #22
Source File: db.py    From opensips-cli with GNU General Public License v3.0 5 votes vote down vote up
def get_url_driver(url, capitalize=False):
        if capitalize:
            driver = make_url(url).drivername.lower()
            capitalized = {
                'mysql': 'MySQL',
                'postgres': 'PostgreSQL',
                'sqlite': 'SQLite',
                'oracle': 'Oracle',
                }
            return capitalized.get(driver, driver.capitalize())
        else:
            return make_url(url).drivername.lower() 
Example #23
Source File: provision.py    From planespotter with MIT License 5 votes vote down vote up
def _oracle_follower_url_from_main(url, ident):
    url = sa_url.make_url(url)
    url.username = ident
    url.password = 'xe'
    return url 
Example #24
Source File: provision.py    From planespotter with MIT License 5 votes vote down vote up
def _sqlite_follower_url_from_main(url, ident):
    url = sa_url.make_url(url)
    if not url.database or url.database == ':memory:':
        return url
    else:
        return sa_url.make_url("sqlite:///%s.db" % ident) 
Example #25
Source File: provision.py    From planespotter with MIT License 5 votes vote down vote up
def _follower_url_from_main(url, ident):
    url = sa_url.make_url(url)
    url.database = ident
    return url 
Example #26
Source File: provision.py    From planespotter with MIT License 5 votes vote down vote up
def __call__(self, cfg, *arg):
        if isinstance(cfg, compat.string_types):
            url = sa_url.make_url(cfg)
        elif isinstance(cfg, sa_url.URL):
            url = cfg
        else:
            url = cfg.db.url
        backend = url.get_backend_name()
        if backend in self.fns:
            return self.fns[backend](cfg, *arg)
        else:
            return self.fns['*'](cfg, *arg) 
Example #27
Source File: engines.py    From planespotter with 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 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 = {}
    elif config.db is not None and url.drivername == config.db.url.drivername:
        default_opt = config.db_opts.copy()
        default_opt.update(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 #28
Source File: db.py    From opensips-cli with GNU General Public License v3.0 5 votes vote down vote up
def get_url_pswd(url):
        return make_url(url).password 
Example #29
Source File: db.py    From opensips-cli with GNU General Public License v3.0 5 votes vote down vote up
def get_url_user(url):
        return make_url(url).username 
Example #30
Source File: db.py    From hivemind with MIT License 5 votes vote down vote up
def init(self, url):
        """Initialize the aiopg.sa engine."""
        conf = make_url(url)
        self.db = await create_engine(user=conf.username,
                                      database=conf.database,
                                      password=conf.password,
                                      host=conf.host,
                                      port=conf.port,
                                      maxsize=20,
                                      **conf.query)