Example #1
0
    def setup_sqlalchemy(self):
        """Setup SQLAlchemy database engine.

        The most common reason for modifying this method is to add
        multiple database support.  To do this you might modify your
        app_cfg.py file in the following manner::

            from tg.configuration import AppConfig, config
            from myapp.model import init_model

            # add this before base_config =
            class MultiDBAppConfig(AppConfig):
                def setup_sqlalchemy(self):
                    '''Setup SQLAlchemy database engine(s)'''
                    from sqlalchemy import engine_from_config
                    engine1 = engine_from_config(config, 'sqlalchemy.first.')
                    engine2 = engine_from_config(config, 'sqlalchemy.second.')
                    # engine1 should be assigned to sa_engine as well as your first engine's name
                    config['tg.app_globals'].sa_engine = engine1
                    config['tg.app_globals'].sa_engine_first = engine1
                    config['tg.app_globals'].sa_engine_second = engine2
                    # Pass the engines to init_model, to be able to introspect tables
                    init_model(engine1, engine2)

            #base_config = AppConfig()
            base_config = MultiDBAppConfig()

        This will pull the config settings from your .ini files to create the necessary
        engines for use within your application.  Make sure you have a look at :ref:`multidatabase`
        for more information.

        """
        from sqlalchemy import engine_from_config

        balanced_master = config.get('sqlalchemy.master.url')
        if not balanced_master:
            engine = engine_from_config(config, 'sqlalchemy.')
        else:
            engine = engine_from_config(config, 'sqlalchemy.master.')
            config['balanced_engines'] = {'master':engine,
                                          'slaves':{},
                                          'all':{'master':engine}}

            all_engines = config['balanced_engines']['all']
            slaves = config['balanced_engines']['slaves']
            for entry in config.keys():
                if entry.startswith('sqlalchemy.slaves.'):
                    slave_path = entry.split('.')
                    slave_name = slave_path[2]
                    if slave_name == 'master':
                        raise TGConfigError('A slave node cannot be named master')
                    slave_config = '.'.join(slave_path[:3])
                    all_engines[slave_name] = slaves[slave_name] = engine_from_config(config, slave_config+'.')

            if not config['balanced_engines']['slaves']:
                raise TGConfigError('When running in balanced mode your must specify at least a slave node')

        # Pass the engine to initmodel, to be able to introspect tables
        config['tg.app_globals'].sa_engine = engine
        self.package.model.init_model(engine)
Example #2
0
def on_celeryd_init(**kw):
    """
    Initializes application resources when the Celery daeomon starts
    """
    settings = app.settings

    redisurl = six.moves.urllib.parse.urlparse(settings['redis.url'])
    app.redis = redis.StrictRedis(
        host=redisurl.hostname,
        port=redisurl.port,
        db=os.path.basename(redisurl.path)
    )

    app.userid = settings['celery.blame']

    # Attempt to add the user via raw engine connection, using the scoped
    # session leaves it in a dangerous non-thread-local state as we're
    # still in the parent setup process
    throw_away_engine = sa.engine_from_config(settings, 'occams.db.')
    with throw_away_engine.begin() as connection:
        try:
            connection.execute(models.User.__table__.insert(),  key=app.userid)
        except sa.exc.IntegrityError:
            pass
    throw_away_engine.dispose()

    # Configure the session with an untainted engine
    engine = sa.engine_from_config(settings, 'occams.db.')
    Session.configure(bind=engine)
Example #3
0
def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    if 'DATABASE_URL' in os.environ:
        settings['sqlalchemy.url'] = os.environ['DATABASE_URL']
    engine = engine_from_config(settings, 'sqlalchemy.')
    auth_secret = os.environ.get('AUTH_SECRET', 'secret')
    authn_policy = AuthTktAuthenticationPolicy(secret=auth_secret,
                                               hashalg='sha256')
    authz_policy = ACLAuthorizationPolicy()
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine
    config = Configurator(
        settings=settings,
        root_factory=MyRoot)
    config.set_session_factory(SignedCookieSessionFactory('seekrit'))
    config.set_authentication_policy(authn_policy)
    config.set_authorization_policy(authz_policy)
    config.include('pyramid_jinja2')
    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_route('index', '/')
    config.add_route('add', '/add')
    config.add_route('entry', '/entries/{id:\d+}')
    config.add_route('edit', '/entries/{id:\d+}/edit')
    config.add_route('login', '/login')
    config.add_route('logout', '/logout')
    # config.add_route('add_json', '/add_json')
    # config.add_route('entry_json', '/entry_json')
    config.scan()
    return config.make_wsgi_app()
Example #4
0
def load_environment(global_conf, app_conf):
    """Configure the Pylons environment via the ``pylons.config``
    object
    """
    # Pylons paths
    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    paths = dict(root=root,
                 controllers=os.path.join(root, 'controllers'),
                 static_files=os.path.join(root, 'public'),
                 templates=[os.path.join(root, 'templates')])

    # Initialize config with the basic options
    config.init_app(global_conf, app_conf, package='mapclient', paths=paths)

    config['routes.map'] = make_map()
    config['pylons.app_globals'] = app_globals.Globals()
    config['pylons.h'] = mapclient.lib.helpers

    # Create the Jinja2 Environment
    config['pylons.app_globals'].jinja2_env = Environment(loader=ChoiceLoader(
            [FileSystemLoader(path) for path in paths['templates']]))
    # Jinja2's unable to request c's attributes without strict_c
    config['pylons.strict_c'] = True

    # Setup the SQLAlchemy database engine
    engine = engine_from_config(config, 'mapclient.sqlalchemy.')
    engine_ckan_data = engine_from_config(config, 'vectorstore.sqlalchemy.')
    init_model(engine, engine_ckan_data)
Example #5
0
    def update_config(self, config_):
        #toolkit.add_template_directory(config_, 'templates')
        #toolkit.add_public_directory(config_, 'public')
        #toolkit.add_resource('fanstatic', 'jsondatastore')

        if not 'ckanext.jsondatastore.write.url' in config_:
            error_message = 'jsonckan.datastore.write_url not found in config'
            raise JsonDatastoreError(error_message)

        ckan_db = config_['sqlalchemy.url']
        self.write_url = config_['ckanext.jsondatastore.write.url']

        if self.write_url == ckan_db:
            raise JsonDatastoreError('The json datastore url cannot be the '
                                     'same as the main ckan database')


        self.write_engine = sqlalchemy.engine_from_config(
            config_,
            prefix='ckanext.jsondatastore.write.',
            client_encoding='utf8',
        ) 

        self.read_engine = sqlalchemy.engine_from_config(
            config_,
            prefix='ckanext.jsondatastore.read.',
            client_encoding='utf8',
        ) 
