def _get_collection(model, insecure=False, limit=None, marker=None, sort_keys=None, sort_dirs=None, fields=None, **filters): columns = ( tuple([getattr(model, f) for f in fields if hasattr(model, f)]) if fields else () ) query = (db_base.model_query(model, columns) if insecure else _secure_query(model, *columns)) query = db_filters.apply_filters(query, model, **filters) query = _paginate_query( model, limit, marker, sort_keys, sort_dirs, query ) try: return query.all() except Exception as e: raise exc.DBError( "Failed when querying database, error type: %s, " "error message: %s" % (e.__class__.__name__, str(e)) )
def rollback_tx(): """Rolls back previously started database transaction.""" ses = _get_thread_local_session() if not ses: raise exc.DBError( "Nothing to roll back. Database transaction has not been started.") ses.rollback()
def commit_tx(): """Commits previously started database transaction.""" ses = _get_thread_local_session() if not ses: raise exc.DBError("Nothing to commit. Database transaction" " has not been previously started.") ses.commit()
def create_job(values, session=None): job = models.Job() job.update(values) try: job.save(session=session) except oslo_db_exc.DBDuplicateEntry as e: raise exc.DBError("Duplicate entry for Job: %s" % e.columns) return job
def create_execution(values, session=None): execution = models.Execution() execution.update(values.copy()) try: execution.save(session=session) except oslo_db_exc.DBDuplicateEntry as e: raise exc.DBError("Duplicate entry for Execution: %s" % e.columns) return execution
def create_runtime(values, session=None): runtime = models.Runtime() runtime.update(values.copy()) try: runtime.save(session=session) except oslo_db_exc.DBDuplicateEntry as e: raise exc.DBError("Duplicate entry for Runtime: %s" % e.columns) return runtime
def create_function(values, session=None): func = models.Function() func.update(values.copy()) try: func.save(session=session) except oslo_db_exc.DBDuplicateEntry as e: raise exc.DBError("Duplicate entry for Function: %s" % e.columns) return func
def create_webhook(values, session=None): webhook = models.Webhook() webhook.update(values.copy()) try: webhook.save(session=session) except oslo_db_exc.DBDuplicateEntry as e: raise exc.DBError("Duplicate entry for webhook: %s" % e.columns) return webhook
def start_tx(): """Starts transaction. Opens new database session and starts new transaction assuming there wasn't any opened sessions within the same thread. """ if _get_thread_local_session(): raise exc.DBError("Database transaction has already been started.") _set_thread_local_session(get_session())
def create_function_worker(values, session=None): mapping = models.FunctionWorkers() mapping.update(values.copy()) try: mapping.save(session=session) except oslo_db_exc.DBDuplicateEntry as e: raise exc.DBError("Duplicate entry for FunctionWorkers: %s" % e.columns) return mapping
def create_function_alias(session=None, **kwargs): alias = models.FunctionAlias() alias.update(kwargs.copy()) try: alias.save(session=session) except oslo_db_exc.DBDuplicateEntry as e: raise exc.DBError("Duplicate entry for function_aliases: %s" % e.columns) return alias
def drop_db(): global _initialized with _SCHEMA_LOCK: if not _initialized: return try: models.Function.metadata.drop_all(db_base.get_engine()) _initialized = False except Exception as e: raise exc.DBError("Failed to drop database: %s" % str(e))
def setup_db(): global _initialized with _SCHEMA_LOCK: if _initialized: return try: models.Function.metadata.create_all(db_base.get_engine()) _initialized = True except sa.exc.OperationalError as e: raise exc.DBError("Failed to setup database: %s" % str(e))
def end_tx(): """Ends transaction. Ends current database transaction. It rolls back all uncommitted changes and closes database session. """ ses = _get_thread_local_session() if not ses: raise exc.DBError("Database transaction has not been started.") if ses.dirty: rollback_tx() ses.close() _set_thread_local_session(None)
def increase_function_version(function_id, old_version, session=None, **kwargs): """This function is supposed to be invoked within locking section.""" version = models.FunctionVersion() kwargs.update({ "function_id": function_id, "version_number": old_version + 1 }) version.update(kwargs.copy()) try: version.save(session=session) except oslo_db_exc.DBDuplicateEntry as e: raise exc.DBError("Duplicate entry for function_versions: %s" % e.columns) return version