From 6cb7f3783e5bc42526f3885eed52434d18b0d146 Mon Sep 17 00:00:00 2001 From: "Bora M. Alper" Date: Fri, 17 Aug 2018 16:02:44 +0300 Subject: [PATCH] fixes #133 --- magneticod/magneticod/persistence.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/magneticod/magneticod/persistence.py b/magneticod/magneticod/persistence.py index a279d76..2aff2c6 100644 --- a/magneticod/magneticod/persistence.py +++ b/magneticod/magneticod/persistence.py @@ -111,10 +111,16 @@ class Database: def __commit_metadata(self) -> None: cur = self.__db_conn.cursor() cur.execute("BEGIN;") + + # Ignore insertions into files table if there is no corresponding row + # in the torrents table (which might be the case as we use INSERT OR + # IGNORE into) + cur.execute("CREATE TEMPORARY TRIGGER torrent_id_not_null BEFORE INSERT ON main.files WHEN NEW.'torrent_id' IS NULL BEGIN SELECT RAISE(IGNORE); END;") + # noinspection PyBroadException try: cur.executemany( - "INSERT INTO torrents (info_hash, name, total_size, discovered_on) VALUES (?, ?, ?, ?);", + "INSERT OR IGNORE INTO torrents (info_hash, name, total_size, discovered_on) VALUES (?, ?, ?, ?);", self.__pending_metadata ) cur.executemany( @@ -122,6 +128,7 @@ class Database: "VALUES ((SELECT id FROM torrents WHERE info_hash=?), ?, ?);", self.__pending_files ) + cur.execute("DROP TRIGGER torrent_id_not_null;") cur.execute("COMMIT;") logging.info("%d metadata (%d files) are committed to the database.", len(self.__pending_metadata), len(self.__pending_files))