This commit is contained in:
Bora M. Alper 2018-08-17 16:02:44 +03:00 committed by GitHub
parent d828c8c41e
commit 6cb7f3783e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -111,10 +111,16 @@ class Database:
def __commit_metadata(self) -> None: def __commit_metadata(self) -> None:
cur = self.__db_conn.cursor() cur = self.__db_conn.cursor()
cur.execute("BEGIN;") 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 # noinspection PyBroadException
try: try:
cur.executemany( 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 self.__pending_metadata
) )
cur.executemany( cur.executemany(
@ -122,6 +128,7 @@ class Database:
"VALUES ((SELECT id FROM torrents WHERE info_hash=?), ?, ?);", "VALUES ((SELECT id FROM torrents WHERE info_hash=?), ?, ?);",
self.__pending_files self.__pending_files
) )
cur.execute("DROP TRIGGER torrent_id_not_null;")
cur.execute("COMMIT;") cur.execute("COMMIT;")
logging.info("%d metadata (%d files) are committed to the database.", logging.info("%d metadata (%d files) are committed to the database.",
len(self.__pending_metadata), len(self.__pending_files)) len(self.__pending_metadata), len(self.__pending_files))