fix error with sqlite ≥ 3.34

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]: 86f477edaa
This commit is contained in:
Michele Guerini Rocco 2024-07-25 13:46:32 +02:00
parent 1c826851b1
commit 06d864d49f
Signed by: rnhmjoj
GPG Key ID: BFBAF4C975F76450

View File

@ -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)));