BEGIN;
CREATE OR REPLACE FUNCTION openreplay_version()
    RETURNS text AS
$$
SELECT 'v1.8.2'
$$ LANGUAGE sql IMMUTABLE;

ALTER TABLE IF EXISTS public.tenants
    ADD COLUMN IF NOT EXISTS last_telemetry bigint NOT NULL DEFAULT CAST(EXTRACT(epoch FROM date_trunc('day', now())) * 1000 AS BIGINT),
    DROP COLUMN IF EXISTS version_number;

CREATE TABLE IF NOT EXISTS sessions_notes
(
    note_id    integer generated BY DEFAULT AS IDENTITY PRIMARY KEY,
    message    text                        NOT NULL,
    created_at timestamp without time zone NOT NULL default (now() at time zone 'utc'),
    user_id    integer                     NULL REFERENCES users (user_id) ON DELETE SET NULL,
    deleted_at timestamp without time zone NULL     DEFAULT NULL,
    tag        text                        NULL,
    session_id bigint                      NOT NULL REFERENCES sessions (session_id) ON DELETE CASCADE,
    project_id integer                     NOT NULL REFERENCES projects (project_id) ON DELETE CASCADE,
    timestamp  integer                     NOT NULL DEFAULT -1,
    is_public  boolean                     NOT NULL DEFAULT FALSE
);

CREATE TABLE IF NOT EXISTS errors_tags
(
    key        text                        NOT NULL,
    value      text                        NOT NULL,
    created_at timestamp without time zone NOT NULL default (now() at time zone 'utc'),
    error_id   text                        NOT NULL REFERENCES errors (error_id) ON DELETE CASCADE,
    session_id bigint                      NOT NULL,
    message_id bigint                      NOT NULL,
    FOREIGN KEY (session_id, message_id) REFERENCES events.errors (session_id, message_id) ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS errors_tags_error_id_idx ON errors_tags (error_id);
CREATE INDEX IF NOT EXISTS errors_tags_session_id_idx ON errors_tags (session_id);
CREATE INDEX IF NOT EXISTS errors_tags_message_id_idx ON errors_tags (message_id);

UPDATE metrics
SET default_config=default_config || '{"col":4}'
WHERE metric_type = 'funnel';

UPDATE dashboard_widgets
SET config=config || '{"col":4}'
WHERE metric_id IN (SELECT metric_id FROM metrics WHERE metric_type = 'funnel');

COMMIT;