Example #6
0
    def create_sqlalchemy_engine(conf):
        from sqlalchemy import engine_from_config
        balanced_master = conf.get('sqlalchemy.master.url')
        if not balanced_master:
            engine = engine_from_config(conf, 'sqlalchemy.')
        else:
            engine = engine_from_config(conf, 'sqlalchemy.master.')
            conf['balanced_engines'] = {'master': engine,
                                        'slaves': {},
                                        'all': {'master': engine}}

            all_engines = conf['balanced_engines']['all']
            slaves = conf['balanced_engines']['slaves']
            for entry in conf.keys():
                if entry.startswith('sqlalchemy.slaves.'):
                    slave_path = entry.split('.')
                    slave_name = slave_path[2]
                    if slave_name == 'master':
                        raise TGConfigError('A slave node cannot be named master')
                    slave_config = '.'.join(slave_path[:3])
                    all_engines[slave_name] = slaves[slave_name] = engine_from_config(conf, slave_config + '.')

            if not conf['balanced_engines']['slaves']:
                raise TGConfigError('When running in balanced mode your must specify at least a slave node')

        return engine
Example #7
0
def main(argv=sys.argv):
    settings_file = os.path.join(
        os.path.dirname(os.path.abspath(__file__)), 'migration.ini')
    settings = get_appsettings(settings_file)

    engine_target = engine_from_config(settings, 'sqlalchemy_target.')
    engine_source = engine_from_config(settings, 'sqlalchemy_source.')

    logging.basicConfig()
    logging.getLogger('sqlalchemy.engine').setLevel(logging.WARN)

    # create a fresh schema on the target database
    Session = sessionmaker(extension=ZopeTransactionExtension())  # noqa
    session = Session(bind=engine_target)
    Base.metadata.drop_all(engine_target, checkfirst=True)
    setup_db(engine_target, session)

    connection_source = engine_source.connect()

    batch_size = 1000
    MigrateUsers(connection_source, session, batch_size).migrate()
    MigrateSummits(connection_source, session, batch_size).migrate()
    MigrateParkings(connection_source, session, batch_size).migrate()
    MigrateSites(connection_source, session, batch_size).migrate()
    MigrateProducts(connection_source, session, batch_size).migrate()
    MigrateHuts(connection_source, session, batch_size).migrate()
    MigrateRoutes(connection_source, session, batch_size).migrate()
    MigrateVersions(connection_source, session, batch_size).migrate()
    UpdateSequences(connection_source, session, batch_size).migrate()
Example #8
0
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.

    """
    if 'DB_URL' in os.environ:
        engine = engine_from_config(
            config.get_section("app:pyramidapp"),
            prefix='sqlalchemy.',
            url=os.environ['DB_URL'],
            poolclass=pool.NullPool)
    else:
        engine = engine_from_config(
            config.get_section("app:pyramidapp"),
            prefix='sqlalchemy.',
            poolclass=pool.NullPool)

    connection = engine.connect()
    context.configure(
        connection=connection,
        target_metadata=target_metadata
    )

    try:
        with context.begin_transaction():
            context.run_migrations()
    finally:
        connection.close()
Example #9
0
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.

    """
    # FIX for Postgres updates
    url = config.get_section(config.config_ini_section).get("sqlalchemy.url")
    driver = url.split(":")[0]

    if driver == "postgresql+psycopg2":
        engine = engine_from_config(
                    config.get_section(config.config_ini_section),
                    prefix='sqlalchemy.',
                    isolation_level="AUTOCOMMIT",
                    poolclass=pool.NullPool)
    else:
        engine = engine_from_config(
                    config.get_section(config.config_ini_section),
                    prefix='sqlalchemy.',
                    poolclass=pool.NullPool)

    connection = engine.connect()
    context.configure(
                connection=connection,
                target_metadata=target_metadata
                )

    try:
        with context.begin_transaction():
            context.run_migrations()
    finally:
        connection.close()
Example #10
0
 def setup_sqlalchemy(self):
     from sqlalchemy import engine_from_config
     engine1=engine_from_config(pylons_config, 'sqlalchemy.first.')
     engine2=engine_from_config(pylons_config, 'sqlalchemy.second.')
     config['pylons.app_globals'].sa_engine=engine1
     config['pylons.app_globals'].sa_engine_first=engine1
     config['pylons.app_globals'].sa_engine_second=engine2
     init_model(engine1, engine2)
