DROP TRIGGER IF EXISTS on_delete_feed_trigger;

ALTER TABLE fav_icons RENAME TO _fav_icons_old;

CREATE TABLE fav_icons (
	feed_id TEXT NOT NULL REFERENCES feeds(feed_id),
	date TEXT NOT NULL,
	format TEXT,
	etag TEXT,
	source_url TEXT,
	data BLOB,
	PRIMARY KEY (feed_id)
);

INSERT INTO fav_icons (feed_id, date, format, etag, source_url, data)
  SELECT feed_id, date, format, etag, lowres_source_url, lowres FROM _fav_icons_old;

DROP TABLE _fav_icons_old;

CREATE TRIGGER on_delete_feed_trigger
	BEFORE DELETE ON feeds
	BEGIN
		DELETE FROM feed_mapping WHERE feed_mapping.feed_id=OLD.feed_id;
		DELETE FROM articles WHERE articles.feed_id=OLD.feed_id AND articles.marked=1;
		DELETE FROM fav_icons WHERE fav_icons.feed_id=OLD.feed_id;
	END;
