sqlalchemy.create_engine

Here are the examples of the python api sqlalchemy.create_engine taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

200 Examples 7

Example 1

View license
    def setUp(self):
        """Set up fake database session before all tests."""
        engine = create_engine('sqlite://', echo=False)  # echo=True for debug
        Base.metadata.create_all(engine)
        Session = sessionmaker(bind=engine)
        self.session = Session()

Example 2

Project: ddldump
Source File: main.py
View license
def get_db_connection(url):
    """
    Args:
        url (str)

    Returns:
        sqlalchemy.engine.base.Engine
    """
    assert isinstance(url, str)

    engine = sqlalchemy.create_engine(url)

    return engine

Example 3

Project: motorway
Source File: intersections.py
View license
    def __init__(self, *args, **kwargs):
        self.engine = create_engine(self.database_uri, echo=self.debug_sql_statements)
        self.metadata = MetaData(bind=self.engine)
        self.connection = self.engine.connect()
        try:
            self.table = Table(self.table, self.metadata, autoload=True, schema=self.schema)
        except NoSuchTableError:
            if not self.create_table:
                raise
            else:
                self.table = Table(self.table, self.metadata,
                    *[
                        Column(column['name'], column['type'], primary_key=column.get('primary_key', False))
                    for column in self.table_columns],
                schema = self.schema)
                self.table.create(self.engine)

        super(DatabaseInsertIntersection, self).__init__(*args, **kwargs)

Example 4

Project: SQLAHelper
Source File: tests.py
View license
    def test_multiple_engines_without_default(self):
        db1 = sa.create_engine(self.db1.url)
        db2 = sa.create_engine(self.db2.url)
        sqlahelper.add_engine(db1, "db1")
        sqlahelper.add_engine(db2, "db2")
        # Can we retrieve the engines?
        self.assertIs(sqlahelper.get_engine("db1"), db1)
        self.assertIs(sqlahelper.get_engine("db2"), db2)
        # There should be no default engine
        self.assertIsNone(sqlahelper.get_session().bind)
        self.assertIsNone(sqlahelper.get_base().metadata.bind)
        self.assertIsNone(sqlahelper.get_engine())

Example 5

Project: checkmate
Source File: helpers.py
View license
def get_backend(project_path,project_config,settings,echo = False,initialize_db = True):
    backend_config = project_config['backend']
    engine = create_engine('sqlite:///%s' % os.path.join(project_path,'.checkmate/db.sqlite'),echo=echo)
    backend = SqlBackend(engine)

    if initialize_db:
        backend.create_schema()
        return backend

        #we run the Alembic migration script.
        print "Running migrations..."
        from checkmate.management.commands.alembic import Command as AlembicCommand
        alembic_command = AlembicCommand(None,backend,args = ['upgrade','head'])
        alembic_command.run()
        print "Done running migrations..."

    return backend

Example 6

Project: suma
Source File: celery.py
View license
    @property
    def dbsession(self):
        if self._dbsession is None:
            engine = create_engine(self.registry.settings['sqlalchemy.url'])
            self._dbsession = create_dbsession(engine)
        return self._dbsession

Example 7

Project: Tempo
Source File: api.py
View license
def get_engine():
    """
    Establish the database, create an engine if needed, and
    register the models.

    :param options: Mapping of configuration options
    """
    global _ENGINE
    if not _ENGINE:
        _ENGINE = create_engine(CFG.db.sql_connection,
                                pool_recycle=CFG.db.sql_idle_timeout)
    return _ENGINE

Example 8

Project: rootio_web
Source File: manage.py
View license
@manager.command
def drop_db_hard():
    #for when db.drop_all won't cut it, particularly with postgres on server

    print "WARNING: This will drop all database tables."
    response = raw_input("Are you sure you want to continue? (Yes/No) ")
    if not response == "Yes":
        print "Aborted."
        sys.exit()

    import sqlalchemy
    engine = sqlalchemy.create_engine('postgresql://postgres:[email protected]')
    meta = sqlalchemy.MetaData(engine)
    meta.reflect()
    meta.drop_all()

Example 9