Example #11
0
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.

    """

    # for the direct-to-DB use case, start a transaction on all
    # engines, then run all migrations, then commit all transactions.

    engines = {'': {'engine': engine_from_config(
        config.get_section(config.config_ini_section),
        prefix='sqlalchemy.',
        poolclass=pool.NullPool)}}
    for name in bind_names:
        engines[name] = rec = {}
        rec['engine'] = engine_from_config(
            context.config.get_section(name),
            prefix='sqlalchemy.',
            poolclass=pool.NullPool)

    for name, rec in engines.items():
        engine = rec['engine']
        rec['connection'] = conn = engine.connect()

        if USE_TWOPHASE:
            rec['transaction'] = conn.begin_twophase()
        else:
            rec['transaction'] = conn.begin()

    try:
        for name, rec in engines.items():
            logger.info("Migrating database %s" % (name or '<default>'))
            context.configure(
                connection=rec['connection'],
                upgrade_token="%s_upgrades" % name,
                downgrade_token="%s_downgrades" % name,
                user_module_prefix="application.models._external_types.",
                target_metadata=get_metadata(name)
            )
            context.run_migrations(engine_name=name)

        if USE_TWOPHASE:
            for rec in engines.values():
                rec['transaction'].prepare()

        for rec in engines.values():
            rec['transaction'].commit()
    except:
        for rec in engines.values():
            rec['transaction'].rollback()
        raise
    finally:
        for rec in engines.values():
            rec['connection'].close()
Example #12
0
def load_environment(global_conf, app_conf):
    """Configure the Pylons environment via the ``pylons.config`` object

    """

    # Pylons paths
    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    paths = dict(root=root,
                 controllers=os.path.join(root, 'controllers'),
                 static_files=os.path.join(root, 'public'),
                 templates=[os.path.join(root, 'templates')])

    # Initialize config with the basic options
    config.init_app(
        global_conf, app_conf, package='onlinelinguisticdatabase', paths=paths
    )

    config['routes.map'] = make_map()
    config['pylons.app_globals'] = app_globals.Globals()
    config['pylons.h'] = onlinelinguisticdatabase.lib.helpers

    # Create the Mako TemplateLookup, with the default auto-escaping
    config['pylons.app_globals'].mako_lookup = TemplateLookup(
        directories=paths['templates'],
        error_handler=handle_mako_error,
        module_directory=os.path.join(app_conf['cache_dir'], 'templates'),
        input_encoding='utf-8', default_filters=['escape'],
        imports=['from webhelpers.html import escape'])

    # Setup the SQLAlchemy database engine
    #  Modification: check if SQLite is RDBMS and, if so,
    #  give the engine a SQLiteSetup listener which
    #  provides the regexp function missing from the SQLite dbapi
    #  (cf. http://groups.google.com/group/pylons-discuss/browse_thread/thread/8c82699e6b6a400c/5c5237c86202e2b8)
    SQLAlchemyURL = config['sqlalchemy.url']
    rdbms = SQLAlchemyURL.split(':')[0]
    if rdbms == 'sqlite':
        engine = engine_from_config(
            config, 'sqlalchemy.', listeners=[SQLiteSetup()])
    else:
        engine = engine_from_config(config, 'sqlalchemy.')
    init_model(engine)

    # Put the application settings into the app_globals object
    #  This has the effect that when the app is restarted the globals like
    #  objectLanguageName, metalanguageName, etc. have the correct values
    #  Do the same for the variable app_globals attributes, e.g., sources list
    #  I HAD TO DISABLE THE FOLLOWING TWO COMMANDS BECAUSE IT WAS CAUSING
    #  setup-app TO CRASH BECAUSE application_settings WAS REQUESTED BEFORE THE
    #  TABLES EXISTED!  FIND ANOTHER WAY TO FIX THIS PROBLEM ...
    #applicationSettingsToAppGlobals(config['pylons.app_globals'])
    #updateSecondaryObjectsInAppGlobals(config['pylons.app_globals'])

    # CONFIGURATION OPTIONS HERE (note: all config options will override
    # any Pylons config options)
    config['pylons.strict_c'] = True
 def __init__(self, *args):
     super(TestDBActions, self).__init__(*args)
     pconfig['pylons.g'] = Mock()
     pconfig['pylons.g'].sa_engine = engine_from_config(config,
         prefix = 'sqlalchemy.reflect.'
     )
     self.memengine = engine_from_config(config,
         prefix = 'sqlalchemy.default.')
     from masterapp import model
     self.model = model
     model.metadata.bind = self.memengine
     model.Session.configure(bind=self.memengine)
Example #14
0
def setup_db_engine(settings):
    cachedir = settings.get("db.cachedir")
    regions = []
    for region in settings.get("db.cacheregions", "").split(" "):
        regions.append(region.split(":"))
    if cachedir:
        init_cache(cachedir, regions)
    if settings.get("app.mode") == "testing":
        return engine_from_config(settings, 'sqlalchemy.',
                                  poolclass=StaticPool)
    else:
        return engine_from_config(settings, 'sqlalchemy.')
def main(argv=sys.argv):
    if len(argv) < 2:
        usage(argv)
    config_uri = argv[1]
    options = parse_vars(argv[2:])
    setup_logging(config_uri)
    settings = get_appsettings(config_uri, options=options)
    if 'DATABASE_URL' in os.environ:
        settings['sqlalchemy.url'] = os.environ['DATABASE_URL']
    engine = engine_from_config(settings, 'sqlalchemy.')
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.create_all(engine)
Example #16
0
def main(argv=sys.argv):
    if len(argv) != 2:
        usage(argv)
    config_uri = argv[1]
    setup_logging(config_uri)
    settings = get_appsettings(config_uri)
    os.environ['PYJASPER_SERVLET_URL'] = settings['jasper_url'] 
    bootstrap(config_uri)
    engine = engine_from_config(settings, 'sqlalchemy.')
    other_engine = engine_from_config(settings, 'othersql.')
    Base.metadata.bind = engine
    OtherBase.metadata.bind = other_engine
    from ..models.esppt_models import (
        esNopModel,
        esRegModel,
        spptModel,
        )
    from ..views.es_reports import GenerateSppt
    DBSession.configure(bind=engine)
    q = DBSession.query(esNopModel, esRegModel).filter(
            esNopModel.es_reg_id == esRegModel.id)
    q = q.filter(esNopModel.email_sent == 0)
    for r_nop, r_reg in q:
        nop = get_nop(r_nop)
        q = spptModel.get_by_nop_thn(nop, r_nop.tahun)
        sppt = q.first()
        if not sppt:
            continue
        nilai = thousand(sppt.pbb_yg_harus_dibayar_sppt)
        g = GenerateSppt(nop, r_nop.tahun, r_reg.kode) 
        #USER_ID) updated menggunakan password dari user yang ada di reg.kode aagusti
        sppt_file = g.sppt_file
        e_filename = os.path.split(sppt_file)[-1]
        f = open(sppt_file)
        content = f.read()
        f.close()
        e_content = base64.encodestring(content)
        e_subject = EMAIL_SUBJECT.format(nop=nop, tahun=r_nop.tahun)
        e_body = EMAIL_BODY.format(nama_wp=sppt.nm_wp_sppt, nop=nop,
                    tahun=r_nop.tahun, nilai=nilai)
        files = [(e_filename, e_content)]
        print('To: {name} <{email}>'.format(name=sppt.nm_wp_sppt,
                email=r_reg.email))
        print('Subject: {s}'.format(s=e_subject))
        print('Body: {s}'.format(s=e_body))
        print('File: {s}'.format(s=e_filename))
        r_nop.email_sent = 1
        flush(r_nop)
        send(r_reg.email, sppt.nm_wp_sppt, e_subject, e_body, files,
                settings['email_pengirim'])
    transaction.commit()
Example #17
0
def main(argv=sys.argv):
    alembic_configfile = os.path.realpath(os.path.join(
        os.path.dirname(os.path.abspath(__file__)),
        '../../../alembic.ini'))
    alembic_config = Config(alembic_configfile)

    settings_file = os.path.join(
        os.path.dirname(os.path.abspath(__file__)), 'migration.ini')
    settings = get_appsettings(settings_file)

    engine_target = engine_from_config(settings, 'sqlalchemy_target.')
    engine_source = engine_from_config(settings, 'sqlalchemy_source.')

    logging.basicConfig()
    logging.getLogger('sqlalchemy.engine').setLevel(logging.WARN)

    Session = sessionmaker(extension=ZopeTransactionExtension())  # noqa
    session = Session(bind=engine_target)

    # set up the target database
    setup_db(alembic_config, session)

    connection_source = engine_source.connect()

    batch_size = 1000
    MigrateAreas(connection_source, session, batch_size).migrate()
    MigrateUserProfiles(connection_source, session, batch_size).migrate()
    MigrateUsers(connection_source, session, batch_size).migrate()
    MigrateSummits(connection_source, session, batch_size).migrate()
    MigrateParkings(connection_source, session, batch_size).migrate()
    MigrateSites(connection_source, session, batch_size).migrate()
    MigrateProducts(connection_source, session, batch_size).migrate()
    MigrateHuts(connection_source, session, batch_size).migrate()
    MigrateRoutes(connection_source, session, batch_size).migrate()
    MigrateMaps(connection_source, session, batch_size).migrate()
    MigrateOutings(connection_source, session, batch_size).migrate()
    MigrateImages(connection_source, session, batch_size).migrate()
    MigrateXreports(connection_source, session, batch_size).migrate()
    MigrateArticles(connection_source, session, batch_size).migrate()
    MigrateBooks(connection_source, session, batch_size).migrate()
    MigrateVersions(connection_source, session, batch_size).migrate()
    MigrateAssociations(connection_source, session, batch_size).migrate()
    CreateClimbingSiteRoutes(connection_source, session, batch_size).migrate()
    SetRouteTitlePrefix(connection_source, session, batch_size).migrate()
    SetDefaultGeometries(connection_source, session, batch_size).migrate()
    MigrateAreaAssociations(connection_source, session, batch_size).migrate()
    MigrateMapAssociations(connection_source, session, batch_size).migrate()
    MigrateMailinglists(connection_source, session, batch_size).migrate()
    UpdateSequences(connection_source, session, batch_size).migrate()
    InitFeed(connection_source, session, batch_size).migrate()
    AnalyzeAllTables(connection_source, session, batch_size).migrate()
Example #18
0
    def __init__(self, base_conf={}, master_url=None, slaves_url=[], **kwargs):
        self.engine = engine_from_config(base_conf, prefix="sqlalchemy.", url=master_url, **kwargs)
        # self.engine = create_engine(master, **kwargs)
        self._master_session = _create_session(self.engine)
        self._slaves_session = []
        for slave in slaves_url:
            # slave = create_engine(slave, **kwargs)
            slave_engine = engine_from_config(base_conf, prefix="sqlalchemy.", url=slave, **kwargs)
            self._slaves_session.append(_create_session(slave_engine))

        if "pool_recycle" in kwargs:
            # ping db, so that mysql won't goaway
            PeriodicCallback(self._ping_db, kwargs["pool_recycle"] * 1000).start()

        signals.call_finished.connect(self._remove)  # 注册信号,请求结束后remove
Example #19
0
def load_environment(global_conf, app_conf):
    """Configure the Pylons environment via the ``pylons.config``
    object
    """
    # Pylons paths
    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    paths = dict(root=root,
                 controllers=os.path.join(root, 'controllers'),
                 static_files=os.path.join(root, 'public'),
                 templates=[os.path.join(root, 'templates')])

    # Initialize config with the basic options
    config.init_app(global_conf, app_conf, package='romnyweb', paths=paths)

    config['routes.map'] = make_map()
    config['pylons.app_globals'] = app_globals.Globals()
    config['pylons.h'] = romnyweb.lib.helpers

    # Create the Mako TemplateLookup, with the default auto-escaping
    config['pylons.app_globals'].mako_lookup = TemplateLookup(
        directories=paths['templates'],
        error_handler=handle_mako_error,
        module_directory=os.path.join(app_conf['cache_dir'], 'templates'),
        input_encoding='utf-8', default_filters=['escape'],
        imports=['from webhelpers.html import escape'])

    # Setup the SQLAlchemy database engine
    engine = engine_from_config(config, 'sqlalchemy.')
    init_model(engine)
    def setUpClass(cls):
        from sqlalchemy import engine_from_config

        engine = engine_from_config({'url': 'sqlite://'}, prefix='')

        qry = open('monasca_api/tests/sqlite_alarm.sql', 'r').read()
        sconn = engine.raw_connection()
        c = sconn.cursor()
        c.executescript(qry)
        sconn.commit()
        c.close()
        cls.engine = engine

        def _fake_engine_from_config(*args, **kw):
            return cls.engine
        cls.fixture = fixtures.MonkeyPatch(
            'sqlalchemy.create_engine', _fake_engine_from_config)
        cls.fixture.setUp()

        metadata = MetaData()
        cls.nm = models.create_nm_model(metadata)
        cls._delete_nm_query = delete(cls.nm)
        cls._insert_nm_query = (insert(cls.nm)
                                .values(
                                    id=bindparam('id'),
                                    tenant_id=bindparam('tenant_id'),
                                    name=bindparam('name'),
                                    type=bindparam('type'),
                                    address=bindparam('address'),
                                    created_at=bindparam('created_at'),
                                    updated_at=bindparam('updated_at')))
Example #21
0
File: env.py Project: yehiaa/clinic
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.

    """

    # this callback is used to prevent an auto-migration from being generated
    # when there are no changes to the schema
    # reference: http://alembic.readthedocs.org/en/latest/cookbook.html
    def process_revision_directives(context, revision, directives):
        if getattr(config.cmd_opts, 'autogenerate', False):
            script = directives[0]
            if script.upgrade_ops.is_empty():
                directives[:] = []
                logger.info('No changes in schema detected.')

    engine = engine_from_config(config.get_section(config.config_ini_section),
                                prefix='sqlalchemy.',
                                poolclass=pool.NullPool)

    connection = engine.connect()
    context.configure(connection=connection,
                      target_metadata=target_metadata,
                      process_revision_directives=process_revision_directives,
                      **current_app.extensions['migrate'].configure_args)

    try:
        with context.begin_transaction():
            context.run_migrations()
    finally:
        connection.close()
