From 122ec1c9b283776b8f1fea5559d4317bdba5b977 Mon Sep 17 00:00:00 2001
From: Michaƫl Ball <michael.ball@gmail.com>
Date: Mon, 3 Aug 2015 06:52:00 +0100
Subject: Initially use memory based db, then dump on exit

---
 db/db_manager.py | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

(limited to 'db')

diff --git a/db/db_manager.py b/db/db_manager.py
index eb813ec..ca47ae4 100644
--- a/db/db_manager.py
+++ b/db/db_manager.py
@@ -11,25 +11,34 @@ class DbManager:
 
         def __init__(self):
             new_db = False
+            cache_size_kb = 9766
 
             if not os.path.isfile(self.config["DEFAULT"]["database"]):
                 new_db = True
 
-            self.conn = apsw.Connection(self.config["DEFAULT"]["database"],
-                                        apsw.SQLITE_OPEN_READWRITE |
-                                        apsw.SQLITE_OPEN_CREATE)
-
             if new_db:
+                self.conn = apsw.Connection(":memory:")
                 self.create_tables()
             else:
+                self.conn = apsw.Connection(self.config["DEFAULT"]["database"])
                 library_info = os.stat(self.config["DEFAULT"]["database"])
                 cache_size_kb = round((library_info.st_size * 1.2) / 1024)
 
                 cursor = self.conn.cursor()
                 # Setting pragma with ? placeholder errors out
-                cursor.execute("pragma cache_size=%s" % cache_size_kb)
+                cursor.execute("pragma cache_size=-%s" % cache_size_kb)
                 cursor.close()
 
+        def __del__(self):
+            if not os.path.isfile(self.config["DEFAULT"]["database"]):
+                tempconn = apsw.Connection(self.config["DEFAULT"]["database"],
+                                           apsw.SQLITE_OPEN_READWRITE |
+                                           apsw.SQLITE_OPEN_CREATE)
+
+                with tempconn.backup("main", 
+                                     self.conn, "main") as backup:
+                    backup.step()
+
         def __str__(self):
             return repr(self)
 
-- 
cgit v1.2.3