Skip to content

Commit

Permalink
fix: update bookmark on sqlite database (go-shiori#367)
Browse files Browse the repository at this point in the history
It used an update and insert at the same time, since the virtual table
cannot have constrants nor upserts we need to try an update the
bookmark content to check for it's existence, inserting it only if the
update fails (don't affect any rows).

This will need to be improved in the future, since not all bookmark
updates require modification of the content table (in my case, I was
testing only updating tags).
  • Loading branch information
fmartingr authored Feb 13, 2022
1 parent fb0bf38 commit 0bd297d
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions internal/database/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ func OpenSQLiteDatabase(databasePath string) (sqliteDB *SQLiteDatabase, err erro
CONSTRAINT bookmark_id_FK FOREIGN KEY(bookmark_id) REFERENCES bookmark(id),
CONSTRAINT tag_id_FK FOREIGN KEY(tag_id) REFERENCES tag(id))`)

tx.MustExec(`CREATE VIRTUAL TABLE IF NOT EXISTS bookmark_content USING fts5(title, content, html, docid)`)
tx.MustExec(`CREATE VIRTUAL TABLE IF NOT EXISTS bookmark_content
USING fts5(title, content, html, docid)`)

// Alter table if needed
if _, err := tx.Exec(`ALTER TABLE account ADD COLUMN owner INTEGER NOT NULL DEFAULT 0`); err != nil {
Expand Down Expand Up @@ -119,9 +120,9 @@ func (db *SQLiteDatabase) SaveBookmarks(bookmarks ...model.Bookmark) (result []m
url = ?, title = ?, excerpt = ?, author = ?,
public = ?, modified = ?`)

// stmtInsertBookContent, _ := tx.Preparex(`INSERT OR IGNORE INTO bookmark_content
// (docid, title, content, html)
// VALUES (?, ?, ?, ?)`)
stmtInsertBookContent, _ := tx.Preparex(`INSERT OR REPLACE INTO bookmark_content
(docid, title, content, html)
VALUES (?, ?, ?, ?)`)

stmtUpdateBookContent, _ := tx.Preparex(`UPDATE bookmark_content SET
title = ?, content = ?, html = ?
Expand Down Expand Up @@ -164,8 +165,13 @@ func (db *SQLiteDatabase) SaveBookmarks(bookmarks ...model.Bookmark) (result []m
book.URL, book.Title, book.Excerpt, book.Author, book.Public, book.Modified,
book.URL, book.Title, book.Excerpt, book.Author, book.Public, book.Modified)

stmtUpdateBookContent.MustExec(book.Title, book.Content, book.HTML, book.ID)
//stmtInsertBookContent.MustExec(book.ID, book.Title, book.Content, book.HTML)
// Try to update it first to check for existence, we can't do an UPSERT here because
// bookmant_content is a virtual table
res := stmtUpdateBookContent.MustExec(book.Title, book.Content, book.HTML, book.ID)
rows, _ := res.RowsAffected()
if rows == 0 {
stmtInsertBookContent.MustExec(book.ID, book.Title, book.Content, book.HTML)
}

// Save book tags
newTags := []model.Tag{}
Expand Down

0 comments on commit 0bd297d

Please sign in to comment.