Example #22
0
def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    engine = engine_from_config(settings, 'sqlalchemy.')
    initialize_sql(engine)
    session_factory = UnencryptedCookieSessionFactoryConfig('nilsbysite')
    config = Configurator(settings=settings, session_factory=session_factory)
    config.add_static_view('static', 'nilsby:static', cache_max_age=3600)
    
    # Auth routes
    config.add_route('user_new', '/signup')
    config.add_route('login', '/login')
    config.add_route('logout', '/logout')

    # Forum routes
    config.add_route('forum_index', '/forum')
    config.add_route('forum_view', '/forum/view/{id}')
    config.add_route('forum_post', '/forum/post')
    config.add_route('forum_reply', '/forum/reply/{post_id}')

    # User routes
    config.add_route('user_index', '/users')
    config.add_route('user_view', '/user/view/{id}')
    
    # Home route
    config.add_route('home', '/')
    config.scan()
    return config.make_wsgi_app()
Example #23
0
def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    authentication_policy = AuthTktAuthenticationPolicy('seekrit',
            callback=groupfinder)
    authorization_policy = ACLAuthorizationPolicy()
    config = Configurator(
        settings = settings
        , authentication_policy = authentication_policy
        , authorization_policy=authorization_policy
    )

    engine = engine_from_config(settings, 'sqlalchemy.')

    DBSession.configure(bind=engine)

    config.registry.registerUtility(DBSession, IDBSession)

    config.override_asset(
        to_override='hiero:templates/blog_index.mako',
        override_with='sontek:templates/blog_index.mako'
    )

    config.override_asset(
        to_override='hiero:templates/entry_detail.mako',
        override_with='sontek:templates/entry_detail.mako'
    )

    config.include('sontek.routes')

    config.scan()
    return config.make_wsgi_app()