Project: exporters
Source File: alchemy_persistence.py
View license
    def _db_init(self):
        db_uri = self.build_db_conn_uri(
            proto=self.PROTOCOL,
            user=self.read_option('user'),
            password=self.read_option('password'),
            host=self.read_option('host'),
            port=self.read_option('port'),
            database=self.read_option('database'),
        )
        self.engine = create_engine(db_uri)
        Base.metadata.create_all(self.engine)
        Base.metadata.bind = self.engine
        DBSession = sessionmaker(bind=self.engine)
        self.session = DBSession()

Example 10

Project: frontera
Source File: __init__.py
View license
    def __init__(self, manager):
        self.manager = manager
        settings = manager.settings
        engine = settings.get('SQLALCHEMYBACKEND_ENGINE')
        engine_echo = settings.get('SQLALCHEMYBACKEND_ENGINE_ECHO')
        models = settings.get('SQLALCHEMYBACKEND_MODELS')
        self.engine = create_engine(engine, echo=engine_echo)
        self.models = dict([(name, load_object(klass)) for name, klass in models.items()])
        self.session_cls = sessionmaker()
        self.session_cls.configure(bind=self.engine)
        self._metadata = None
        self._queue = None
        self._states = None

Example 11

Project: piecash
Source File: test_book.py
View license
    def test_create_with_FK(self):
        # create and keep FK
        b = create_book(uri_conn=db_sqlite_uri, keep_foreign_keys=True, overwrite=True)
        b.session.close()

        insp = Inspector.from_engine(create_engine(db_sqlite_uri))
        fk_total = []
        for tbl in insp.get_table_names():
            fk_total.append(insp.get_foreign_keys(tbl))
        assert len(fk_total) == 25

Example 12

Project: SecPi
Source File: database.py
View license
def connect(path):
	global session
	global engine
	
	# TODO: think about check_same_thread=False
	engine = create_engine("sqlite:///%s/data.db"%path, connect_args={'check_same_thread':False}, echo = False) # echo = true aktiviert debug logging

	Session = sessionmaker(bind=engine)
	session = Session()

Example 13

Project: glacier-cli
Source File: glacier.py
View license
    def __init__(self, key, db_path=None):
        self.key = key
        if db_path is None:
            db_path = os.path.join(get_user_cache_dir(), 'glacier-cli', 'db')
        if db_path != ':memory:':
            mkdir_p(os.path.dirname(db_path))
        self.engine = sqlalchemy.create_engine('sqlite:///%s' % db_path)
        self.Base.metadata.create_all(self.engine)
        self.Session.configure(bind=self.engine)
        self.session = self.Session()

Example 14

Project: luigi
Source File: sqla_test.py
View license
    def test_create_table_raises_no_columns(self):
        """
        Check that the test fails when the columns are not set
        :return:
        """
        class TestSQLData(sqla.CopyToTable):
            connection_string = self.connection_string
            table = "test_table"
            columns = []
            chunk_size = 1

        def output(self):
            pass

        sql_copy = TestSQLData()
        eng = sqlalchemy.create_engine(TestSQLData.connection_string)
        self.assertRaises(NotImplementedError, sql_copy.create_table, eng)

Example 15

Project: popong-api
Source File: database.py
View license
def init_app(app):
    sqlalchemy_uri = app.config.get('SQLALCHEMY_URI')
    engine = create_engine(sqlalchemy_uri)
    db_session = scoped_session(sessionmaker(autocommit=False,
                                             autoflush=False,
                                             bind=engine))
    Base.query = db_session.query_property()

    init_db(engine)

    @app.teardown_request
    def shutdown_session(exception=None):
        db_session.remove()

Example 16

Project: statik
Source File: database.py
View license
    def __init__(self, data_path, models, encoding=None):
        """Constructor.

        Args:
            data_path: The full path to where the database files can be found.
            models: Loaded model/field data.
            encoding: The encoding to load files as ('utf-8', etc). If 'None', will
                      default to the system-preferred default encoding
        """
        self.encoding = encoding
        self.tables = dict()
        self.data_path = data_path
        self.models = models
        self.engine = create_engine('sqlite:///:memory:')
        self.Base = declarative_base()
        self.session = sessionmaker(bind=self.engine)()
        set_global('session', self.session)
        self.find_backrefs()
        self.create_db(models)

