def db_session(request, config): """Get a standalone database session for preparing database state.""" from h import db engine = db.make_engine(config.registry.settings) session = db.Session(bind=engine) try: yield session finally: session.close() engine.dispose()
def db_session(request, settings): """SQLAlchemy session.""" transaction.commit() db.Session.remove() engine = db.make_engine(settings) db.bind_engine(engine, should_create=True, should_drop=True) api_db.use_session(db.Session) return db.Session
def _init_db(settings): engine = db.make_engine(settings) # If the alembic_version table is present, then the database is managed by # alembic, and we shouldn't call `db.init`. try: engine.execute('select 1 from alembic_version') except sqlalchemy.exc.ProgrammingError: log.info("initializing database") db.init(engine, should_create=True, authority=settings['h.authority']) else: log.info("detected alembic_version table, skipping db initialization")
def db_session(request, settings): """SQLAlchemy session.""" engine = db.make_engine(settings) db.bind_engine(engine, should_create=True, should_drop=True) def destroy(): transaction.commit() db.Session.remove() request.addfinalizer(destroy) return db.Session
def remove_old_flags(event): """Remove old feature flags from the database.""" # Skip this if we're in a script, not actual app startup. See the comment # in h.cli:main for an explanation. if 'H_SCRIPT' in os.environ: return engine = db.make_engine(event.app.registry.settings) session = db.Session(bind=engine) Feature.remove_old_flags(session) session.commit() session.close() engine.dispose()
def main(): args = parser.parse_args() request = Request.blank('/') env = paster.bootstrap(args.config_uri, request=request) request.root = env['root'] engine = db.make_engine(request.registry.settings) Session.configure(bind=engine) APIBase.query = Session.query_property() if 'DEBUG_QUERY' in os.environ: logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO) migrate_annotations(request.es) print('') delete_nonexisting_annotations(request.es)
def _init_db(settings): engine = db.make_engine(settings) # If the alembic_version table is present, then the database is managed by # alembic, and we shouldn't call `db.init`. try: engine.execute("select 1 from alembic_version") except sqlalchemy.exc.ProgrammingError: log.info("initializing database") db.init( engine, should_create=True, authority=text_type(settings["h.authority"]) ) # Stamp the database with the current schema version so that future # migrations start from the correct point. alembic_cfg = alembic.config.Config("conf/alembic.ini") alembic.command.stamp(alembic_cfg, "head") else: log.info("detected alembic_version table, skipping db initialization")
def db_engine(): from h import db engine = db.make_engine(TEST_SETTINGS) yield engine engine.dispose()
def setup_database(settings): """Set up the database connection and create tables.""" engine = db.make_engine(settings) db.bind_engine(engine, should_create=True, should_drop=True) api_db.use_session(db.Session)
def _get_session(settings): engine = db.make_engine(settings) return db.Session(bind=engine)
def get_session(settings): """Get a DB session from the provided settings.""" return db.Session(bind=db.make_engine(settings))