def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine
    config = Configurator(settings=settings)
    config.add_static_view('static', 'static', cache_max_age=3600)

    config.add_route('index', '/')
    config.add_route('drink_page', '/drink/{item_id}')
    config.add_route('user_redirect', '/user')
    config.add_route('user_page', '/user/{username}')
    config.add_route('machine_page', '/machine/{machine_id}')
    config.add_route('autocomplete', '/autocomplete')
    config.add_route('drink_redirect', '/fordrink')

    config.add_route('api_item_usage', '/api/drink/{item_id}')
    config.add_route('api_user_usage', '/api/user/{username}')
    config.add_route('api_machine_usage', '/api/machine/{machine_id}')
    config.add_route('api_total_usage', '/api/total')
    config.add_route('api_pop_hours', '/api/hours')

    config.scan()
    return config.make_wsgi_app()
Example #25
0
def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    engine = engine_from_config(settings, 'sqlalchemy.')
    initialize_sql(engine)
    config = Configurator(settings=settings)
    config.add_translation_dirs('omcevmembership:locale/')
    config.add_static_view('static', 'omcevmembership:static', cache_max_age=3600)

    config.add_subscriber('omcevmembership.subscribers.add_base_template',
                          'pyramid.events.BeforeRender')
    # home /
    config.add_route('home', '/')
    config.add_view('omcevmembership.views.home_view',
                    route_name='home',
                    renderer='templates/home.pt')
    # /why
    config.add_route('why', '/why')
    config.add_view('omcevmembership.views.why_view',
                    route_name='why',
                    renderer='templates/why.pt')
    # /types
    config.add_route('types', '/types')
    config.add_view('omcevmembership.views.types_view',
                    route_name='types',
                    renderer='templates/membership_types.pt')

    # beitrittserklaerung
    config.add_route('beitrittserklaerung', '/beitrittserklaerung')
    config.add_view('omcevmembership.views.join_membership',
                    route_name='beitrittserklaerung',
                    renderer='templates/join.pt')
    return config.make_wsgi_app()
Example #26
0
def main(argv=sys.argv):
    if len(argv) != 2:
        usage(argv)

    config_uri = argv[1]

    settings = get_appsettings(config_uri)
    engine = engine_from_config(settings, 'sqlalchemy.')
    Session = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
    Session.configure(bind=engine)
    db = Session()

    with transaction.manager:
        testing = db.query(Update).filter_by(status=UpdateStatus.testing,
                                             request=None)
        for update in testing:
            # If this release does not have any testing requirements, skip it
            if not update.release.mandatory_days_in_testing:
                print('%s doesn\'t have mandatory days in testing' % update.release.name)
                continue
            # If this has already met testing requirements, skip it
            if update.met_testing_requirements:
                continue
            if update.meets_testing_requirements:
                print('%s now meets testing requirements' % update.title)
                text = config.get('testing_approval_msg') % update.days_in_testing
                update.comment(db, text, author='bodhi')
Example #27
0
def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    authn_policy = AuthTktAuthenticationPolicy(
        'sosecret', callback=groupfinder, hashalg='sha512')
    authz_policy = ACLAuthorizationPolicy()

    session_factory = session_factory = session_factory_from_settings(settings)

    config = Configurator(settings=settings,
                          root_factory='.models.RootFactory',
                          session_factory=session_factory)
    config.set_authentication_policy(authn_policy)
    config.set_authorization_policy(authz_policy)

    # Routes
    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_route('home_page', '/')
    config.add_route('login', '/login')
    config.add_route('logout', '/logout')

    # Admin
    config.add_route('admin_list_users', '/admin/users')
    config.add_route('admin_create_user', '/admin/users/create')
    config.add_route('admin_delete_user', '/admin/user/delete/{id}')
    config.add_route('admin_list_teams', '/admin/teams')
    config.add_route('admin_create_team', '/admin/teams/create')
    config.add_route('admin_delete_team', '/admin/teams/delete/{id}')

    config.scan()
    return config.make_wsgi_app()
Example #28
0
def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    settings['sqlalchemy.url'] = settings['cn.dialect'] + quote_plus(settings['sqlalchemy.url'])
    engine = engine_from_config(settings, 'sqlalchemy.')
    dbConfig['url'] = settings['sqlalchemy.url']
    
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine
    Base.metadata.create_all(engine)
    Base.metadata.reflect(views=True, extend_existing=False)

    config = Configurator(settings=settings)
    # Add renderer for datetime objects
    json_renderer = JSON()
    json_renderer.add_adapter(datetime, datetime_adapter)
    json_renderer.add_adapter(Decimal, decimal_adapter)
    json_renderer.add_adapter(bytes, bytes_adapter)
    config.add_renderer('json', json_renderer)

    # Set up authentication and authorization
    includeme(config)
 #   config.set_root_factory(SecurityRoot)

    # config.set_request_factory(request_factory)
    config.add_subscriber(add_cors_headers_response_callback, NewRequest)
    # Set the default permission level to 'read'
    config.set_default_permission('read')
    config.include('pyramid_tm')
    add_routes(config)
    config.scan()
    return config.make_wsgi_app()
Example #29
0
def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine

    #authn_policy = AuthTktAuthenticationPolicy(
    #    'sosecret', callback=groupfinder, hashalg='sha512')
    #authz_policy = ACLAuthorizationPolicy()

    config = Configurator(settings=settings, root_factory='webnews.models.mymodel.Accesses')
    config.set_authentication_policy(authn_policy)
    config.set_authorization_policy(authz_policy)    

    config.include('pyramid_jinja2')
    config.include('.models')
    config.include('.routes')
    	
    config.include('ps_alchemy')

    config.include(sacrud_settings)
    settings = config.registry.settings

    
    config.scan()
    return config.make_wsgi_app()
