DROP TRIGGER post_bookmarked;

ALTER TABLE bookmark ADD COLUMN feedHomepageLink TEXT DEFAULT '' NOT NULL;

UPDATE bookmark
SET feedHomepageLink = COALESCE(
  (SELECT COALESCE(homepageLink, bookmark.feedIcon) FROM feed WHERE feed.id = bookmark.sourceId),
  ''
)
WHERE feedHomepageLink = '';

UPDATE bookmark SET feedHomepageLink = bookmark.feedIcon WHERE feedHomepageLink = '';

CREATE TRIGGER IF NOT EXISTS
post_bookmarked
AFTER UPDATE OF bookmarked ON post WHEN new.bookmarked == 1
BEGIN
  INSERT OR REPLACE INTO bookmark(id, sourceId, title, description, imageUrl, date, link, commentsLink, bookmarked, read, feedName, feedIcon, feedHomepageLink)
  SELECT new.id, new.sourceId, new.title, new.description, new.imageUrl, new.date, new.link, new.commentsLink, new.bookmarked, new.read, feed.name, feed.icon, feed.homepageLink
  FROM feed WHERE feed.id == new.sourceId;
END;
