From: Christian Heller <c.heller@plomlompom.de>
Date: Sun, 2 Jun 2024 22:50:35 +0000 (+0200)
Subject: Fix bug of relationships writing for todo_children writing in wrong column.
X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%22https:/validator.w3.org/ledger?a=commitdiff_plain;h=0630d9cfdc47e306b96ad05b4077ee96eec71226;p=plomtask

Fix bug of relationships writing for todo_children writing in wrong column.
---

diff --git a/plomtask/.db.py.swp b/plomtask/.db.py.swp
new file mode 100644
index 0000000..b62d92e
Binary files /dev/null and b/plomtask/.db.py.swp differ
diff --git a/plomtask/.http.py.swp b/plomtask/.http.py.swp
new file mode 100644
index 0000000..003e5d3
Binary files /dev/null and b/plomtask/.http.py.swp differ
diff --git a/plomtask/.versioned_attributes.py.swp b/plomtask/.versioned_attributes.py.swp
new file mode 100644
index 0000000..1c5a544
Binary files /dev/null and b/plomtask/.versioned_attributes.py.swp differ
diff --git a/plomtask/__pycache__/__init__.cpython-311.pyc b/plomtask/__pycache__/__init__.cpython-311.pyc
new file mode 100644
index 0000000..67ebc21
Binary files /dev/null and b/plomtask/__pycache__/__init__.cpython-311.pyc differ
diff --git a/plomtask/__pycache__/conditions.cpython-311.pyc b/plomtask/__pycache__/conditions.cpython-311.pyc
new file mode 100644
index 0000000..23e3c3b
Binary files /dev/null and b/plomtask/__pycache__/conditions.cpython-311.pyc differ
diff --git a/plomtask/__pycache__/dating.cpython-311.pyc b/plomtask/__pycache__/dating.cpython-311.pyc
new file mode 100644
index 0000000..dc6f1cd
Binary files /dev/null and b/plomtask/__pycache__/dating.cpython-311.pyc differ
diff --git a/plomtask/__pycache__/days.cpython-311.pyc b/plomtask/__pycache__/days.cpython-311.pyc
new file mode 100644
index 0000000..393a8bb
Binary files /dev/null and b/plomtask/__pycache__/days.cpython-311.pyc differ
diff --git a/plomtask/__pycache__/db.cpython-311.pyc b/plomtask/__pycache__/db.cpython-311.pyc
new file mode 100644
index 0000000..0dc717f
Binary files /dev/null and b/plomtask/__pycache__/db.cpython-311.pyc differ
diff --git a/plomtask/__pycache__/exceptions.cpython-311.pyc b/plomtask/__pycache__/exceptions.cpython-311.pyc
new file mode 100644
index 0000000..85b2cdc
Binary files /dev/null and b/plomtask/__pycache__/exceptions.cpython-311.pyc differ
diff --git a/plomtask/__pycache__/http.cpython-311.pyc b/plomtask/__pycache__/http.cpython-311.pyc
new file mode 100644
index 0000000..edbb8ea
Binary files /dev/null and b/plomtask/__pycache__/http.cpython-311.pyc differ
diff --git a/plomtask/__pycache__/misc.cpython-311.pyc b/plomtask/__pycache__/misc.cpython-311.pyc
new file mode 100644
index 0000000..30b4ccf
Binary files /dev/null and b/plomtask/__pycache__/misc.cpython-311.pyc differ
diff --git a/plomtask/__pycache__/processes.cpython-311.pyc b/plomtask/__pycache__/processes.cpython-311.pyc
new file mode 100644
index 0000000..bebfbe3
Binary files /dev/null and b/plomtask/__pycache__/processes.cpython-311.pyc differ
diff --git a/plomtask/__pycache__/task.cpython-311.pyc b/plomtask/__pycache__/task.cpython-311.pyc
new file mode 100644
index 0000000..173d529
Binary files /dev/null and b/plomtask/__pycache__/task.cpython-311.pyc differ
diff --git a/plomtask/__pycache__/todos.cpython-311.pyc b/plomtask/__pycache__/todos.cpython-311.pyc
new file mode 100644
index 0000000..9a8f08c
Binary files /dev/null and b/plomtask/__pycache__/todos.cpython-311.pyc differ
diff --git a/plomtask/__pycache__/versioned_attributes.cpython-311.pyc b/plomtask/__pycache__/versioned_attributes.cpython-311.pyc
new file mode 100644
index 0000000..7c20eb2
Binary files /dev/null and b/plomtask/__pycache__/versioned_attributes.cpython-311.pyc differ
diff --git a/plomtask/db.py b/plomtask/db.py
index b5461a5..90ec833 100644
--- a/plomtask/db.py
+++ b/plomtask/db.py
@@ -172,11 +172,17 @@ class DatabaseConnection:
         self.conn.close()
 
     def rewrite_relations(self, table_name: str, key: str, target: int | str,
-                          rows: list[list[Any]]) -> None:
-        """Rewrite relations in table_name to target, with rows values."""
+                          rows: list[list[Any]], key_index: int = 0) -> None:
+        # pylint: disable=too-many-arguments
+        """Rewrite relations in table_name to target, with rows values.
+
+        Note that single rows are expected without the column and value
+        identified by key and target, which are inserted inside the function
+        at key_index.
+        """
         self.delete_where(table_name, key, target)
         for row in rows:
-            values = tuple([target] + row)
+            values = tuple(row[:key_index] + [target] + row[key_index:])
             q_marks = self.__class__.q_marks_from_values(values)
             self.exec(f'INSERT INTO {table_name} VALUES {q_marks}', values)
 
@@ -229,7 +235,7 @@ class BaseModel(Generic[BaseModelId]):
     table_name = ''
     to_save: list[str] = []
     to_save_versioned: list[str] = []
-    to_save_relations: list[tuple[str, str, str]] = []
+    to_save_relations: list[tuple[str, str, str, int]] = []
     id_: None | BaseModelId
     cache_: dict[BaseModelId, Self]
     to_search: list[str] = []
@@ -420,11 +426,11 @@ class BaseModel(Generic[BaseModelId]):
         self.cache()
         for attr_name in self.to_save_versioned:
             getattr(self, attr_name).save(db_conn)
-        for table, column, attr_name in self.to_save_relations:
+        for table, column, attr_name, key_index in self.to_save_relations:
             assert isinstance(self.id_, (int, str))
             db_conn.rewrite_relations(table, column, self.id_,
                                       [[i.id_] for i
-                                       in getattr(self, attr_name)])
+                                       in getattr(self, attr_name)], key_index)
 
     def remove(self, db_conn: DatabaseConnection) -> None:
         """Remove from DB and cache, including dependencies."""
@@ -432,7 +438,7 @@ class BaseModel(Generic[BaseModelId]):
             raise HandledException('cannot remove unsaved item')
         for attr_name in self.to_save_versioned:
             getattr(self, attr_name).remove(db_conn)
-        for table, column, attr_name in self.to_save_relations:
+        for table, column, attr_name, _ in self.to_save_relations:
             db_conn.delete_where(table, column, self.id_)
         self.uncache()
         db_conn.delete_where(self.table_name, 'id', self.id_)
diff --git a/plomtask/processes.py b/plomtask/processes.py
index 027e975..bfadc2b 100644
--- a/plomtask/processes.py
+++ b/plomtask/processes.py
@@ -26,10 +26,10 @@ class Process(BaseModel[int], ConditionsRelations):
     table_name = 'processes'
     to_save = ['calendarize']
     to_save_versioned = ['title', 'description', 'effort']
-    to_save_relations = [('process_conditions', 'process', 'conditions'),
-                         ('process_blockers', 'process', 'blockers'),
-                         ('process_enables', 'process', 'enables'),
-                         ('process_disables', 'process', 'disables')]
+    to_save_relations = [('process_conditions', 'process', 'conditions', 0),
+                         ('process_blockers', 'process', 'blockers', 0),
+                         ('process_enables', 'process', 'enables', 0),
+                         ('process_disables', 'process', 'disables', 0)]
     to_search = ['title.newest', 'description.newest']
 
     def __init__(self, id_: int | None, calendarize: bool = False) -> None:
diff --git a/plomtask/todos.py b/plomtask/todos.py
index 9fac63b..775ef48 100644
--- a/plomtask/todos.py
+++ b/plomtask/todos.py
@@ -26,12 +26,12 @@ class Todo(BaseModel[int], ConditionsRelations):
     table_name = 'todos'
     to_save = ['process_id', 'is_done', 'date', 'comment', 'effort',
                'calendarize']
-    to_save_relations = [('todo_conditions', 'todo', 'conditions'),
-                         ('todo_blockers', 'todo', 'blockers'),
-                         ('todo_enables', 'todo', 'enables'),
-                         ('todo_disables', 'todo', 'disables'),
-                         ('todo_children', 'parent', 'children'),
-                         ('todo_children', 'child', 'parents')]
+    to_save_relations = [('todo_conditions', 'todo', 'conditions', 0),
+                         ('todo_blockers', 'todo', 'blockers', 0),
+                         ('todo_enables', 'todo', 'enables', 0),
+                         ('todo_disables', 'todo', 'disables', 0),
+                         ('todo_children', 'parent', 'children', 0),
+                         ('todo_children', 'child', 'parents', 1)]
     to_search = ['comment']
 
     # pylint: disable=too-many-arguments