Skip to content

Commit

Permalink
database.upgrade:upgrade_2_0_2to3 - add function to upgrade database …
Browse files Browse the repository at this point in the history
…to version 3
  • Loading branch information
MatteoCampinoti94 committed Aug 13, 2024
1 parent 3100382 commit a02ac2d
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion acacore/database/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def get_upgrade_function(current_version: Version, latest_version: Version) -> C
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 < latest_version:
return upgrade_last
else:
Expand All @@ -42,6 +44,7 @@ def get_upgrade_function(current_version: Version, latest_version: Version) -> C

# noinspection SqlResolve
def upgrade_1to2(db: FileDB) -> 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")
Expand All @@ -55,7 +58,7 @@ def upgrade_1to2(db: FileDB) -> Version:
db.identification_warnings.create()

cursor = db.execute("select * from files where action_data != '{}'")
cursor.rowfactory = Row
cursor.row_factory = Row

for file in cursor:
action_data: dict[str, Any] = loads(file["action_data"])
Expand All @@ -77,6 +80,58 @@ def upgrade_2to2_0_2(db: FileDB) -> Version:
return set_db_version(db, Version("2.0.2"))


# noinspection SqlResolve
def upgrade_2_0_2to3(db: FileDB) -> Version:
def convert_action_data(data: dict):
new_data: dict[str, Any] = {}

if rename := data.get("rename"):
new_data["rename"] = rename

if reidentify := data.get("reidentify"):
new_data["reidentify"] = {"reason": reidentify["reason"]}
if on_fail := reidentify.get("onfail"):
new_data["reidentify"]["on_fail"] = on_fail

if convert := data.get("convert"):
new_data["convert"] = {"tool": convert[0]["converter"], "outputs": convert[0]["outputs"]}

if extract := data.get("extract"):
new_data["extract"] = {"tool": extract["tool"]}
if extension := extract.get("extension"):
new_data["extract"]["extension"] = extension

if manual := data.get("manual"):
new_data["manual"] = manual

if (ignore := data.get("ignore")) and ignore.get("reason"):
new_data["ignore"] = {"template": "not-preservable", "reason": ignore.get("reason", "")}
elif template := data.get("template"):
new_data["ignore"] = {"template": template["template"]}
if template_text := template.get("template_text"):
new_data["ignore"]["reason"] = template_text

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")

cursor = db.execute("select * from Files where action_data != '{}'")
cursor.row_factory = Row

for file in cursor:
db.execute(
"update Files set action_data = ? where uuid is ?",
[dumps(convert_action_data(loads(file["action_data"]))), file["uuid"]],
)

db.commit()

return set_db_version(db, Version("3.0.0"))


def upgrade_last(db: FileDB) -> Version:
db.init()
return set_db_version(db, Version(__version__))
Expand Down Expand Up @@ -126,4 +181,6 @@ 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)

0 comments on commit a02ac2d

Please sign in to comment.