def main(argv=sys.argv):
    if len(argv) != 2:
        usage(argv)
    config_uri = argv[1]
    setup_logging(config_uri)
    settings = get_appsettings(config_uri)
    mkdir(settings['static_files'])
    # Create Ziggurat tables
    alembic_ini_file = 'alembic.ini'
    if not os.path.exists(alembic_ini_file):
        alembic_ini = ALEMBIC_CONF.replace('{{db_url}}',
                                           settings['sqlalchemy.url'])
        f = open(alembic_ini_file, 'w')
        f.write(alembic_ini)
        f.close()
    bin_path = os.path.split(sys.executable)[0]
    alembic_bin = os.path.join(bin_path, 'alembic')
    command = '%s upgrade head' % alembic_bin
    os.system(command)
    os.remove(alembic_ini_file)
    # Insert data
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    init_model()
    create_schemas(engine)
    Base.metadata.create_all(engine)
    initial_data.insert()
    transaction.commit()
Example #31
0
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.

    """
    engine = engine_from_config(
        config.get_section(config.config_ini_section),
        prefix='sqlalchemy.',
        poolclass=pool.NullPool
    )

    connection = engine.connect()
    context.configure(
        connection=connection,
        target_metadata=target_metadata
    )

    try:
        with context.begin_transaction():
            context.run_migrations()
    finally:
        connection.close()
Example #32
0
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.

    """

    # this callback is used to prevent an auto-migration from being generated
    # when there are no changes to the schema
    # reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
    def process_revision_directives(context, revision, directives):
        if getattr(config.cmd_opts, 'autogenerate', False):
            script = directives[0]
            if script.upgrade_ops.is_empty():
                directives[:] = []
                logger.info('No changes in schema detected.')

    connectable = engine_from_config(
        config.get_section(config.config_ini_section),
        prefix='sqlalchemy.',
        poolclass=pool.NullPool,
    )

    with connectable.connect() as connection:
        context.configure(
            connection=connection,
            target_metadata=target_metadata,
            process_revision_directives=process_revision_directives,
            render_as_batch=True,
            compare_type=True,
            **current_app.extensions['migrate'].configure_args
        )

        with context.begin_transaction():
            context.run_migrations()
Example #33
0
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.

    """
    alembic_config = config.get_section(config.config_ini_section)
    from app.main import app

    alembic_config['sqlalchemy.url'] = app.config['SQLALCHEMY_DATABASE_URI']

    engine = engine_from_config(alembic_config,
                                prefix='sqlalchemy.',
                                poolclass=pool.NullPool)

    connection = engine.connect()
    context.configure(connection=connection, target_metadata=target_metadata)

    try:
        with context.begin_transaction():
            context.run_migrations()
    finally:
        connection.close()
Example #34
0
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.

    """
    conf = config.get_section(config.config_ini_section)
    conf["sqlalchemy.url"] = get_url()
    connectable = engine_from_config(
        conf,
        prefix="sqlalchemy.",
        poolclass=pool.NullPool,
    )

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

        with context.begin_transaction():
            context.run_migrations()
Example #35
0
def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    settings['sqlalchemy.url'] = settings['cn.dialect'] + quote_plus(
        settings['sqlalchemy.url'])
    engine = engine_from_config(settings, 'sqlalchemy.')
    dbConfig['url'] = settings['sqlalchemy.url']
    dbConfig['siteName'] = settings['siteName']
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine
    Base.metadata.create_all(engine)
    Base.metadata.reflect(views=True, extend_existing=False)

    config = Configurator(settings=settings)
    config.include('.cors')
    config.add_cors_preflight_handler()

    config.add_static_view(name='static', path='static')

    # Add renderer for datetime objects
    json_renderer = JSON()
    json_renderer.add_adapter(datetime, datetime_adapter)
    json_renderer.add_adapter(Decimal, decimal_adapter)
    json_renderer.add_adapter(bytes, bytes_adapter)
    config.add_renderer('json', json_renderer)

    # Set up authentication and authorization
    includeme(config)
    config.set_root_factory(SecurityRoot)

    # Set the default permission level to 'read'
    config.set_default_permission('read')
    config.include('pyramid_tm')
    add_routes(config)
    config.scan()
    return config.make_wsgi_app()
Example #36
0
def main(global_config, **settings):
    """This function returns a Pyramid WSGI application."""

    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine
    authn_policy = AuthTktAuthenticationPolicy(
        secret='sosecret',
        callback=group_finder,
        hashalg='sha512',
    )
    authz_policy = ACLAuthorizationPolicy()
    config = Configurator(
        settings=settings,
        session_factory=my_session_factory,
        root_factory='okarchive.models.RootFactory',
    )
    config.set_authentication_policy(authn_policy)
    config.set_authorization_policy(authz_policy)
    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_static_view('deform', 'deform:static', cache_max_age=3600)
    config.include('pyramid_chameleon')
    config.scan()
    return config.make_wsgi_app()
Example #37
0
def main(argv=sys.argv):
    if len(argv) != 2:
        usage(argv)

    config_uri = argv[1]

    setup_logging(config_uri)
    log = logging.getLogger(__name__)

    settings = get_appsettings(config_uri)
    engine = engine_from_config(settings, 'sqlalchemy.')
    Session = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
    Session.configure(bind=engine)
    db = Session()

    setup_buildsystem(settings)

    with transaction.manager:
        now = datetime.utcnow()

        overrides = db.query(BuildrootOverride)
        overrides = overrides.filter(BuildrootOverride.expired_date == None)
        overrides = overrides.filter(BuildrootOverride.expiration_date < now)

        count = overrides.count()

        if not count:
            log.info("No active buildroot override to expire")
            return

        log.info("Expiring %d buildroot overrides...", count)

        for override in overrides:
            override.expire()
            db.add(override)
            log.info("Expired %s" % override.build.nvr)
Example #38
0
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.

    """

    # https://alembic.sqlalchemy.org/en/latest/cookbook.html#don-t-generate-empty-migrations-with-autogenerate
    def process_revision_directives(context, revision, directives):
        if config.cmd_opts.autogenerate:
            script = directives[0]
            if script.upgrade_ops.is_empty():
                directives[:] = []

    connectable = engine_from_config(
        config.get_section(config.config_ini_section),
        prefix="sqlalchemy.",
        poolclass=pool.NullPool,
        url=get_datasets_db_url(),
    )

    with connectable.connect() as connection:
        context.configure(
            connection=connection,
            target_metadata=target_metadata,
            version_table_schema='dataflow',
            include_object=include_object,
            include_schemas=True,
            process_revision_directives=process_revision_directives,
        )

        connection.execute("CREATE SCHEMA IF NOT EXISTS dataflow")

        with context.begin_transaction():
            context.run_migrations()
Example #39
0
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.

    """
    app_env = os.getenv('APP_ENV')
    if app_env:
        connectable = engine_from_config(
            config.get_section(config.config_ini_section),
            prefix=f"{app_env}.sqlalchemy.",
            poolclass=pool.NullPool,
        )

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

            with context.begin_transaction():
                context.run_migrations()
    else:
        raise KeyError('APP_ENV environment variable must be set to run database migrations')
