From 4fe59319b6f15764e6961dcde00a921abf1b8a22 Mon Sep 17 00:00:00 2001 From: Matteo Campinoti Date: Fri, 1 Nov 2024 10:27:31 +0100 Subject: [PATCH 1/7] models.file:file.original_name - rename to original_path and save the entire original relative_path --- acacore/models/file.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/acacore/models/file.py b/acacore/models/file.py index 9675651..f6ea562 100644 --- a/acacore/models/file.py +++ b/acacore/models/file.py @@ -90,7 +90,7 @@ class File(BaseModel): parent: UUID4 | None = None processed: bool = False lock: bool = False - original_name: str + original_path: Path processed_names: list[str] = Field(default_factory=list) root: Path | None = DBField(None, ignore=True) @@ -99,7 +99,7 @@ class File(BaseModel): @classmethod def _model_validator(cls, data: dict): if isinstance(data, dict): - data["original_name"] = data.get("original_name", "").strip() or Path(data["relative_path"]).name + data["original_path"] = data.get("original_path", "").strip() or data["relative_path"] return data # noinspection PyNestedDecorators From a3f7fe13a68c78e38ecae7c6dcaf47b98d058b5a Mon Sep 17 00:00:00 2001 From: Matteo Campinoti Date: Wed, 6 Nov 2024 12:17:26 +0100 Subject: [PATCH 2/7] database.upgrade:upgrade_3_3_1to3_3_2 - upgrade original_name to original_path Uses the same algorithm as the 3.3.0 upgrade. --- acacore/database/upgrade.py | 64 +++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/acacore/database/upgrade.py b/acacore/database/upgrade.py index 9d9b584..7965b0c 100644 --- a/acacore/database/upgrade.py +++ b/acacore/database/upgrade.py @@ -154,12 +154,14 @@ def upgrade_3to3_0_2(conn: Connection) -> Version: return set_db_version(conn, Version("3.0.2")) +# noinspection SqlResolve def upgrade_3_0_2to3_0_6(conn: Connection) -> Version: conn.execute("update Files set action = 'ignore' where action = 'template'") conn.commit() return set_db_version(conn, Version("3.0.6")) +# noinspection SqlResolve def upgrade_3_0_6to3_0_7(conn: Connection) -> Version: def convert_action_data(data: dict) -> dict | None: if (reidentify := data.get("reidentify")) and reidentify.get("on_fail"): @@ -181,6 +183,7 @@ def convert_action_data(data: dict) -> dict | None: return set_db_version(conn, Version("3.0.7")) +# noinspection SqlResolve def upgrade_3_1to3_2(conn: Connection) -> Version: def convert_action_data(data: dict) -> dict: if not data.get("convert"): @@ -222,6 +225,7 @@ def upgrade_3_2to3_3(conn: Connection) -> Version: if not conn.execute("select 1 from pragma_table_info('Files') where name = 'processed_names'").fetchone(): conn.execute("alter table Files add column processed_names text default '[]'") + # noinspection SqlResolve,DuplicatedCode def _find_original_name(uuid: str, relative_path: str) -> str: original_path: Path = Path(relative_path) original_name: str = original_path.name @@ -292,6 +296,64 @@ def upgrade_3_3to3_3_1(conn: Connection) -> Version: return set_db_version(conn, Version("3.3.1")) +# noinspection SqlResolve +def upgrade_3_3_1to3_3_2(conn: Connection) -> Version: + if conn.execute("select 1 from pragma_table_info('Files') where name = 'original_name'").fetchone(): + conn.execute("alter table Files rename column original_name to original_path") + + # noinspection SqlResolve,DuplicatedCode + def _find_original_path(uuid: str, relative_path: str) -> str: + original_path: Path = Path(relative_path) + + for [data_raw] in conn.execute( + "select data from History" + " where uuid = ? and data is not null and data not in ('', '\"\"', 'null', '[]', '{}')" + " order by time desc", + (uuid,), + ): + try: + data = loads(data_raw) + except JSONDecodeError: + continue + + if ( + not isinstance(data, list) + or len(data) != 2 + or not isinstance(data[0], str) + or not isinstance(data[1], str) + ): + continue + + a: str = data[0] + b: str = data[1] + + if a == str(original_path): + original_path = Path(b) + elif b == str(original_path): + original_path = Path(a) + elif a == original_path.name: + original_path = original_path.with_name(b) + elif b == original_path.name: + original_path = original_path.with_name(a) + + return str(original_path) + + # noinspection SqlWithoutWhere + conn.execute("update Files set original_path = relative_path") + + conn.executemany( + "update Files set original_path = ? where uuid = ?", + ( + (_find_original_path(uuid, relative_path), uuid) + for uuid, relative_path in conn.execute( + "select distinct f.uuid, f.relative_path from Files f join History h where h.uuid = f.uuid" + ) + ), + ) + + return set_db_version(conn, Version("3.3.0")) + + def get_upgrade_function(current_version: Version, latest_version: Version) -> Callable[[Connection], Version]: if current_version < Version("2.0.0"): return upgrade_1to2 @@ -311,6 +373,8 @@ def get_upgrade_function(current_version: Version, latest_version: Version) -> C return upgrade_3_2to3_3 elif current_version < Version("3.3.1"): return upgrade_3_3to3_3_1 + elif current_version < Version("3.3.2"): + return upgrade_3_3_1to3_3_2 elif current_version < latest_version: return lambda c: set_db_version(c, Version(__version__)) else: From d97c4d3b0f9d8cbf4b10950959e4066ce7552d93 Mon Sep 17 00:00:00 2001 From: Matteo Campinoti Date: Wed, 6 Nov 2024 12:18:03 +0100 Subject: [PATCH 3/7] version - patch 3.3.1 > 3.3.2 --- acacore/__version__.py | 2 +- pyproject.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/acacore/__version__.py b/acacore/__version__.py index ff04168..3e2d550 100644 --- a/acacore/__version__.py +++ b/acacore/__version__.py @@ -1 +1 @@ -__version__ = "3.3.1" +__version__ = "3.3.2" diff --git a/pyproject.toml b/pyproject.toml index fbb484a..89c2c4a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "acacore" -version = "3.3.1" +version = "3.3.2" description = "" authors = ["Matteo Campinoti "] license = "GPL-3.0" From a21f0202046ffe58b6785bd7d69ac5969bcb8e4c Mon Sep 17 00:00:00 2001 From: Matteo Campinoti Date: Wed, 6 Nov 2024 12:28:13 +0100 Subject: [PATCH 4/7] database.upgrade:upgrade_3_3_1to3_3_2 - fix incorrect version --- acacore/database/upgrade.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/acacore/database/upgrade.py b/acacore/database/upgrade.py index 7965b0c..e1a9182 100644 --- a/acacore/database/upgrade.py +++ b/acacore/database/upgrade.py @@ -351,7 +351,7 @@ def _find_original_path(uuid: str, relative_path: str) -> str: ), ) - return set_db_version(conn, Version("3.3.0")) + return set_db_version(conn, Version("3.3.2")) def get_upgrade_function(current_version: Version, latest_version: Version) -> Callable[[Connection], Version]: From 42c7fa8c69c5ac930fac9403c064e43203c536d9 Mon Sep 17 00:00:00 2001 From: Matteo Campinoti Date: Wed, 6 Nov 2024 13:25:56 +0100 Subject: [PATCH 5/7] database.upgrade:get_db_version - ignore SqlResolve inspection --- acacore/database/upgrade.py | 1 + 1 file changed, 1 insertion(+) diff --git a/acacore/database/upgrade.py b/acacore/database/upgrade.py index e1a9182..32fe1b3 100644 --- a/acacore/database/upgrade.py +++ b/acacore/database/upgrade.py @@ -20,6 +20,7 @@ from .files_db import FileDB +# noinspection SqlResolve def get_db_version(conn: Connection) -> Version | None: if res := conn.execute("select VALUE from Metadata where KEY like 'version'").fetchone(): return Version(res[0]) From c1bf2999139a9d7033a89b8ac36ae267cb342e12 Mon Sep 17 00:00:00 2001 From: Matteo Campinoti Date: Wed, 6 Nov 2024 13:29:12 +0100 Subject: [PATCH 6/7] models.file:File._model_validator - improve check for original path --- acacore/models/file.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/acacore/models/file.py b/acacore/models/file.py index f6ea562..a1411e6 100644 --- a/acacore/models/file.py +++ b/acacore/models/file.py @@ -99,7 +99,12 @@ class File(BaseModel): @classmethod def _model_validator(cls, data: dict): if isinstance(data, dict): - data["original_path"] = data.get("original_path", "").strip() or data["relative_path"] + if (op := data.get("original_path", None)) and isinstance(op, Path): + data["original_path"] = op + elif isinstance(op, str) and op.strip(): + data["original_path"] = Path(op) + else: + data["original_path"] = data["relative_path"] return data # noinspection PyNestedDecorators From 3de3b5ce85c7041bb8067c53c25263e0b501e0ac Mon Sep 17 00:00:00 2001 From: Matteo Campinoti Date: Wed, 6 Nov 2024 13:31:40 +0100 Subject: [PATCH 7/7] models.file:File._model_validator - remove redundant default in dict.get call --- acacore/models/file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/acacore/models/file.py b/acacore/models/file.py index a1411e6..56e9665 100644 --- a/acacore/models/file.py +++ b/acacore/models/file.py @@ -99,7 +99,7 @@ class File(BaseModel): @classmethod def _model_validator(cls, data: dict): if isinstance(data, dict): - if (op := data.get("original_path", None)) and isinstance(op, Path): + if (op := data.get("original_path")) and isinstance(op, Path): data["original_path"] = op elif isinstance(op, str) and op.strip(): data["original_path"] = Path(op)