From e445ba6434325dcf1ebc7fcf4f06e27c6a3f2197 Mon Sep 17 00:00:00 2001 From: Bastian Krol Date: Mon, 5 Feb 2024 12:18:39 +0100 Subject: [PATCH] [postgres] make init scripts idempotent This allows to attach a persistent volume to the PostgreSQL service and still always run the init scripts on startup. For the first startup with a fresh volume, the database will be initialized correctly; on subsequent starts the init scripts will do nothing. --- src/ffspostgres/init-scripts/10-ffs_schema.sql | 5 +++-- src/ffspostgres/init-scripts/20-ffs_data.sql | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/ffspostgres/init-scripts/10-ffs_schema.sql b/src/ffspostgres/init-scripts/10-ffs_schema.sql index e86f047780..309abca499 100644 --- a/src/ffspostgres/init-scripts/10-ffs_schema.sql +++ b/src/ffspostgres/init-scripts/10-ffs_schema.sql @@ -1,13 +1,14 @@ -- Copyright The OpenTelemetry Authors -- SPDX-License-Identifier: Apache-2.0 -CREATE TABLE public.featureflags ( +CREATE TABLE IF NOT EXISTS public.featureflags ( name character varying(255), description character varying(255), enabled double precision DEFAULT 0.0 NOT NULL ); +ALTER TABLE ONLY public.featureflags DROP CONSTRAINT featureflags_pkey; ALTER TABLE ONLY public.featureflags ADD CONSTRAINT featureflags_pkey PRIMARY KEY (name); -CREATE UNIQUE INDEX featureflags_name_index ON public.featureflags USING btree (name); +CREATE UNIQUE INDEX IF NOT EXISTS featureflags_name_index ON public.featureflags USING btree (name); diff --git a/src/ffspostgres/init-scripts/20-ffs_data.sql b/src/ffspostgres/init-scripts/20-ffs_data.sql index 56a6ecccc8..d5e9fb26a4 100644 --- a/src/ffspostgres/init-scripts/20-ffs_data.sql +++ b/src/ffspostgres/init-scripts/20-ffs_data.sql @@ -13,4 +13,5 @@ VALUES ('paymentServiceSimulateSlownessUpperBound', 'Maximum simulated delay in milliseconds in payment service, if enabled', 600), ('shippingServiceSimulateSlowness', 'Simulate slow response times in the shipping service', 0), ('shippingServiceSimulateSlownessLowerBound', 'Minimum simulated delay in milliseconds in shipping service, if enabled', 250), - ('shippingServiceSimulateSlownessUpperBound', 'Maximum simulated delay in milliseconds in shipping service, if enabled', 400); + ('shippingServiceSimulateSlownessUpperBound', 'Maximum simulated delay in milliseconds in shipping service, if enabled', 400) + ON CONFLICT DO NOTHING;