Example #40
0
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.

    """

    # this callback is used to prevent an auto-migration from being generated
    # when there are no changes to the schema
    # reference: http://alembic.readthedocs.org/en/latest/cookbook.html
    def process_revision_directives(context, revision, directives):
        if getattr(config.cmd_opts, "autogenerate", False):
            script = directives[0]
            if script.upgrade_ops.is_empty():
                directives[:] = []
                logger.info("No changes in schema detected.")

    engine = engine_from_config(config.get_section(config.config_ini_section),
                                prefix="sqlalchemy.",
                                poolclass=pool.NullPool)

    connection = engine.connect()
    context.configure(
        connection=connection,
        target_metadata=target_metadata,
        process_revision_directives=process_revision_directives,
        transaction_per_migration=True,
        **current_app.extensions["migrate"].configure_args,
    )

    try:
        with context.begin_transaction():
            context.run_migrations()
    finally:
        connection.close()
Example #41
0
def main(argv=sys.argv):
    if len(argv) < 2:
        usage(argv)
    config_uri = argv[1]
    options = parse_vars(argv[2:])
    print("PODLIVA")
    setup_logging(config_uri)
    settings = get_appsettings(config_uri, options=options)

    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)

    udemy = UdemyAPI(settings)

    ids = udemy.get_course_ids('physics')
    for index, iid in enumerate(ids):
        print("Loading course #{}...".format(index + 1))
        course_id = udemy.get_parsed_course(iid)
        comments = udemy.get_parsed_course_comments(iid)
        with transaction.manager:
            course = DBSession.query(Course).get(course_id)
            course.comments.extend(comments)
            DBSession.flush()
        print("Course #{} is loaded!".format(index + 1))
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 = engine_from_config(
        config.get_section(config.config_ini_section),
        prefix="sqlalchemy.",
        poolclass=pool.NullPool,
    )

    with connectable.connect() as connection:
        context.configure(
            connection=connection,
            target_metadata=target_metadata,
            compare_type=True,
            include_object=include_object,
            version_table="{{ cookiecutter.plugin_name }}_alembic_version",
        )

        with context.begin_transaction():
            context.run_migrations()
Example #43
0
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 = engine_from_config(
        config.get_section(config.config_ini_section),
        prefix="sqlalchemy.",
        poolclass=pool.NullPool,
    )

    with connectable.connect() as connection:
        context.configure(
            connection=connection,
            target_metadata=target_metadata,
            include_schemas=True,  # New
            version_table_schema=target_metadata.schema,  # New
            include_object=include_object  # New
        )

        with context.begin_transaction():
            context.run_migrations()
Example #44
0
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 = engine_from_config(
        config.get_section(config.config_ini_section),
        prefix="sqlalchemy.",
        poolclass=pool.NullPool,
    )

    with connectable.connect() as connection:
        context.configure(
            connection=connection,
            target_metadata=target_metadata,
            render_as_batch=config.get_main_option("sqlalchemy.url").startswith(
                "sqlite:"
            ),
        )

        with context.begin_transaction():
            context.run_migrations()
Example #45
0
def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine
    authn_policy = AuthTktAuthenticationPolicy('sosecret',
                                               callback=groupfinder,
                                               hashalg='sha512')
    authz_policy = ACLAuthorizationPolicy()
    config = Configurator(settings=settings,
                          root_factory='tutorial.models.RootFactory')
    config.set_authentication_policy(authn_policy)
    config.set_authorization_policy(authz_policy)
    config.include('pyramid_chameleon')
    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_route('view_wiki', '/')
    config.add_route('login', '/login')
    config.add_route('logout', '/logout')
    config.add_route('view_page', '/{pagename}')
    config.add_route('add_page', '/add_page/{pagename}')
    config.add_route('edit_page', '/{pagename}/edit_page')
    config.scan()
    return config.make_wsgi_app()
Example #46
0
def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine
    config = Configurator(settings=settings,
                          root_factory='votabo.lib.security.RootFactory')
    config.add_static_view(name='static',
                           path='votabo:static',
                           cache_max_age=3600)

    configure_routes(config)
    configure_templates(config)
    configure_locale(config, settings)
    configure_cache(config, settings)
    configure_auth(config)
    configure_user(config)

    from .lib.hmom import HttpMethodOverrideMiddleware

    app = config.make_wsgi_app()
    app = HttpMethodOverrideMiddleware(app)
    return app
Example #47
0
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.

    """
    cmd_line_url = context.get_x_argument(as_dictionary=True).get('dbname')
    if cmd_line_url:
        engine = create_engine(cmd_line_url)
    else:
        engine = engine_from_config(config.get_section(
            config.config_ini_section),
                                    prefix='sqlalchemy.',
                                    poolclass=pool.NullPool)

    connection = engine.connect()
    context.configure(connection=connection, target_metadata=target_metadata)

    try:
        with context.begin_transaction():
            context.run_migrations()
    finally:
        connection.close()
