From e68c1776c43a82911e670b5e6ac89631e6057183 Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 08:31:17 +0800 Subject: [PATCH] Code refactor --- api/operation.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/api/operation.py b/api/operation.py index 92c8d67..53d1f45 100644 --- a/api/operation.py +++ b/api/operation.py @@ -1,6 +1,14 @@ from psycopg.rows import dict_row from .connection import g_conn_dict as conn +def _get_parents(name: str, id: int) -> list[int]: + ids = [id] + with conn[name].cursor(row_factory=dict_row) as cur: + while ids[-1] != 0: + cur.execute(f"select parent from operation where id = {ids[-1]}") + ids.append(int(cur.fetchone()['parent'])) + return ids + def _get_current_operation(name: str) -> int: with conn[name].cursor(row_factory=dict_row) as cur: cur.execute(f"select id from current_operation") @@ -90,15 +98,6 @@ def take_snapshot(name: str, tag: str) -> None: parent = _get_current_operation(name) cur.execute(f"insert into snapshot_operation (id, tag) values ({curr}, '{tag}')") - -def _get_parents(name: str, id: int) -> list[int]: - ids = [id] - with conn[name].cursor(row_factory=dict_row) as cur: - while ids[-1] != 0: - cur.execute(f"select parent from operation where id = {ids[-1]}") - ids.append(int(cur.fetchone()['parent'])) - return ids - def pick_snapshot(name: str, tag: str) -> None: if tag == None or tag == '': print('Non empty tag is expected!')