DROP TRIGGER IF EXISTS on_delete_article_trigger;

ALTER TABLE enclosures RENAME TO _enclosures_old;

CREATE TABLE enclosures (
    article_id TEXT NOT NULL REFERENCES articles(article_id),
    url TEXT NOT NULL,
    mime_type TEXT default null,
    title TEXT default null,
    summary TEXT default null,
    thumbnail_url TEXT default null,
    filesize INTEGER default null,
    width INTEGER default null,
    height INTEGER default null,
    duration INTEGER default null,
    framerate DOUBLE default null,
    position DOUBLE default null,
    alternative TEXT default null,
    is_default BOOLEAN NOT NULL default 0,
    PRIMARY KEY (article_id, url)
);

INSERT INTO enclosures (article_id, url, mime_type, title, position)
  SELECT article_id, url, mime_type, title, position
  FROM _enclosures_old;

DROP TABLE _enclosures_old;

CREATE TRIGGER on_delete_article_trigger
    BEFORE DELETE ON articles
    BEGIN
        DELETE FROM taggings WHERE taggings.article_id=OLD.article_id;
        DELETE FROM enclosures WHERE enclosures.article_id=OLD.article_id;
        DELETE FROM thumbnails WHERE thumbnails.article_id=OLD.article_id;
        DELETE FROM images WHERE images.article_id=OLD.article_id;
    END;
