From 06d864d49fe15355dd605c04e875e47db7948b2b Mon Sep 17 00:00:00 2001 From: rnhmjoj Date: Thu, 25 Jul 2024 13:46:32 +0200 Subject: [PATCH] =?UTF-8?q?fix=20error=20with=20sqlite=20=E2=89=A5=203.34?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since sqlite 3.34[1] deleting a record in a fts5 table that doesn't exist became an error. The resuls it that adding a torrent "corrupts" the database and magnetico fails with this error: Could not add new torrent to the database. tx.Exec (INSERT OR REPLACE INTO torrents) database disk image is malformed The problem can be simply avoided by setting modified_on = discovered_on when inserting the torrent, instead of updating it afterwards with a trigger. [1]: https://github.com/sqlite/sqlite/commit/86f477edaa17767b39c7bae5b67cac8580f7a8c1 --- pkg/persistence/sqlite3.go | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/pkg/persistence/sqlite3.go b/pkg/persistence/sqlite3.go index ff32a6d..ba8bbd1 100644 --- a/pkg/persistence/sqlite3.go +++ b/pkg/persistence/sqlite3.go @@ -161,9 +161,10 @@ func (db *sqlite3Database) AddNewTorrent(infoHash []byte, name string, files []F info_hash, name, total_size, - discovered_on - ) VALUES (?, ?, ?, ?); - `, infoHash, name, totalSize, time.Now().Unix()) + discovered_on, + modified_on + ) VALUES (?, ?, ?, ?, ?); + `, infoHash, name, totalSize, time.Now().Unix(), time.Now().Unix()) if err != nil { return errors.Wrap(err, "tx.Exec (INSERT OR REPLACE INTO torrents)") } @@ -651,16 +652,6 @@ func (db *sqlite3Database) setupDatabase() error { DEFAULT 32503680000 ; - -- If 'modified_on' is not explicitly supplied, then it shall be set, by default, to - -- 'discovered_on' right after the row is inserted to 'torrents'. - -- - -- {WHEN expr} does NOT work for some reason (trigger doesn't get triggered), so we use - -- AND NEW."modified_on" = 32503680000 - -- instead in the WHERE clause. - CREATE TRIGGER "torrents_modified_on_default_t" AFTER INSERT ON "torrents" BEGIN - UPDATE "torrents" SET "modified_on" = NEW."discovered_on" WHERE "id" = NEW."id" AND NEW."modified_on" = 32503680000; - END; - -- Set 'modified_on' value of all rows to 'discovered_on' or 'updated_on', whichever is -- greater; beware that 'updated_on' can be NULL too. UPDATE torrents SET modified_on = (SELECT MAX(discovered_on, IFNULL(updated_on, 0)));