Python sqlalchemy.engine.base.Engine() Examples

The following are 30 code examples of sqlalchemy.engine.base.Engine(). 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.base , or try the search function .
Example #1
Source File: env.py    From android_universal with MIT License 6 votes vote down vote up
def run_migrations_offline():
    """Run migrations in 'offline' mode.

    This configures the context with just a URL
    and not an Engine, though an Engine is acceptable
    here as well.  By skipping the Engine creation
    we don't even need a DBAPI to be available.

    Calls to context.execute() here emit the given string to the
    script output.

    """
    context.configure(
        url=meta.engine.url, target_metadata=target_metadata,
        literal_binds=True)
    with context.begin_transaction():
        context.run_migrations() 
Example #2
Source File: hive.py    From incubator-superset with Apache License 2.0 6 votes vote down vote up
def select_star(  # pylint: disable=too-many-arguments
        cls,
        database: "Database",
        table_name: str,
        engine: Engine,
        schema: Optional[str] = None,
        limit: int = 100,
        show_cols: bool = False,
        indent: bool = True,
        latest_partition: bool = True,
        cols: Optional[List[Dict[str, Any]]] = None,
    ) -> str:
        return super(  # pylint: disable=bad-super-call
            PrestoEngineSpec, cls
        ).select_star(
            database,
            table_name,
            engine,
            schema,
            limit,
            show_cols,
            indent,
            latest_partition,
            cols,
        ) 
Example #3
Source File: sql_connection_manager.py    From hummingbot with Apache License 2.0 6 votes vote down vote up
def get_db_engine(cls, 
                      dialect: str, 
                      params: dict) -> Engine:
        # Fallback to `sqlite` if dialect is None
        if dialect is None:
            dialect = "sqlite"

        if "sqlite" in dialect:
            db_path = params.get("db_path")

            return create_engine(f"{dialect}:///{db_path}")
        else:
            username = params.get("db_username")
            password = params.get("db_password")
            host = params.get("db_host")
            port = params.get("db_port")
            db_name = params.get("db_name")

            return create_engine(f"{dialect}://{username}:{password}@{host}:{port}/{db_name}") 
Example #4
Source File: sqlalchemy_bigquery.py    From pybigquery with MIT License 6 votes vote down vote up
def get_table_names(self, connection, schema=None, **kw):
        if isinstance(connection, Engine):
            connection = connection.connect()

        datasets = connection.connection._client.list_datasets()
        result = []
        for d in datasets:
            if schema is not None and d.dataset_id != schema:
                continue

            if self.dataset_id is not None and d.dataset_id != self.dataset_id:
                continue

            tables = connection.connection._client.list_tables(d.reference)
            for t in tables:
                if self.dataset_id is None:
                    table_name = d.dataset_id + '.' + t.table_id
                else:
                    table_name = t.table_id
                result.append(table_name)
        return result 
Example #5
Source File: env.py    From android_universal with MIT License 6 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.

    """
    # specify here how the engine is acquired
    # engine = meta.engine
    raise NotImplementedError("Please specify engine connectivity here")

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

        with context.begin_transaction():
            context.run_migrations() 
Example #6
Source File: sqlalchemy_bigquery.py    From pybigquery with MIT License 6 votes vote down vote up
def _get_table(self, connection, table_name, schema=None):
        if isinstance(connection, Engine):
            connection = connection.connect()

        project, dataset, table_name_prepared = self._split_table_name(table_name)
        if dataset is None:
            if schema is not None:
                dataset = schema
            elif self.dataset_id:
                dataset = self.dataset_id

        table = connection.connection._client.dataset(dataset, project=project).table(table_name_prepared)
        try:
            t = connection.connection._client.get_table(table)
        except NotFound as e:
            raise NoSuchTableError(table_name)
        return t 
Example #7
Source File: env.py    From jbox with MIT License 6 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.

    """
    # specify here how the engine is acquired
    # engine = meta.engine
    raise NotImplementedError("Please specify engine connectivity here")

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

        with context.begin_transaction():
            context.run_migrations() 
Example #8
Source File: env.py    From jbox with MIT License 6 votes vote down vote up
def run_migrations_offline():
    """Run migrations in 'offline' mode.

    This configures the context with just a URL
    and not an Engine, though an Engine is acceptable
    here as well.  By skipping the Engine creation
    we don't even need a DBAPI to be available.

    Calls to context.execute() here emit the given string to the
    script output.

    """
    context.configure(
        url=meta.engine.url, target_metadata=target_metadata,
        literal_binds=True)
    with context.begin_transaction():
        context.run_migrations() 
