Skip to content
This repository was archived by the owner on Jan 21, 2022. It is now read-only.

Commit 6cb7f37

Browse files
authored
fixes #133
1 parent d828c8c commit 6cb7f37

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

magneticod/magneticod/persistence.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,24 @@ def is_infohash_new(self, info_hash):
111111
def __commit_metadata(self) -> None:
112112
cur = self.__db_conn.cursor()
113113
cur.execute("BEGIN;")
114+
115+
# Ignore insertions into files table if there is no corresponding row
116+
# in the torrents table (which might be the case as we use INSERT OR
117+
# IGNORE into)
118+
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;")
119+
114120
# noinspection PyBroadException
115121
try:
116122
cur.executemany(
117-
"INSERT INTO torrents (info_hash, name, total_size, discovered_on) VALUES (?, ?, ?, ?);",
123+
"INSERT OR IGNORE INTO torrents (info_hash, name, total_size, discovered_on) VALUES (?, ?, ?, ?);",
118124
self.__pending_metadata
119125
)
120126
cur.executemany(
121127
"INSERT INTO files (torrent_id, size, path) "
122128
"VALUES ((SELECT id FROM torrents WHERE info_hash=?), ?, ?);",
123129
self.__pending_files
124130
)
131+
cur.execute("DROP TRIGGER torrent_id_not_null;")
125132
cur.execute("COMMIT;")
126133
logging.info("%d metadata (%d files) are committed to the database.",
127134
len(self.__pending_metadata), len(self.__pending_files))

0 commit comments

Comments
 (0)