From b52f2dd324120190aeac134acbe14011ee7641ce Mon Sep 17 00:00:00 2001 From: Matteo Campinoti Date: Fri, 16 Aug 2024 13:48:17 +0200 Subject: [PATCH 01/16] models.reference_files:TActionType - remove "template" --- acacore/models/reference_files.py | 1 - 1 file changed, 1 deletion(-) diff --git a/acacore/models/reference_files.py b/acacore/models/reference_files.py index 0f3d6dd..39ed117 100644 --- a/acacore/models/reference_files.py +++ b/acacore/models/reference_files.py @@ -14,7 +14,6 @@ TActionType = Literal[ "convert", "extract", - "template", "manual", "rename", "ignore", From 7c6b5158bbecf84f54c203f694448e87132d4dc2 Mon Sep 17 00:00:00 2001 From: Matteo Campinoti Date: Mon, 19 Aug 2024 10:28:19 +0200 Subject: [PATCH 02/16] database.upgrade:upgrade - do not print versions --- acacore/database/upgrade.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/acacore/database/upgrade.py b/acacore/database/upgrade.py index cc69bfa..756328b 100644 --- a/acacore/database/upgrade.py +++ b/acacore/database/upgrade.py @@ -181,6 +181,4 @@ def upgrade(db: FileDB): while current_version < latest_version: update_function = get_upgrade_function(current_version, latest_version) - print(current_version, end=" ") current_version = update_function(db) - print(current_version) From 870c48e24f6a5cb13a13800bc151ea26b690a9da Mon Sep 17 00:00:00 2001 From: Matteo Campinoti Date: Tue, 20 Aug 2024 08:40:24 +0200 Subject: [PATCH 03/16] database.base:Table - add offset parameter to select methods --- acacore/database/base.py | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/acacore/database/base.py b/acacore/database/base.py index 6914f26..09fe65b 100644 --- a/acacore/database/base.py +++ b/acacore/database/base.py @@ -283,6 +283,7 @@ def select( columns: list[Column | SelectColumn] | None = None, where: str | None = None, order_by: list[tuple[str | Column, str]] | None = None, + offset: int | None = None, limit: int | None = None, parameters: list[Any | None] | None = None, ) -> Cursor: @@ -293,6 +294,7 @@ def select( :param where: A WHERE expression, defaults to None. :param order_by: A list tuples containing one column (either as Column or string) and a sorting direction ("ASC", or "DESC"), defaults to None. + :param offset: The number of rows skip, defaults to None. :param limit: The number of rows to limit the results to, defaults to None. :param parameters: Values to substitute in the SELECT expression, both in the `where` and SelectColumn statements, defaults to None. @@ -316,6 +318,9 @@ def select( order_statements = [f"{c.name if isinstance(c, Column) else c} {s}" for c, s in order_by] statement += f" ORDER BY {','.join(order_statements)}" + if offset is not None: + statement += f" OFFSET {offset}" + if limit is not None: statement += f" LIMIT {limit}" @@ -432,6 +437,7 @@ def select( model: Type[M] | None = None, where: str | None = None, order_by: list[tuple[str | Column, str]] | None = None, + offset: int | None = None, limit: int | None = None, parameters: list[Any] | None = None, ) -> ModelCursor[M]: @@ -442,21 +448,14 @@ def select( :param where: A WHERE expression, defaults to None. :param order_by: A list tuples containing one column (either as Column or string) and a sorting direction ("ASC", or "DESC"), defaults to None. + :param offset: The number of rows skip, defaults to None. :param limit: The number of rows to limit the results to, defaults to None. :param parameters: Values to substitute in the SELECT expression, both in the `where` and SelectColumn statements, defaults to None. :return: A Cursor object wrapping the SQLite cursor returned by the SELECT transaction. """ return ModelCursor[M]( - super() - .select( - model_to_columns(model or self.model), - where, - order_by, - limit, - parameters, - ) - .cursor, + super().select(model_to_columns(model or self.model), where, order_by, offset, limit, parameters).cursor, model or self.model, self, ) @@ -695,6 +694,7 @@ def select( columns: list[Column | SelectColumn] | None = None, where: str | None = None, order_by: list[tuple[str | Column, str]] | None = None, + offset: int | None = None, limit: int | None = None, parameters: list[Any] | None = None, ) -> Cursor: @@ -705,6 +705,7 @@ def select( :param where: A WHERE expression, defaults to None. :param order_by: A list tuples containing one column (either as Column or string) and a sorting direction ("ASC", or "DESC"), defaults to None. + :param offset: The number of rows skip, defaults to None. :param limit: The number of rows to limit the results to, defaults to None. :param parameters: Values to substitute in the SELECT expression, both in the `where` and SelectColumn statements, defaults to None. @@ -724,7 +725,7 @@ def select( ) for c in map(SelectColumn.from_column, self.columns) ] - return super().select(columns, where, order_by, limit, parameters) + return super().select(columns, where, order_by, offset, limit, parameters) def insert(self, *_args, **_kwargs): """ @@ -793,19 +794,12 @@ def select( model: Type[M] | None = None, where: str | None = None, order_by: list[tuple[str | Column, str]] | None = None, + offset: int | None = None, limit: int | None = None, parameters: list[Any] | None = None, ) -> ModelCursor[M]: return ModelCursor[M]( - super() - .select( - model_to_columns(model or self.model), - where, - order_by, - limit, - parameters, - ) - .cursor, + super().select(model_to_columns(model or self.model), where, order_by, offset, limit, parameters).cursor, model or self.model, self, ) From 2fbb10f78884e43b873a6899ac3743f84cd8c502 Mon Sep 17 00:00:00 2001 From: Matteo Campinoti Date: Tue, 20 Aug 2024 08:49:58 +0200 Subject: [PATCH 04/16] models.reference_files:TTemplateType - add "extracted-archive" --- acacore/models/reference_files.py | 1 + 1 file changed, 1 insertion(+) diff --git a/acacore/models/reference_files.py b/acacore/models/reference_files.py index 39ed117..2484c0a 100644 --- a/acacore/models/reference_files.py +++ b/acacore/models/reference_files.py @@ -27,6 +27,7 @@ "duplicate", "not-preservable", "not-convertable", + "extracted-archive", ] ActionTypeEnum: tuple[TActionType, ...] = get_type_args(TActionType) From 0d42c553e17d5817b3d081bde72224574bd91626 Mon Sep 17 00:00:00 2001 From: Matteo Campinoti Date: Tue, 20 Aug 2024 09:13:20 +0200 Subject: [PATCH 05/16] database.upgrade - only use Connection methods to select and update database data --- acacore/database/upgrade.py | 92 +++++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 40 deletions(-) diff --git a/acacore/database/upgrade.py b/acacore/database/upgrade.py index 756328b..266a884 100644 --- a/acacore/database/upgrade.py +++ b/acacore/database/upgrade.py @@ -1,5 +1,6 @@ from json import dumps from json import loads +from sqlite3 import Connection from sqlite3 import DatabaseError from sqlite3 import Row from typing import Any @@ -9,27 +10,28 @@ from acacore.__version__ import __version__ -from .files_db import FileDB - __all__ = [ "upgrade", "is_latest", ] -def get_db_version(db: FileDB) -> Version: - return Version(db.metadata.select().version) +from .files_db import FileDB + + +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]) + return None -def set_db_version(db: FileDB, version: Version) -> Version: - metadata = db.metadata.select() - metadata.version = str(version) - db.metadata.update(metadata) - db.commit() +def set_db_version(conn: Connection, version: Version) -> Version: + conn.execute("insert or replace into Metadata (KEY, VALUE) values (?, ?)", ("version", version)) + conn.commit() return version -def get_upgrade_function(current_version: Version, latest_version: Version) -> Callable[[FileDB], Version]: +def get_upgrade_function(current_version: Version, latest_version: Version) -> Callable[[Connection], Version]: if current_version < Version("2.0.0"): return upgrade_1to2 elif current_version < Version("2.0.2"): @@ -43,21 +45,25 @@ def get_upgrade_function(current_version: Version, latest_version: Version) -> C # noinspection SqlResolve -def upgrade_1to2(db: FileDB) -> Version: +def upgrade_1to2(conn: Connection) -> Version: # Add "lock" column if not already present - if not db.execute("select 1 from pragma_table_info('Files') where name = 'lock'").fetchone(): - db.execute("alter table Files add column lock boolean") - db.execute("update Files set lock = false") + if not conn.execute("select 1 from pragma_table_info('Files') where name = 'lock'").fetchone(): + conn.execute("alter table Files add column lock boolean") + conn.execute("update Files set lock = false") # Rename "replace" action to "template" - db.execute("update Files set action = 'template' where action = 'replace'") + conn.execute("update Files set action = 'template' where action = 'replace'") # Ensure action_data is always a readable JSON - db.execute("update Files set action_data = '{}' where action_data is null or action_data = ''") + conn.execute("update Files set action_data = '{}' where action_data is null or action_data = ''") # Reset _IdentificationWarnings view - db.execute("drop view if exists _IdentificationWarnings") - db.identification_warnings.create() - - cursor = db.execute("select * from files where action_data != '{}'") + conn.execute("drop view if exists _IdentificationWarnings") + conn.execute( + "CREATE VIEW _IdentificationWarnings AS" + " SELECT uuid,checksum,relative_path,is_binary,size,puid,signature,warning,action,action_data,processed,lock" + ' FROM Files WHERE "Files".warning is not null or "Files".puid is NULL;' + ) + + cursor = conn.execute("select * from files where action_data != '{}'") cursor.row_factory = Row for file in cursor: @@ -66,22 +72,27 @@ def upgrade_1to2(db: FileDB) -> Version: action_data["template"] = action_data.get("replace") # Remove None and empty lists (default values) action_data = {k: v for k, v in action_data.items() if v} - db.execute("update Files set action_data = ? where uuid = ?", [dumps(action_data), file["uuid"]]) + conn.execute("update Files set action_data = ? where uuid = ?", [dumps(action_data), file["uuid"]]) - db.commit() + conn.commit() - return set_db_version(db, Version("2.0.0")) + return set_db_version(conn, Version("2.0.0")) -def upgrade_2to2_0_2(db: FileDB) -> Version: - db.execute("drop view if exists _IdentificationWarnings") - db.identification_warnings.create() - db.commit() - return set_db_version(db, Version("2.0.2")) +# noinspection SqlResolve +def upgrade_2to2_0_2(conn: Connection) -> Version: + conn.execute("drop view if exists _IdentificationWarnings") + conn.execute( + "CREATE VIEW _IdentificationWarnings AS" + " SELECT uuid,checksum,relative_path,is_binary,size,puid,signature,warning,action,action_data,processed,lock" + ' FROM Files WHERE "Files".warning is not null or "Files".puid is NULL;' + ) + conn.commit() + return set_db_version(conn, Version("2.0.2")) # noinspection SqlResolve -def upgrade_2_0_2to3(db: FileDB) -> Version: +def upgrade_2_0_2to3(conn: Connection) -> Version: def convert_action_data(data: dict): new_data: dict[str, Any] = {} @@ -114,27 +125,26 @@ def convert_action_data(data: dict): return new_data # Add "parent" column if not already present - if not db.execute("select 1 from pragma_table_info('Files') where name = 'parent'").fetchone(): - db.execute("alter table Files add column parent text") - db.execute("update Files set parent = null") + if not conn.execute("select 1 from pragma_table_info('Files') where name = 'parent'").fetchone(): + conn.execute("alter table Files add column parent text") + conn.execute("update Files set parent = null") - cursor = db.execute("select * from Files where action_data != '{}'") + cursor = conn.execute("select * from Files where action_data != '{}'") cursor.row_factory = Row for file in cursor: - db.execute( + conn.execute( "update Files set action_data = ? where uuid is ?", [dumps(convert_action_data(loads(file["action_data"]))), file["uuid"]], ) - db.commit() + conn.commit() - return set_db_version(db, Version("3.0.0")) + return set_db_version(conn, Version("3.0.0")) -def upgrade_last(db: FileDB) -> Version: - db.init() - return set_db_version(db, Version(__version__)) +def upgrade_last(conn: Connection) -> Version: + return set_db_version(conn, Version(__version__)) def is_latest(db: FileDB, *, raise_on_difference: bool = False) -> bool: @@ -151,9 +161,11 @@ def is_latest(db: FileDB, *, raise_on_difference: bool = False) -> bool: if not db.is_initialised(check_views=False, check_indices=False): raise DatabaseError("Database is not initialised") - current_version: Version = get_db_version(db) + current_version: Version | None = get_db_version(db) latest_version: Version = Version(__version__) + if not current_version: + raise DatabaseError(f"Database does not contain version information") if current_version > latest_version: raise DatabaseError(f"Database version is greater than latest version: {current_version} > {latest_version}") if current_version < latest_version and raise_on_difference: From ffecb9d18bf6ac2fdc5f94b729c27fdd9afa1d2e Mon Sep 17 00:00:00 2001 From: Matteo Campinoti Date: Tue, 20 Aug 2024 09:25:15 +0200 Subject: [PATCH 06/16] database.upgrade:is_latest - remove unneeded f-string --- 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 266a884..2ed8a7c 100644 --- a/acacore/database/upgrade.py +++ b/acacore/database/upgrade.py @@ -165,7 +165,7 @@ def is_latest(db: FileDB, *, raise_on_difference: bool = False) -> bool: latest_version: Version = Version(__version__) if not current_version: - raise DatabaseError(f"Database does not contain version information") + raise DatabaseError("Database does not contain version information") if current_version > latest_version: raise DatabaseError(f"Database version is greater than latest version: {current_version} > {latest_version}") if current_version < latest_version and raise_on_difference: From 946af910f20ca57ca0ea6e03bdd17ec4d0c1a186 Mon Sep 17 00:00:00 2001 From: Matteo Campinoti Date: Tue, 20 Aug 2024 09:26:22 +0200 Subject: [PATCH 07/16] database.base:View.create_statement - use * for select if view columns match table columns --- acacore/database/base.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/acacore/database/base.py b/acacore/database/base.py index 09fe65b..cc4b19c 100644 --- a/acacore/database/base.py +++ b/acacore/database/base.py @@ -654,10 +654,15 @@ def create_statement(self, exist_ok: bool = True) -> str: elements.append("AS") - select_names = [ - f"{c.name} as {c.alias}" if c.alias else f"{on_table}.{c.name}" - for c in [SelectColumn.from_column(c) for c in self.columns] - ] + if not any(isinstance(c, SelectColumn) for c in self.columns) and [c.name for c in self.columns] == [ + c.name for c in self.on.columns + ]: + select_names = ["*"] + else: + select_names = [ + f"{c.name} as {c.alias}" if c.alias else f"{on_table}.{c.name}" + for c in [SelectColumn.from_column(c) for c in self.columns] + ] elements.append( f"SELECT {','.join(select_names)} " f"FROM {on_table}", From 39cd76a31d1fd2f742d3bd8e041eca260af76796 Mon Sep 17 00:00:00 2001 From: Matteo Campinoti Date: Tue, 20 Aug 2024 09:27:16 +0200 Subject: [PATCH 08/16] database.upgrade:set_db_version - fix incorrect parameter type --- 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 2ed8a7c..4c540b7 100644 --- a/acacore/database/upgrade.py +++ b/acacore/database/upgrade.py @@ -26,7 +26,7 @@ def get_db_version(conn: Connection) -> Version | None: def set_db_version(conn: Connection, version: Version) -> Version: - conn.execute("insert or replace into Metadata (KEY, VALUE) values (?, ?)", ("version", version)) + conn.execute("insert or replace into Metadata (KEY, VALUE) values (?, ?)", ("version", str(version))) conn.commit() return version From d253b8636731be6b6ce9f30750016be87a025873 Mon Sep 17 00:00:00 2001 From: Matteo Campinoti Date: Tue, 20 Aug 2024 09:28:21 +0200 Subject: [PATCH 09/16] database.upgrade - ignore update statements without WHERE --- acacore/database/upgrade.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/acacore/database/upgrade.py b/acacore/database/upgrade.py index 4c540b7..a164bc6 100644 --- a/acacore/database/upgrade.py +++ b/acacore/database/upgrade.py @@ -49,6 +49,7 @@ def upgrade_1to2(conn: Connection) -> Version: # Add "lock" column if not already present if not conn.execute("select 1 from pragma_table_info('Files') where name = 'lock'").fetchone(): conn.execute("alter table Files add column lock boolean") + # noinspection SqlWithoutWhere conn.execute("update Files set lock = false") # Rename "replace" action to "template" conn.execute("update Files set action = 'template' where action = 'replace'") @@ -127,6 +128,7 @@ def convert_action_data(data: dict): # Add "parent" column if not already present if not conn.execute("select 1 from pragma_table_info('Files') where name = 'parent'").fetchone(): conn.execute("alter table Files add column parent text") + # noinspection SqlWithoutWhere conn.execute("update Files set parent = null") cursor = conn.execute("select * from Files where action_data != '{}'") From 88f261258d118daeda6400140defcdf0d58fc828 Mon Sep 17 00:00:00 2001 From: Matteo Campinoti Date: Tue, 20 Aug 2024 09:33:08 +0200 Subject: [PATCH 10/16] database.upgrade:upgrade_3to3_0_2 - add function to upgrade from 3.0.x to 3.0.2 --- acacore/database/upgrade.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/acacore/database/upgrade.py b/acacore/database/upgrade.py index a164bc6..41bc7de 100644 --- a/acacore/database/upgrade.py +++ b/acacore/database/upgrade.py @@ -38,6 +38,8 @@ def get_upgrade_function(current_version: Version, latest_version: Version) -> C return upgrade_2to2_0_2 elif current_version < Version("3.0.0"): return upgrade_2_0_2to3 + elif current_version < Version("3.0.2"): + return upgrade_3to3_0_2 elif current_version < latest_version: return upgrade_last else: @@ -60,8 +62,7 @@ def upgrade_1to2(conn: Connection) -> Version: conn.execute("drop view if exists _IdentificationWarnings") conn.execute( "CREATE VIEW _IdentificationWarnings AS" - " SELECT uuid,checksum,relative_path,is_binary,size,puid,signature,warning,action,action_data,processed,lock" - ' FROM Files WHERE "Files".warning is not null or "Files".puid is NULL;' + ' SELECT * FROM Files WHERE "Files".warning is not null or "Files".puid is NULL;' ) cursor = conn.execute("select * from files where action_data != '{}'") @@ -85,8 +86,7 @@ def upgrade_2to2_0_2(conn: Connection) -> Version: conn.execute("drop view if exists _IdentificationWarnings") conn.execute( "CREATE VIEW _IdentificationWarnings AS" - " SELECT uuid,checksum,relative_path,is_binary,size,puid,signature,warning,action,action_data,processed,lock" - ' FROM Files WHERE "Files".warning is not null or "Files".puid is NULL;' + ' SELECT * FROM Files WHERE "Files".warning is not null or "Files".puid is NULL;' ) conn.commit() return set_db_version(conn, Version("2.0.2")) @@ -145,6 +145,17 @@ def convert_action_data(data: dict): return set_db_version(conn, Version("3.0.0")) +# noinspection SqlResolve +def upgrade_3to3_0_2(conn: Connection) -> Version: + conn.execute("drop view if exists _IdentificationWarnings") + conn.execute( + "CREATE VIEW _IdentificationWarnings AS" + ' SELECT * FROM Files WHERE "Files".warning is not null or "Files".puid is NULL;' + ) + conn.commit() + return set_db_version(conn, Version("3.0.2")) + + def upgrade_last(conn: Connection) -> Version: return set_db_version(conn, Version(__version__)) From 60b02bbcf6e86084d4bf34e1e6241e59f5dad501 Mon Sep 17 00:00:00 2001 From: Matteo Campinoti Date: Tue, 20 Aug 2024 09:33:51 +0200 Subject: [PATCH 11/16] version - patch 3.0.1 > 3.0.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 0552768..131942e 100644 --- a/acacore/__version__.py +++ b/acacore/__version__.py @@ -1 +1 @@ -__version__ = "3.0.1" +__version__ = "3.0.2" diff --git a/pyproject.toml b/pyproject.toml index 619194e..59f4d3e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "acacore" -version = "3.0.1" +version = "3.0.2" description = "" authors = ["Matteo Campinoti "] license = "GPL-3.0" From 11e26c1bd0213aabe2b3b9ae357613338bc9b2ec Mon Sep 17 00:00:00 2001 From: Matteo Campinoti Date: Tue, 20 Aug 2024 09:38:12 +0200 Subject: [PATCH 12/16] database.upgrade:upgrade_3to3_0_2 - fix WHERE for `_IdentificationWarnings` --- 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 41bc7de..de7f462 100644 --- a/acacore/database/upgrade.py +++ b/acacore/database/upgrade.py @@ -150,7 +150,7 @@ def upgrade_3to3_0_2(conn: Connection) -> Version: conn.execute("drop view if exists _IdentificationWarnings") conn.execute( "CREATE VIEW _IdentificationWarnings AS" - ' SELECT * FROM Files WHERE "Files".warning is not null or "Files".puid is NULL;' + ' SELECT * FROM Files WHERE ("Files".warning is not null or "Files".puid is null) and "Files".size != 0' ) conn.commit() return set_db_version(conn, Version("3.0.2")) From 119e59fefd2e42aaa3850dbee75fcf9f443aaf2e Mon Sep 17 00:00:00 2001 From: Matteo Campinoti Date: Tue, 20 Aug 2024 09:39:51 +0200 Subject: [PATCH 13/16] database.upgrade:upgrade_2_0_2to3 - add `_IdentificationWarnings` reset --- acacore/database/upgrade.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/acacore/database/upgrade.py b/acacore/database/upgrade.py index de7f462..2293e6e 100644 --- a/acacore/database/upgrade.py +++ b/acacore/database/upgrade.py @@ -131,6 +131,13 @@ def convert_action_data(data: dict): # noinspection SqlWithoutWhere conn.execute("update Files set parent = null") + # Reset _IdentificationWarnings view + conn.execute("drop view if exists _IdentificationWarnings") + conn.execute( + "CREATE VIEW _IdentificationWarnings AS" + ' SELECT * FROM Files WHERE "Files".warning is not null or "Files".puid is NULL;' + ) + cursor = conn.execute("select * from Files where action_data != '{}'") cursor.row_factory = Row From 35e8dcb856af6c7afbba42b1557ae5be673ba408 Mon Sep 17 00:00:00 2001 From: Matteo Campinoti Date: Tue, 20 Aug 2024 09:40:56 +0200 Subject: [PATCH 14/16] database.upgrade:get_upgrade_function - move below upgrade functions --- acacore/database/upgrade.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/acacore/database/upgrade.py b/acacore/database/upgrade.py index 2293e6e..cd897ba 100644 --- a/acacore/database/upgrade.py +++ b/acacore/database/upgrade.py @@ -31,21 +31,6 @@ def set_db_version(conn: Connection, version: Version) -> Version: return version -def get_upgrade_function(current_version: Version, latest_version: Version) -> Callable[[Connection], Version]: - if current_version < Version("2.0.0"): - return upgrade_1to2 - elif current_version < Version("2.0.2"): - return upgrade_2to2_0_2 - elif current_version < Version("3.0.0"): - return upgrade_2_0_2to3 - elif current_version < Version("3.0.2"): - return upgrade_3to3_0_2 - elif current_version < latest_version: - return upgrade_last - else: - return lambda _: latest_version - - # noinspection SqlResolve def upgrade_1to2(conn: Connection) -> Version: # Add "lock" column if not already present @@ -167,6 +152,21 @@ def upgrade_last(conn: Connection) -> Version: return set_db_version(conn, Version(__version__)) +def get_upgrade_function(current_version: Version, latest_version: Version) -> Callable[[Connection], Version]: + if current_version < Version("2.0.0"): + return upgrade_1to2 + elif current_version < Version("2.0.2"): + return upgrade_2to2_0_2 + elif current_version < Version("3.0.0"): + return upgrade_2_0_2to3 + elif current_version < Version("3.0.2"): + return upgrade_3to3_0_2 + elif current_version < latest_version: + return upgrade_last + else: + return lambda _: latest_version + + def is_latest(db: FileDB, *, raise_on_difference: bool = False) -> bool: """ Check if a database is using the latest version of acacore. From 8334d38b4fb1b5019bf83aeb800486bba6be233a Mon Sep 17 00:00:00 2001 From: Matteo Campinoti Date: Tue, 20 Aug 2024 09:42:00 +0200 Subject: [PATCH 15/16] database.upgrade:upgrade_last - remove unneeded function Can use a lambda with the same behaviour --- acacore/database/upgrade.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/acacore/database/upgrade.py b/acacore/database/upgrade.py index cd897ba..a37f62d 100644 --- a/acacore/database/upgrade.py +++ b/acacore/database/upgrade.py @@ -148,10 +148,6 @@ def upgrade_3to3_0_2(conn: Connection) -> Version: return set_db_version(conn, Version("3.0.2")) -def upgrade_last(conn: Connection) -> Version: - return set_db_version(conn, Version(__version__)) - - def get_upgrade_function(current_version: Version, latest_version: Version) -> Callable[[Connection], Version]: if current_version < Version("2.0.0"): return upgrade_1to2 @@ -162,7 +158,7 @@ def get_upgrade_function(current_version: Version, latest_version: Version) -> C elif current_version < Version("3.0.2"): return upgrade_3to3_0_2 elif current_version < latest_version: - return upgrade_last + return lambda c: set_db_version(c, Version(__version__)) else: return lambda _: latest_version From 4320a15f977265444e1f8058da781ef1cbe59f13 Mon Sep 17 00:00:00 2001 From: Matteo Campinoti Date: Tue, 20 Aug 2024 09:51:41 +0200 Subject: [PATCH 16/16] database.files_db:FileDB.upgrade - run init after upgrade --- acacore/database/files_db.py | 1 + 1 file changed, 1 insertion(+) diff --git a/acacore/database/files_db.py b/acacore/database/files_db.py index f327e3b..e0781c7 100644 --- a/acacore/database/files_db.py +++ b/acacore/database/files_db.py @@ -272,6 +272,7 @@ def upgrade(self): from acacore.database.upgrade import upgrade upgrade(self) + self.init() def is_empty(self) -> bool: """Check if the database contains any files."""