From d9051ee6c0f9a713156e5e6a4874f09c67a2d98e Mon Sep 17 00:00:00 2001
From: Remy Moll <me@moll.re>
Date: Mon, 24 Apr 2023 20:19:44 +0200
Subject: [PATCH] actually wal is reuqired?

---
 bot/commands/journal.py     | 52 ++++++++++++++++++++-----------------
 bot/commands/list/models.py |  2 +-
 bot/models.py               |  2 +-
 3 files changed, 30 insertions(+), 26 deletions(-)

diff --git a/bot/commands/journal.py b/bot/commands/journal.py
index 164c39b..4adc566 100644
--- a/bot/commands/journal.py
+++ b/bot/commands/journal.py
@@ -70,38 +70,42 @@ class JournalHandler(BaseHandler):
         date = update.message.text
         try:
             date = datetime.datetime.strptime(date, "%d%m%Y").date()
-            with self.models.db:
-                self.current_model, new = self.models.JournalEntry.get_or_create(
-                    date = date
-                )
-            if not new:
-                await update.message.reply_text("An entry already exists for this date")
-                return ConversationHandler.END
         except ValueError:
             await update.message.reply_text("Please enter the date in the format DDMMYYYY")
             return DATE_ENTRY
-
-        await update.message.reply_text("Please enter the content for the entry")
-        return CONTENT
+        self.logger.info(f"Date: {date}")
+        
+        with self.models.db:
+            self.current_model, new = self.models.JournalEntry.get_or_create(
+                date = date
+            )
+        if not new:
+            await update.message.reply_text("An entry already exists for this date")
+            return ConversationHandler.END
+        else:
+            await update.message.reply_text("Please enter the content for the entry")
+            return CONTENT
 
 
     async def content_save(self, update, context):
-        self.current_model.author_id = update.message.from_user.id
+        with self.models.db:
+            self.current_model.author_id = update.message.from_user.id
 
-        if update.message.text:
-            self.current_model.text = update.message.text
-        else:
-            if update.message.photo:
-                file = await update.message.effective_attachment[-1].get_file()
+            if update.message.text:
+                self.current_model.text = update.message.text
             else:
-                file = await update.message.effective_attachment.get_file()
-            
-            file_bytes = await file.download_as_bytearray()
-            file_path = file.file_path
-            self.current_model.save_media(file_bytes, file_path)
+                if update.message.photo:
+                    file = await update.message.effective_attachment[-1].get_file()
+                else:
+                    file = await update.message.effective_attachment.get_file()
+                
+                file_bytes = await file.download_as_bytearray()
+                file_path = file.file_path
+                self.current_model.save_media(file_bytes, file_path)
+
+                self.current_model.text = update.message.caption
+            
+            self.current_model.save()
 
-            self.current_model.text = update.message.caption
-        
-        self.current_model.save()
         await update.message.reply_text(f"Saved entry for {self.current_model.date.strftime('%A, %-d. %B %Y')}")
         return ConversationHandler.END
diff --git a/bot/commands/list/models.py b/bot/commands/list/models.py
index f672f47..3f23004 100644
--- a/bot/commands/list/models.py
+++ b/bot/commands/list/models.py
@@ -25,6 +25,6 @@ class ListModel(BaseModel):
 
 
 def set_db(db_path):
-    db.initialize(SqliteDatabase(db_path))
+    db.initialize(SqliteDatabase(db_path, pragmas={'journal_mode': 'wal'}))
     db.connect()
     db.create_tables([ListModel], safe=True)
diff --git a/bot/models.py b/bot/models.py
index b62534c..7408f67 100644
--- a/bot/models.py
+++ b/bot/models.py
@@ -54,6 +54,6 @@ class JournalEntry(BaseModel):
 
 
 def set_db(db_path):
-    db.initialize(SqliteDatabase(db_path))
+    db.initialize(SqliteDatabase(db_path, pragmas={'journal_mode': 'wal'}))
     db.connect()
     db.create_tables([JournalEntry], safe=True)