BEGIN TRANSACTION;

ALTER TABLE logs RENAME TO logs_0_16_8;

-- The difference with the previous table are:
--
-- 1. account_jid is now account_id and is the second column
--
-- 2. additinal_data has been renamed to additional_data
--    Maybe I had run some unstable verison whcih created the column with
--    a misspelled name?
--
-- 3. The default value for additional_data has changed from '()' to '{}'
--
CREATE TABLE logs(
                    log_line_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
                    account_id INTEGER,
                    jid_id INTEGER,
                    contact_name TEXT,
                    time INTEGER,
                    kind INTEGER,
                    show INTEGER,
                    message TEXT,
                    subject TEXT,
                    additional_data TEXT DEFAULT '{}',
                    stanza_id TEXT,
                    mam_id TEXT,
                    encryption TEXT,
                    encryption_state TEXT,
                    marker INTEGER
            );


INSERT INTO logs(log_line_id, account_id, jid_id, contact_name, time, kind, show, message, subject, additional_data, stanza_id, mam_id, encryption, encryption_state, marker)
SELECT log_line_id, account_jid, jid_id, contact_name, time, kind, show, message, subject, additinal_data, stanza_id, mam_id, encryption, encryption_state, marker
FROM logs_0_16_8;

-- As a workaround, set a fixed value for the account_id column, as this info
-- is not available in the database.
UPDATE logs SET account_id = 2 WHERE account_id IS NULL;

-- Fix the new default value for additional_data
UPDATE logs SET additional_data = '{}' WHERE additional_data = '()';

DROP TABLE logs_0_16_8;

CREATE INDEX idx_logs_jid_id_time ON logs (jid_id, time DESC);

COMMIT;

VACUUM;
