From 8de6d833b72a5d7ca3cd0c4a60cf0c7aec5fce4b Mon Sep 17 00:00:00 2001 From: Taha Yassine Kraiem Date: Tue, 23 Jan 2024 12:13:31 +0100 Subject: [PATCH] refactor(DB): stop DB init script if the DB already exists --- .../db/init_dbs/postgresql/init_schema.sql | 20 +++++++++++++++++-- .../db/init_dbs/postgresql/init_schema.sql | 20 +++++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/ee/scripts/schema/db/init_dbs/postgresql/init_schema.sql b/ee/scripts/schema/db/init_dbs/postgresql/init_schema.sql index 0f2157908..393167968 100644 --- a/ee/scripts/schema/db/init_dbs/postgresql/init_schema.sql +++ b/ee/scripts/schema/db/init_dbs/postgresql/init_schema.sql @@ -1,3 +1,17 @@ +\set or_version 'v1.17.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; @@ -6,12 +20,14 @@ CREATE SCHEMA IF NOT EXISTS events_ios; 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.17.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 $$ diff --git a/scripts/schema/db/init_dbs/postgresql/init_schema.sql b/scripts/schema/db/init_dbs/postgresql/init_schema.sql index c8a4f3d3f..0032d8f8f 100644 --- a/scripts/schema/db/init_dbs/postgresql/init_schema.sql +++ b/scripts/schema/db/init_dbs/postgresql/init_schema.sql @@ -1,3 +1,17 @@ +\set or_version 'v1.17.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; @@ -6,12 +20,14 @@ CREATE SCHEMA IF NOT EXISTS events_ios; 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.17.0' +SELECT '%1$s' $$ LANGUAGE sql IMMUTABLE; - +$fn_def$, :'or_version') +\gexec CREATE OR REPLACE FUNCTION generate_api_key(length integer) RETURNS text AS $$