refactor(DB): stop DB init script if the DB already exists

This commit is contained in:
Taha Yassine Kraiem 2024-01-23 11:50:19 +01:00
parent af4b19f7e2
commit 5e116856c9
2 changed files with 48 additions and 14 deletions

View file

@ -1,3 +1,17 @@
\set or_version 'v1.14.0-ee'
SET client_min_messages TO NOTICE;
\set ON_ERROR_STOP true
SELECT EXISTS (SELECT 1
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = 'tenants') AS db_exists;
\gset
\if :db_exists
\echo >DB already exists, stopping script
\echo >If you are trying to upgrade openreplay, please follow the instructions here: https://docs.openreplay.com/en/deployment/upgrade/
\q
\endif
BEGIN;
-- Schemas and functions definitions:
CREATE SCHEMA IF NOT EXISTS events_common;
@ -5,11 +19,14 @@ CREATE SCHEMA IF NOT EXISTS events;
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE EXTENSION IF NOT EXISTS pgcrypto;
SELECT format($fn_def$
CREATE OR REPLACE FUNCTION openreplay_version()
RETURNS text AS
$$
SELECT 'v1.14.0-ee'
SELECT '%1$s'
$$ LANGUAGE sql IMMUTABLE;
$fn_def$, :'or_version')
\gexec
CREATE OR REPLACE FUNCTION generate_api_key(length integer) RETURNS text AS
@ -194,7 +211,7 @@ $$
deleted_at timestamp without time zone NULL DEFAULT NULL,
api_key text UNIQUE DEFAULT generate_api_key(20) NOT NULL,
jwt_iat timestamp without time zone NULL DEFAULT NULL,
data jsonb NOT NULL DEFAULT'{}'::jsonb,
data jsonb NOT NULL DEFAULT '{}'::jsonb,
weekly_report boolean NOT NULL DEFAULT TRUE,
origin text NULL DEFAULT NULL,
role_id integer REFERENCES roles (role_id) ON DELETE SET NULL,
@ -323,7 +340,7 @@ $$
button_url text NULL,
image_url text NULL,
created_at timestamp NOT NULL DEFAULT timezone('utc'::text, now()),
options jsonb NOT NULL DEFAULT'{}'::jsonb,
options jsonb NOT NULL DEFAULT '{}'::jsonb,
CONSTRAINT notification_tenant_xor_user CHECK ( tenant_id NOTNULL AND user_id ISNULL OR
tenant_id ISNULL AND user_id NOTNULL )
);
@ -371,7 +388,7 @@ $$
project_id integer NOT NULL REFERENCES projects (project_id) ON DELETE CASCADE,
provider integration_provider NOT NULL,
options jsonb NOT NULL,
request_data jsonb NOT NULL DEFAULT'{}'::jsonb,
request_data jsonb NOT NULL DEFAULT '{}'::jsonb,
PRIMARY KEY (project_id, provider)
);
@ -603,8 +620,8 @@ $$
BEGIN
ALTER TABLE public.sessions
ADD CONSTRAINT web_browser_constraint CHECK (
(sessions.platform = 'web' AND sessions.user_browser NOTNULL) OR
(sessions.platform != 'web' AND sessions.user_browser ISNULL));
(sessions.platform = 'web' AND sessions.user_browser NOTNULL) OR
(sessions.platform != 'web' AND sessions.user_browser ISNULL));
EXCEPTION
WHEN duplicate_object THEN RAISE NOTICE 'Table constraint exists';
END;
@ -618,8 +635,8 @@ $$
BEGIN
ALTER TABLE public.sessions
ADD CONSTRAINT web_user_agent_constraint CHECK (
(sessions.platform = 'web' AND sessions.user_agent NOTNULL) OR
(sessions.platform != 'web' AND sessions.user_agent ISNULL));
(sessions.platform = 'web' AND sessions.user_agent NOTNULL) OR
(sessions.platform != 'web' AND sessions.user_agent ISNULL));
EXCEPTION
WHEN duplicate_object THEN RAISE NOTICE 'Table constraint already exists';
END;
@ -661,7 +678,7 @@ $$
provider oauth_provider NOT NULL,
created_by integer NOT NULL,
created_at timestamp DEFAULT timezone('utc'::text, now()) NOT NULL,
provider_data jsonb DEFAULT'{}'::jsonb NOT NULL
provider_data jsonb DEFAULT '{}'::jsonb NOT NULL
);
CREATE INDEX IF NOT EXISTS assigned_sessions_session_id_idx ON assigned_sessions (session_id);

View file

@ -1,3 +1,17 @@
\set or_version 'v1.14.0'
SET client_min_messages TO NOTICE;
\set ON_ERROR_STOP true
SELECT EXISTS (SELECT 1
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = 'tenants') AS db_exists;
\gset
\if :db_exists
\echo >DB already exists, stopping script
\echo >If you are trying to upgrade openreplay, please follow the instructions here: https://docs.openreplay.com/en/deployment/upgrade/
\q
\endif
BEGIN;
-- Schemas and functions definitions:
CREATE SCHEMA IF NOT EXISTS events_common;
@ -5,11 +19,14 @@ CREATE SCHEMA IF NOT EXISTS events;
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE EXTENSION IF NOT EXISTS pgcrypto;
SELECT format($fn_def$
CREATE OR REPLACE FUNCTION openreplay_version()
RETURNS text AS
$$
SELECT 'v1.14.0'
SELECT '%1$s'
$$ LANGUAGE sql IMMUTABLE;
$fn_def$, :'or_version')
\gexec
CREATE OR REPLACE FUNCTION generate_api_key(length integer) RETURNS text AS
@ -475,16 +492,16 @@ $$
ALTER TABLE public.sessions
ADD CONSTRAINT web_browser_constraint CHECK (
(sessions.platform = 'web' AND sessions.user_browser NOTNULL) OR
(sessions.platform != 'web' AND sessions.user_browser ISNULL));
(sessions.platform = 'web' AND sessions.user_browser NOTNULL) OR
(sessions.platform != 'web' AND sessions.user_browser ISNULL));
ALTER TABLE public.sessions
ADD CONSTRAINT web_user_browser_version_constraint CHECK ( sessions.platform = 'web' OR sessions.user_browser_version ISNULL);
ALTER TABLE public.sessions
ADD CONSTRAINT web_user_agent_constraint CHECK (
(sessions.platform = 'web' AND sessions.user_agent NOTNULL) OR
(sessions.platform != 'web' AND sessions.user_agent ISNULL));
(sessions.platform = 'web' AND sessions.user_agent NOTNULL) OR
(sessions.platform != 'web' AND sessions.user_agent ISNULL));
CREATE TABLE user_viewed_sessions