def annotate_project(annotation: Dict[str, str], config: BaseConfig) -> str: try: engine = get_engine(config) _projects = get_projects(config) _project = annotation.get('project') _description = annotation.get('description') _key = annotation.get('key') assert _project in [x.get('project_id') for x in _projects ], f'Project {_project} does not exist' with engine.begin() as connection: connection.execute(project.update().where( project.c.project_id == _project).values( description=_description, project_key=_key)) message = f'Annotated project {_project}' logging.info(message) return message except Exception as e: logging.error(str(e)) raise e
def sqlite_writer(row: Dict[str, Any], config: BaseConfig) -> None: database = config.sqlite.database try: assert os.path.isfile( database), 'Database file {database} does not exist' engine = get_engine(config) # Override pysqlite's default transaction behaviour # cf. https://docs.sqlalchemy.org/en/latest/dialects/sqlite.html @event.listens_for(engine, "connect") def do_connect(dbapi_connection, connection_record): # disable pysqlite's emitting of the BEGIN statement entirely. # also stops it from emitting COMMIT before any DDL. dbapi_connection.isolation_level = None cursor = dbapi_connection.cursor() cursor.execute("PRAGMA foreign_keys=ON") cursor.close() @event.listens_for(engine, "begin") def do_begin(connection): # emit our own BEGIN connection.execute("BEGIN EXCLUSIVE") with engine.begin() as connection: connection.execute(worklog.insert(), row) except Exception as e: logging.error(str(e)) raise e
def modify_worklog(modifier: str, config: BaseConfig) -> str: try: engine = get_engine(config) delta = _parse_modifier(modifier) with engine.begin() as connection: select_query = worklog.select().order_by( worklog.c.worklog_id.desc()).limit(1) select_row = connection.execute(select_query).fetchone() _id = select_row['worklog_id'] _timestamp = select_row['timestamp'] + delta connection.execute(worklog.update().where( worklog.c.worklog_id == _id).values(timestamp=_timestamp)) message = f'Modified last worklog: {select_row["timestamp"]} → {_timestamp}' logging.info( f'Modified last worklog: {select_row["timestamp"]} → {_timestamp}' ) return message except Exception as e: logging.error(str(e)) raise e
def postgresql_writer(row: Dict[str, Any], config: BaseConfig) -> None: try: engine = get_engine(config) with engine.begin() as connection: connection.execute(worklog.insert(), row) except Exception as e: logging.error(str(e)) raise e
def test_database(prepared_config, tmpdir) -> Tuple[BaseConfig, Engine, MetaData]: """Prepare a test database""" _config = prepared_config _config.set_override(name='driver', override='sqlite', group='backend') _config.set_override(name='database', override=os.path.join(tmpdir, 'test_database.sqlite'), group='sqlite') _engine = get_engine(_config) yield _config, _engine, _metadata
def insert_projects(projects: Sequence[Dict[str, str]], config: BaseConfig) -> None: try: engine = get_engine(config) with engine.begin() as connection: connection.execute(project.insert(), projects) except Exception as e: logging.error(str(e)) raise e
def remove_projects(projects: Sequence[str], config: BaseConfig) -> None: try: engine = get_engine(config) with engine.begin() as connection: connection.execute(project.update().where( project.c.project_id.in_(projects)).values( deleted=datetime.now())) except Exception as e: logging.error(str(e)) raise e
def _get_actions(config: BaseConfig) -> Sequence[Union[Tuple[str, Any], None]]: try: engine = get_engine(config) with engine.begin() as connection: result = connection.execute( action.select().where(action.c.deleted == None)) return result.fetchall() except Exception as e: logging.error(str(e)) raise e
def remove_worklog(config: BaseConfig) -> str: try: engine = get_engine(config) with engine.begin() as connection: select_query = worklog.select().order_by( worklog.c.worklog_id.desc()).limit(1) select_row = connection.execute(select_query).fetchone() _id = select_row['worklog_id'] connection.execute( worklog.delete().where(worklog.c.worklog_id == _id)) message = 'Removed last worklog' logging.info(message) return message except Exception as e: logging.error(str(e)) raise e