Example #48
0
def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """

    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine
    Base.metadata.create_all(engine)

    config = Configurator(settings=settings)

    config.include('pyramid_chameleon')
    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_route('home', '/')

    config.include('prf')
    root = config.get_root_resource()
    root.add('iri', view=IrisView)

    global iris_classifier
    iris_classifier = IrisClassifier()

    config.scan()
    return config.make_wsgi_app()
Example #49
0
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 = engine_from_config(config.get_section(
        config.config_ini_section),
                                     prefix='sqlalchemy.',
                                     poolclass=pool.NullPool)

    with connectable.connect() as connection:
        context.configure(
            # uncomment to pick up column-type changes
            # compare_type=True,
            render_as_batch=True,
            connection=connection,
            include_object=include_object,
            target_metadata=target_metadata,
            version_table='alembic_version_apicrud')

        with context.begin_transaction():
            context.run_migrations()
Example #50
0
def run_migrations_online():
   
    def process_revision_directives(context, revision, directives):
        if getattr(config.cmd_opts, 'autogenerate', False):
            script = directives[0]
            if script.upgrade_ops.is_empty():
                directives[:] = []
                logger.info('No changes in schema detected.')

    engine = engine_from_config(config.get_section(config.config_ini_section),
                                prefix='sqlalchemy.',
                                poolclass=pool.NullPool)

    connection = engine.connect()
    context.configure(connection=connection,
                      target_metadata=target_metadata,
                      process_revision_directives=process_revision_directives,
                      **current_app.extensions['migrate'].configure_args)

    try:
        with context.begin_transaction():
            context.run_migrations()
    finally:
        connection.close()
Example #51
0
File: env.py Project: chessdb/api
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 = engine_from_config(
        config.get_section(config.config_ini_section),
        prefix="sqlalchemy.",
        poolclass=pool.NullPool,
    )
    retries = 0
    while True:
        try:
            retries += 1
            connection = connectable.connect()
        except Exception:
            if retries < DB_RETRY_LIMIT:
                time.sleep(DB_RETRY_INTERVAL)
            else:
                raise
        else:
            break

    with connection:
        context.configure(connection=connection,
                          target_metadata=target_metadata)

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

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

        with context.begin_transaction():
            context.run_migrations()
Example #52
0
def main(global_config, **settings):
    engine = engine_from_config(settings, prefix='sqlalchemy.')

    authn_policy = AuthTktAuthenticationPolicy(settings['auth.secret'],
                                               hashalg='sha512')
    authz_policy = ACLAuthorizationPolicy()

    config = Configurator(settings=settings,
                          authentication_policy=authn_policy,
                          authorization_policy=authz_policy)
    config.include('pyramid_chameleon')

    config.add_static_view('public', 'tdf:public')

    # Routes
    config.add_route('index', '/')
    config.add_route('login', '/login')
    config.add_route('logout', '/logout')

    # View Plugin
    config.scan('.app.views.index_view')
    config.scan('.app.views.authentication_view')

    return config.make_wsgi_app()
def main(argv=sys.argv):
    if len(argv) < 2:
        usage(argv)
    config_uri = argv[1]
    options = parse_vars(argv[2:])
    setup_logging(config_uri)
    settings = get_appsettings(config_uri, options=options)
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.create_all(engine)
    #with transaction.manager:
    #    model = MyModel(name='one', value=1)
    #    DBSession.add(model)

    c = Customers.add(session=DBSession,
                      name="GE",
                      description="General Electric Corp.")

    a = Accounts.add(
        session=DBSession,
        customer_id=c.id,
        name='GE Location X',
        description='The GE Plant at Location X',
    )
Example #54
0
def main(argv=sys.argv):
    if len(argv) < 2:
        usage(argv)
    config_uri = argv[1]
    map_id = argv[2]
    setup_logging(config_uri)
    settings = get_appsettings(config_uri)
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)

    # fill AdminZoneFinance with new variable set in MAPS_CONFIG
    map_ids = [MAPS_CONFIG.keys()] if map_id == 'ALL' else [map_id]

    for map_id in map_ids:
        config = MAPS_CONFIG[map_id]
        q = DBSession.query(AdminZoneFinance.data).filter(config['sql_filter'])
        store = AdminZoneFinance.data
        q.update(
            {
                store:
                store + hstore(map_id, cast(config['sql_variable'], String))
            },
            synchronize_session=False)
        transaction.commit()
Example #55
0
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.

    """
    if dburl:
        connectable = create_engine(dburl)
    else:
        connectable = engine_from_config(
            config.get_section(config.config_ini_section),
            prefix='sqlalchemy.',
            poolclass=pool.NullPool)


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

        with context.begin_transaction():
            context.run_migrations()
Example #56
0
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.

    """
    uri = os.environ.get('SQLALCHEMY_URI', None)

    if uri:
        connectable = create_engine(uri)
    else:
        connectable = engine_from_config(
            config.get_section(config.config_ini_section),
            prefix="sqlalchemy.",
            poolclass=pool.NullPool,
        )

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

        with context.begin_transaction():
            context.run_migrations()
Example #57
0
def initialize_db(config):
    """
    Initialize the database using the given configuration.

    This *must* be called before you can use the :data:`Session` object.

    Args:
        config (dict): The Bodhi server configuration dictionary.

    Returns:
        sqlalchemy.engine: The database engine created from the configuration.
    """
    # The SQLAlchemy database engine. This is constructed using the value of
    # ``DB_URL`` in :data:`config``. Note: A copy is provided since ``engine_from_config``
    # uses ``pop``.
    engine = engine_from_config(config.copy(), 'sqlalchemy.')
    # When using SQLite we need to make sure foreign keys are enabled:
    # http://docs.sqlalchemy.org/en/latest/dialects/sqlite.html#foreign-key-support
    if config['sqlalchemy.url'].startswith('sqlite:'):
        event.listen(
            engine, 'connect', lambda db_con, con_record: db_con.execute(
                'PRAGMA foreign_keys=ON'))
    Session.configure(bind=engine)
    return engine
Example #58
0
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.

    """
    # Load db config on top of alembic config. This enables CLI spec of
    # database to migrate.
    alembic_config = config.get_section(config.config_ini_section)
    db_config = config.get_section(db_name)
    for key in db_config:
        alembic_config[key] = db_config[key]

    connectable = engine_from_config(alembic_config,
                                     prefix='sqlalchemy.',
                                     poolclass=pool.NullPool)

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

        with context.begin_transaction():
            context.run_migrations()
def session():
    """
    makes a copy of the oil database, and creates a session object to it

    When done, closes the session and deletes the DB file

    This is kind kludgy, but hopefully works
    """

    orig = os.path.join(os.path.split(oil_library.__file__)[0], "OilLib.db")
    # NOTE: this should probably be in a temp dir...
    db_file = "./OilLibCopy.db"
    # make a copy:
    shutil.copy(orig, db_file)

    settings = {"sqlalchemy.url": 'sqlite:///{0}'.format(db_file)}
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    DBSession.configure(extension=ZopeTransactionExtension())

    yield DBSession

    DBSession.close()
    os.remove(db_file)
Example #60
0
def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    config = Configurator(root_factory=Root,
                          settings=settings,
                          authorization_policy=authorization_policy,
                          authentication_policy=authentication_policy)
    config.set_session_factory(session_factory_from_settings(settings))
    config.add_route('announce', '/{passkey}/announce')
    config.add_route('addtorrent', '/addtorrent', factory=Protected)
    config.add_route('signup', '/signup')
    config.add_route('login', '/login')
    config.add_route('index', '/')
    config.add_route('browse', '/browse')
    config.add_route('setup', '/setup')
    config.add_route('get_torrent', '/get_torrent/{id}', factory=Protected)
    config.add_route('logout', '/logout')
    config.add_static_view('static', 'hermes:static')
    config.scan('hermes.views')
    config.scan('hermes.model')
    engine = engine_from_config(settings, 'sqlalchemy.')
    log.error(engine)
    initialize_sql(engine)
    return config.make_wsgi_app()