CREATE TABLE ccns (
    ccn_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),
    pubkey TEXT NOT NULL UNIQUE,
    name TEXT NOT NULL,
    created_at INTEGER NOT NULL DEFAULT (unixepoch()),
    is_active INTEGER NOT NULL DEFAULT 1
);

ALTER TABLE events
ADD COLUMN ccn_pubkey TEXT;

CREATE INDEX idx_events_ccn_pubkey ON events(ccn_pubkey);

ALTER TABLE event_chunks RENAME COLUMN chunk_data TO content;
ALTER TABLE event_chunks ADD COLUMN ccn_pubkey TEXT;
ALTER TABLE event_chunks DROP COLUMN conversation_key;
CREATE INDEX idx_event_chunks_ccn_pubkey ON event_chunks(ccn_pubkey);

UPDATE ccns SET is_active = 0;
UPDATE ccns SET is_active = 1
WHERE pubkey = (SELECT pubkey FROM ccns LIMIT 1);