Example 17

Project: freepy
Source File: sql.py
View license
  @staticmethod
  def _get_engine(max_overflow, pool_size, timeout, url):
    if 'sqlite' in url:
      engine = create_engine(
        url,
        connect_args={
          'check_same_thread': False
        },
        poolclass=StaticPool
      )
    else:
      engine = create_engine(
        url,
        max_overflow=max_overflow,
        pool_size=pool_size,
        pool_timeout=timeout
      )
    return engine

Example 18

Project: cslbot
Source File: env.py
View license
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.

    """
    botconfig = configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation())
    config_path = config.get_main_option('bot_config_path', join(dirname(__file__), '../..'))
    with open(join(config_path, 'config.cfg')) as f:
        botconfig.read_file(f)
    url = botconfig['db']['engine']

    connectable = create_engine(url)
    with connectable.connect() as connection:
        context.configure(connection=connection, target_metadata=target_metadata, render_as_batch=True)

        with context.begin_transaction():
            context.run_migrations()

Example 19

Project: appointment-reminders-flask
Source File: env.py
View license
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(connection_url_from_env(),
                                poolclass=pool.NullPool)

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

        with context.begin_transaction():
            context.run_migrations()

Example 20

Project: tushare
Source File: storing_test.py
View license
def db():
    df = ts.get_tick_data('600848',date='2014-12-22')
    engine = create_engine('mysql://root:[email protected]/mystock?charset=utf8')
#     db = MySQLdb.connect(host='127.0.0.1',user='root',passwd='jimmy1',db="mystock",charset="utf8")
#     df.to_sql('TICK_DATA',con=db,flavor='mysql')
#     db.close()
    df.to_sql('tick_data',engine,if_exists='append')

Example 21

Project: backy2
Source File: sql.py
View license
    def __init__(self, config):
        _MetaBackend.__init__(self)
        # engine = sqlalchemy.create_engine(config.get('engine'), echo=True)
        engine = sqlalchemy.create_engine(config.get('engine'))
        Base.metadata.create_all(engine)
        Session = sessionmaker(bind=engine)
        self.session = Session()
        self._flush_block_counter = 0

Example 22

Project: yandex-tank
Source File: guns.py
View license
    def __init__(self, core):
        super(SqlGun, self).__init__(core)

        from sqlalchemy import create_engine
        from sqlalchemy import exc
        self.exc = exc

        self.engine = create_engine(self.get_option("db"))

Example 23

Project: zulip
Source File: sqlalchemy_utils.py
View license
def get_sqlalchemy_connection():
    # type: () -> sqlalchemy.engine.base.Connection
    global sqlalchemy_engine
    if sqlalchemy_engine is None:
        def get_dj_conn():
            # type: () -> TimeTrackingConnection
            connection.ensure_connection()
            return connection.connection
        sqlalchemy_engine = sqlalchemy.create_engine('postgresql://',
                                                     creator=get_dj_conn,
                                                     poolclass=NonClosingPool,
                                                     pool_reset_on_return=False)
    sa_connection = sqlalchemy_engine.connect()
    sa_connection.execution_options(autocommit=False)
    return sa_connection

Example 24

Project: alembic
Source File: fixtures.py
View license
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 25

Project: blitzdb
Source File: test_datetime.py
View license
@pytest.fixture(scope="function")
def backend():

    engine = create_engine('sqlite:///:memory:', echo=True)
    backend = Backend(engine = engine,autodiscover_classes = False)
    backend.register(Actor)
    backend.init_schema()
    backend.create_schema()
    return backend

Example 26

Project: mindpark
Source File: reader.py
View license
    def __call__(self, filename):
        assert os.path.isfile(filename)
        engine = sql.create_engine('sqlite:///{}'.format(filename))
        metadata = sql.MetaData()
        metadata.reflect(engine)
        for name in self._select_metrics(metadata.tables.keys()):
            columns = self._collect_columns(engine, metadata.tables[name])
            if columns is None:
                continue
            yield name, columns

Example 27

Project: DIRAC
Source File: InstalledComponentsDB.py
View license
  def __initializeConnection( self, dbPath ):

    result = getDBParameters( dbPath )
    if not result[ 'OK' ]:
      raise Exception( 'Cannot get database parameters: %s' % result['Message'] )

    dbParameters = result[ 'Value' ]
    self.host = dbParameters[ 'Host' ]
    self.port = dbParameters[ 'Port' ]
    self.user = dbParameters[ 'User' ]
    self.password = dbParameters[ 'Password' ]
    self.dbName = dbParameters[ 'DBName' ]

    self.engine = create_engine( 'mysql://%s:%[email protected]%s:%s/%s' % ( self.user, self.password, self.host, self.port, self.dbName ),
                                 pool_recycle = 3600, echo_pool = True)
    self.session = scoped_session( sessionmaker( bind = self.engine ) )
    self.inspector = Inspector.from_engine( self.engine )

Example 28

Project: docker-registry
Source File: db.py
View license
    def __init__(self, database=None):
        if database is None:
            cfg = config.load()
            database = cfg.sqlalchemy_index_database
        self._database = database
        self._engine = sqlalchemy.create_engine(database)
        self._session = sqlalchemy.orm.sessionmaker(bind=self._engine)
        self.version = 1
        self._setup_database()
        super(SQLAlchemyIndex, self).__init__()

Example 29

Project: blain
Source File: db.py
View license
    def __init__(self, type = "sqlite", location = ":memory:"):
        if type == 'sqlite':
            location = "/" + location
        self.models = {}
        self.type = type
        self.location = location
        self.name = type + '://' + location
        self.engine = create_engine(self.name)#, listeners=[SetTextFactory()])
        self.session = sessionmaker(bind=self.engine)()
        self.column_mapping = {'string': String,       'str': String,
                              'integer': Integer,      'int': Integer,
                              'unicode': Unicode,     'text': Text,
                          'unicodetext': UnicodeText, 'date': Date,
                              'numeric': Numeric,     'time': Time,
                                'float': Float,   'datetime': DateTime,
                             'interval': Interval,  'binary': Binary,
                              'boolean': Boolean,     'bool': Boolean,
                           'pickletype': PickleType}

Example 30

Project: snakemq
Source File: sqla.py
View license
    def __init__(self, *args, **kwargs):
        """
        Parameters are passed to create_engine().
        """
        self.engine = create_engine(*args, **kwargs)
        self.conn = self.engine.connect()

Example 31

Project: aurora
Source File: utils.py
View license
def get_session():
    """Creates session for process"""
    engine = create_engine(current_app.config['SQLALCHEMY_DATABASE_URI'])
    Session = scoped_session(sessionmaker(bind=engine,
                                          autoflush=False,
                                          autocommit=False))
    session = Session()
    setattr(session, '_model_changes', dict())
    return session

Example 32

Project: cryptobot-email
Source File: unsubscribe.py
View license
  def __init__(self, url, setup=False, debug=False):
    self.engine = create_engine(url, echo=debug)

    if setup:
      self.setup()
    else:
      self.session = sessionmaker(self.engine)()

    self.hash_params = self.session.query(Hash).first()

Example 33

Project: catsnap
Source File: __init__.py
View license
    def session(self):
        if not self._engine:
            self._engine = create_engine(self.config()['postgres_url'])
        if not self._session:
            self._session = MutexSession(self._engine)
        return self._session

Example 34

Project: Vulpix
Source File: main.py
View license
    def __init__(self):
        tornado.web.Application.__init__(self, handlers, **site_config)
        tornado.locale.load_gettext_translations(self.settings['i18n_path'], "vulpix")
        # Set Jinja2
        jinja_env = Environment(loader = FileSystemLoader(self.settings['template_path']))
        jinja_env.filters.update(filters)
        self.jinja2 = jinja_env
#        self.db = tornado.database.Connection(
#                  host=options.mysql_host, database=options.mysql_database,
#                  user=options.mysql_user, password=options.mysql_password)
        engine = create_engine(mysql_path, convert_unicode=True)#, echo=options.debug)
        models.init_db(engine)
        self.db = scoped_session(sessionmaker(bind=engine))

Example 35

Project: bodhi
Source File: base.py
View license
    def setUp(self):
        engine = create_engine(DB_PATH)
        # We want a special session that lasts longer than a transaction
        Session = scoped_session(
            sessionmaker(bind=engine, extension=ZopeTransactionExtension(keep_session=True)))
        log.debug('Creating all models for %s' % engine)
        Base.metadata.bind = engine
        Base.metadata.create_all(engine)
        self.db = Session()
        populate(self.db)

        # Track sql statements in every test
        self.sql_statements = []

        def track(conn, cursor, statement, param, ctx, many):
            self.sql_statements.append(statement)

        event.listen(engine, "before_cursor_execute", track)

Example 36

Project: faitout
Source File: __init__.py
View license
def get_engine(db_url, debug=False, pool_recycle=3600):
    """ Create the engine use to connect to the database.

    :arg db_url: URL used to connect to the database. The URL contains
    information with regards to the database engine, the host to connect
    to, the user and password and the database name.
      ie: <engine>://<user>:<password>@<host>/<dbname>
    :arg debug: a boolean specifying wether we should have the verbose
        output of sqlalchemy or not.
    :return an engine that can be used to connect the database.
    """
    return create_engine(db_url, echo=debug, pool_recycle=pool_recycle)

Example 37

View license
def dbConnection():
    """Use the config to set up a database engine and connection"""
    if not CONFIG_DB:
        raise ValueError("Database configuration is not defined")

    dbName = CONFIG_DB['db_name']
    if not dbName:
        raise ValueError("Need dbName defined")

    # Create sqlalchemy connection and session
    engine = sqlalchemy.create_engine(
        "postgresql://{username}:{password}@{host}:{port}/{db_name}".format(
            **CONFIG_DB),
        pool_size=100, max_overflow=50)
    connection = engine.connect()
    Session = scoped_session(sessionmaker(bind=engine, autoflush=True))
    return _DB(engine, connection, Session, Session())

Example 38

Project: eralchemy
Source File: sqla.py
View license
def database_to_intermediary(database_uri, schema=None):
    """ Introspect from the database (given the database_uri) to create the intermediary representation. """
    from sqlalchemy.ext.automap import automap_base
    from sqlalchemy import create_engine

    Base = automap_base()
    engine = create_engine(database_uri)
    if schema is not None:
        Base.metadata.schema = schema

    # reflect the tables
    Base.prepare(engine, reflect=True)
    return declarative_to_intermediary(Base)

Example 39

Project: golismero
Source File: sqla.py
View license
    def __init__(self, *args, **kwargs):
        """
        Parameters are passed to create_engine().
        """
        self.engine = create_engine(*args, **kwargs)
        self.conn = self.engine.connect()

Example 40

Project: timesketch
Source File: __init__.py
View license
def configure_engine(url):
    """Configure and setup the database session."""
    # These needs to be global because of the way Flask works.
    # pylint: disable=global-variable-not-assigned
    # TODO: Can we wrap this in a class?
    global engine, session_maker, db_session
    engine = create_engine(url)
    db_session.remove()
    # Set the query class to our own AclBaseQuery
    session_maker.configure(
        autocommit=False, autoflush=False, bind=engine, query_cls=AclBaseQuery)

Example 41

View license
def test_populate_db_populates_users():
    engine = sqlalchemy.create_engine('sqlite://')
    session = create_session(engine)

    try:
        populate_db(session, total_users=10)

        connection = session.connection().connection
        cursor = connection.cursor()
        cursor.execute('SELECT COUNT(*) FROM Users')
        assert cursor.fetchone()[0] == 10
        cursor.execute('SELECT COUNT(*) FROM UserSessions')
        assert cursor.fetchone()[0] >= 10
    finally:
        session.close()

Example 42

Project: Flask-Blogging
Source File: test_sqlastorage.py
View license
    def _create_storage(self):
        temp_dir = tempfile.gettempdir()
        self._dbfile = os.path.join(temp_dir, "temp.db")
        self._engine = create_engine('sqlite:///'+self._dbfile)
        self._meta = sqla.MetaData()
        self.storage = SQLAStorage(self._engine, metadata=self._meta)
        self._meta.create_all(bind=self._engine)

Example 43

Project: emailhunter-clone
Source File: database.py
View license
	def connect(self):

		self.engine = create_engine('sqlite:///' + DATABASE_NAME)
		self.connection = self.engine.connect()
		self.connected = True if self.connection else False
		self.metadata = MetaData()

		# Define the tables
		self.website_table = Table('website', self.metadata,
			Column('id', Integer, primary_key=True),
			Column('url', String, nullable=False),
			Column('has_crawled', Boolean, default=False),
			Column('emails', String, nullable=True),
		)

		# Create the tables
		self.metadata.create_all(self.engine)

Example 44

Project: synnefo
Source File: migrate.py
View license
    def __init__(self, db):
        self.engine = create_engine(db)
        self.metadata = MetaData(self.engine)
        #self.engine.echo = True
        self.conn = self.engine.connect()

        options = getattr(settings, 'BACKEND', None)[1]
        self.backend = ModularBackend(*options)

Example 45

Project: tornado-sqlalchemy-template
Source File: app.py
View license
  def __init__(self):
    handlers = [
      url(r'/', IndexHandler, name='index'),
    ]
    settings = dict(
      debug=options.debug,
      static_path=os.path.join(os.path.dirname(__file__), "static"),
      template_path=os.path.join(os.path.dirname(__file__), 'templates'),
      xsrf_cookies=True,
      # TODO Change this to a random string
      cookie_secret="nzjxcjasduuqwheazmu293nsadhaslzkci9023nsadnua9sdads/Vo=",
      ui_modules=uimodules,
    )
    tornado.web.Application.__init__(self, handlers, **settings)
    engine = create_engine(options.db_path, convert_unicode=True, echo=options.debug)
    models.init_db(engine)
    self.db = scoped_session(sessionmaker(bind=engine))

Example 46

Project: cum
Source File: db.py
View license
def initialize():
    global db_path, engine, session
    db_path = os.path.join(config.cum_dir, 'cum.db')
    db_url = sqlalchemy.engine.url.URL('sqlite', database=db_path)
    engine = create_engine(db_url)
    if not os.path.exists(db_path):
        Base.metadata.create_all(bind=engine)
    Session = sessionmaker(bind=engine)
    session = Session()

Example 47

Project: holmes-api
Source File: elastic.py
View license
    def connect_to_db(self):
        from sqlalchemy import create_engine
        from sqlalchemy.orm import scoped_session, sessionmaker
        conn_string = self.config.get('SQLALCHEMY_CONNECTION_STRING')
        engine = create_engine(
            conn_string,
            convert_unicode=True,
            pool_size=1,
            max_overflow=0,
            echo=self.debug
        )
        maker = sessionmaker(bind=engine, autoflush=True)
        self.db = scoped_session(maker)

Example 48

Project: hotdoc
Source File: doc_database.py
View license
    def setup(self, db_folder):
        """
        Banana banana
        """
        db_path = os.path.join(db_folder, 'hotdoc.db')

        if os.path.exists(db_path):
            self.__incremental = True

        self.__engine = create_engine('sqlite:///%s' % db_path)
        self.__session = sessionmaker(self.__engine)()
        self.__session.autoflush = False
        Base.metadata.create_all(self.__engine)

Example 49

Project: kittystore
Source File: __init__.py
View license
    def __init__(self, settings, engine=None, debug=False):
        self.settings = settings
        if engine is None:
            engine = create_engine(settings.KITTYSTORE_URL, echo=debug)
        self.engine = engine
        self.debug = debug
        self._config = None
        self._script = None

Example 50

Project: commandment
Source File: database.py
View license
def config_engine(uri, echo):
    global engine, mysessionmaker, db_session

    engine = create_engine(uri, convert_unicode=True, echo=echo)
    db_session.remove()
    mysessionmaker.configure(autocommit=False, autoflush=False, bind=engine)