Example #9
Source File: db.py    From maubot with GNU Affero General Public License v3.0 6 votes vote down vote up
def init(config: Config) -> Engine:
    db = sql.create_engine(config["database"])
    Base.metadata.bind = db

    for table in (DBPlugin, DBClient):
        table.bind(db)

    if not db.has_table("alembic_version"):
        log = logging.getLogger("maubot.db")

        if db.has_table("client") and db.has_table("plugin"):
            log.warning("alembic_version table not found, but client and plugin tables found. "
                        "Assuming pre-Alembic database and inserting version.")
            db.execute("CREATE TABLE IF NOT EXISTS alembic_version ("
                       "    version_num VARCHAR(32) PRIMARY KEY"
                       ");")
            db.execute("INSERT INTO alembic_version VALUES ('d295f8dcfa64');")
        else:
            log.critical("alembic_version table not found. "
                         "Did you forget to `alembic upgrade head`?")
            sys.exit(10)

    return db 
Example #10
Source File: tracker_store.py    From rasa-for-botfront with Apache License 2.0 6 votes vote down vote up
def _create_database(engine: "Engine", db: Text):
        """Create database `db` on `engine` if it does not exist."""

        import psycopg2

        conn = engine.connect()

        cursor = conn.connection.cursor()
        cursor.execute("COMMIT")
        cursor.execute(f"SELECT 1 FROM pg_catalog.pg_database WHERE datname = '{db}'")
        exists = cursor.fetchone()
        if not exists:
            try:
                cursor.execute(f"CREATE DATABASE {db}")
            except psycopg2.IntegrityError as e:
                logger.error(f"Could not create database '{db}': {e}")

        cursor.close()
        conn.close() 
Example #11
Source File: extended_declarative_base.py    From uszipcode-project with MIT License 5 votes vote down vote up
def ensure_session(engine_or_session):
    """
    If it is an engine, then create a session from it. And indicate that
    this session should be closed after the job done.
    """
    if isinstance(engine_or_session, Engine):
        ses = sessionmaker(bind=engine_or_session)()
        auto_close = True
    elif isinstance(engine_or_session, Session):
        ses = engine_or_session
        auto_close = False
    return ses, auto_close 
Example #12
Source File: base_test_dbcore.py    From geomancer with MIT License 5 votes vote down vote up
def test_get_engine_return_type(self, core):
        """Test if get_engine() returns appropriate type"""
        engine = core.get_engine()
        assert isinstance(engine, Engine) 
Example #13
Source File: __init__.py    From mautrix-telegram with GNU Affero General Public License v3.0 5 votes vote down vote up
def init(db_engine: Engine) -> None:
    for table in (Portal, Message, User, Contact, UserPortal, Puppet, TelegramFile, UserProfile,
                  RoomState, BotChat):
        table.db = db_engine
        table.t = table.__table__
        table.c = table.t.c
        table.column_names = table.c.keys()
    if init_nio_db:
        init_nio_db(db_engine) 
Example #14
Source File: db.py    From gitlab with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, db: Engine) -> None:
        self.db = db
        Base.metadata.create_all(db)
        self.Session = sessionmaker(bind=self.db) 
Example #15
Source File: test_query.py    From sqlalchemy with MIT License 5 votes vote down vote up
def _dialect_fixture(self):
        class MyDialect(default.DefaultDialect):
            default_paramstyle = "qmark"

        from sqlalchemy.engine import base

        return base.Engine(mock.Mock(), MyDialect(), mock.Mock()) 
Example #16
Source File: base.py    From incubator-superset with Apache License 2.0 5 votes vote down vote up
def get_engine(
        cls,
        database: "Database",
        schema: Optional[str] = None,
        source: Optional[str] = None,
    ) -> Engine:
        user_name = utils.get_username()
        return database.get_sqla_engine(
            schema=schema, nullpool=True, user_name=user_name, source=source
        ) 
Example #17
Source File: base.py    From incubator-superset with Apache License 2.0 5 votes vote down vote up
def extra_table_metadata(
        cls, database: "Database", table_name: str, schema_name: str
    ) -> Dict[str, Any]:
        """
        Returns engine-specific table metadata

        :param database: Database instance
        :param table_name: Table name
        :param schema_name: Schema name
        :return: Engine-specific table metadata
        """
        # TODO: Fix circular import caused by importing Database
        return {} 
Example #18
Source File: presto.py    From incubator-superset with Apache License 2.0 5 votes vote down vote up
def select_star(  # pylint: disable=too-many-arguments
        cls,
        database: "Database",
        table_name: str,
        engine: Engine,
        schema: Optional[str] = None,
        limit: int = 100,
        show_cols: bool = False,
        indent: bool = True,
        latest_partition: bool = True,
        cols: Optional[List[Dict[str, Any]]] = None,
    ) -> str:
        """
        Include selecting properties of row objects. We cannot easily break arrays into
        rows, so render the whole array in its own row and skip columns that correspond
        to an array's contents.
        """
        cols = cols or []
        presto_cols = cols
        if is_feature_enabled("PRESTO_EXPAND_DATA") and show_cols:
            dot_regex = r"\.(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"
            presto_cols = [
                col for col in presto_cols if not re.search(dot_regex, col["name"])
            ]
        return super().select_star(
            database,
            table_name,
            engine,
            schema,
            limit,
            show_cols,
            indent,
            latest_partition,
            presto_cols,
        ) 
