\set previous_version 'v1.16.0-ee'
\set next_version 'v1.17.0-ee'
SELECT openreplay_version()                       AS current_version,
       openreplay_version() = :'previous_version' AS valid_previous,
       openreplay_version() = :'next_version'     AS is_next
\gset

\if :valid_previous
\echo valid previous DB version :'previous_version', starting DB upgrade to :'next_version'
BEGIN;
SELECT format($fn_def$
CREATE OR REPLACE FUNCTION openreplay_version()
    RETURNS text AS
$$
SELECT '%1$s'
$$ LANGUAGE sql IMMUTABLE;
$fn_def$, :'next_version')
\gexec

--

ALTER TABLE IF EXISTS public.sessions
    ADD COLUMN IF NOT EXISTS has_ut_test boolean DEFAULT FALSE;

-- !!! The following query takes a lot of time
CREATE INDEX IF NOT EXISTS sessions_session_id_has_ut_test_idx ON public.sessions (session_id, has_ut_test);

UPDATE public.sessions
SET has_ut_test= TRUE
WHERE session_id IN (SELECT session_id FROM public.ut_tests_signals);

ALTER TABLE IF EXISTS public.projects
    ADD COLUMN IF NOT EXISTS conditional_capture boolean DEFAULT FALSE;

CREATE TABLE IF NOT EXISTS public.projects_conditions
(
    condition_id integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    project_id   integer      NOT NULL REFERENCES public.projects (project_id) ON DELETE CASCADE,
    name         varchar(255) NOT NULL,
    capture_rate integer      NOT NULL CHECK (capture_rate >= 0 AND capture_rate <= 100),
    filters      jsonb        NOT NULL DEFAULT '[]'::jsonb
);

COMMIT;

\elif :is_next
\echo new version detected :'next_version', nothing to do
\else
\warn skipping DB upgrade of :'next_version', expected previous version :'previous_version', found :'current_version'
\endif