create table if not exists mentions (
  entity_id integer not null,
  entity_type integer not null,
  url text not null,
  input_text text not null,
  byte_range_start integer not null,
  byte_range_end integer not null
);

insert into mentions (
        entity_id, url, input_text, byte_range_start, byte_range_end, entity_type)
  select entry_id, url, input_text, byte_range_start, byte_range_end, 'journal_entry'
  from journal_mentions;

drop table journal_mentions;

-- entity types:
-- 0: journal_entry
-- 1: contact.text_body
-- 2: contact.lives_with
create trigger if not exists cascade_delete_journal_mentions
  after delete on journal_entries for each row begin
    delete from mentions where entity_type = 0 and entity_id = OLD.id;
  end;

create trigger if not exists cascade_delete_contact_text_body_mentions
  after delete on contacts for each row begin
    delete from mentions where entity_type = 1 and entity_id = OLD.id;
    delete from mentions where entity_type = 2 and entity_id = OLD.id;
  end;