Example #19
Source File: strategies.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def create(self, *args, **kwargs):
        """Given arguments, returns a new Engine instance."""

        raise NotImplementedError() 
Example #20
Source File: notify.py    From pgnotify with The Unlicense 5 votes vote down vote up
def get_dbapi_connection(x):
    if isinstance(x, string_types):
        c = psycopg2.connect(x)
        x = c
    elif sqlalchemy and isinstance(x, Engine):
        sqla_connection = x.connect()
        sqla_connection.execution_options(isolation_level="AUTOCOMMIT")
        sqla_connection.detach()
        x = sqla_connection.connection.connection
    else:
        pass
    x.autocommit = True
    return x 
Example #21
Source File: strategies.py    From stdm with GNU General Public License v2.0 5 votes vote down vote up
def create(self, *args, **kwargs):
        """Given arguments, returns a new Engine instance."""

        raise NotImplementedError() 
Example #22
Source File: strategies.py    From moviegrabber with GNU General Public License v3.0 5 votes vote down vote up
def create(self, *args, **kwargs):
        """Given arguments, returns a new Engine instance."""

        raise NotImplementedError() 
Example #23
Source File: sql_connection_manager.py    From hummingbot with Apache License 2.0 5 votes vote down vote up
def engine(self) -> Engine:
        return self._engine 
Example #24
Source File: strategies.py    From android_universal with MIT License 5 votes vote down vote up
def create(self, *args, **kwargs):
        """Given arguments, returns a new Engine instance."""

        raise NotImplementedError() 
Example #25
Source File: test_PersistentStoreConnectionSetting.py    From tethys with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_get_value_as_engine(self):
        ps_cs_setting = self.test_app.settings_set.select_subclasses().get(name='primary')
        ps_cs_setting.persistent_store_service = self.pss
        ps_cs_setting.save()

        # Execute
        ret = PersistentStoreConnectionSetting.objects.get(name='primary').get_value(as_engine=True)

        # Check if ret is an instance of sqlalchemy Engine
        self.assertIsInstance(ret, Engine)
        self.assertEqual('postgresql://foo:password@localhost:5432', str(ret.url)) 
Example #26
Source File: test_PersistentStoreDatabaseSetting.py    From tethys with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_get_value_as_engine(self):
        ps_ds_setting = self.test_app.settings_set.select_subclasses().get(name='spatial_db')
        ps_ds_setting.persistent_store_service = self.pss
        ps_ds_setting.save()

        # Execute
        ret = self.test_app.settings_set.select_subclasses().get(name='spatial_db').get_value(as_engine=True)

        self.assertIsInstance(ret, Engine)
        self.assertEqual(self.expected_url, str(ret.url)) 
Example #27
Source File: nio_state_store.py    From mautrix-python with Mozilla Public License 2.0 5 votes vote down vote up
def init(db_engine: Engine) -> None:
    for table in (DBAccount, DBOlmSession, DBDeviceKey, DBMegolmInboundSession,
                  DBOutgoingKeyRequest):
        table.db = db_engine
        table.t = table.__table__
        table.c = table.t.c
        table.column_names = table.c.keys() 
Example #28
Source File: plugin_base.py    From maubot with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, client: 'MaubotMatrixClient', loop: AbstractEventLoop, http: ClientSession,
                 instance_id: str, log: Logger, config: Optional['BaseProxyConfig'],
                 database: Optional[Engine], webapp: Optional['PluginWebApp'],
                 webapp_url: Optional[str]) -> None:
        self.client = client
        self.loop = loop
        self.http = http
        self.id = instance_id
        self.log = log
        self.config = config
        self.database = database
        self.webapp = webapp
        self.webapp_url = URL(webapp_url) if webapp_url else None
        self._handlers_at_startup = [] 
Example #29
Source File: strategies.py    From jbox with MIT License 5 votes vote down vote up
def create(self, *args, **kwargs):
        """Given arguments, returns a new Engine instance."""

        raise NotImplementedError() 
Example #30
Source File: sqlalchemy_bigquery.py    From pybigquery with MIT License 5 votes vote down vote up
def get_schema_names(self, connection, **kw):
        if isinstance(connection, Engine):
            connection = connection.connect()

        datasets = connection.connection._client.list_datasets()
        if self.dataset_id is not None:
            return [d.dataset_id for d in datasets if d.dataset_id == self.dataset_id]
        else:
            return [d.dataset_id for d in datasets]