From ebe647d3661dbf97e0ff117056510477a44a27f0 Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Tue, 11 Jun 2024 10:27:47 +0200 Subject: [PATCH] Add column `external_uuid` to `contact/contactgroup` table --- schema/pgsql/schema.sql | 12 +++++++++--- schema/pgsql/upgrades/NAMEIT.sql | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 schema/pgsql/upgrades/NAMEIT.sql diff --git a/schema/pgsql/schema.sql b/schema/pgsql/schema.sql index 4002b281..aa257986 100644 --- a/schema/pgsql/schema.sql +++ b/schema/pgsql/schema.sql @@ -47,8 +47,10 @@ CREATE TABLE channel ( config text, -- JSON with channel-specific attributes -- for now type determines the implementation, in the future, this will need a reference to a concrete -- implementation to allow multiple implementations of a sms channel for example, probably even user-provided ones + external_uuid uuid NOT NULL, - CONSTRAINT pk_channel PRIMARY KEY (id) + CONSTRAINT pk_channel PRIMARY KEY (id), + UNIQUE (external_uuid) ); CREATE TABLE contact ( @@ -56,9 +58,11 @@ CREATE TABLE contact ( full_name citext NOT NULL, username citext, -- reference to web user default_channel_id bigint NOT NULL REFERENCES channel(id), + external_uuid uuid NOT NULL, CONSTRAINT pk_contact PRIMARY KEY (id), - UNIQUE (username) + UNIQUE (username), + UNIQUE (external_uuid) ); CREATE TABLE contact_address ( @@ -74,8 +78,10 @@ CREATE TABLE contact_address ( CREATE TABLE contactgroup ( id bigserial, name citext NOT NULL, + external_uuid uuid NOT NULL, - CONSTRAINT pk_contactgroup PRIMARY KEY (id) + CONSTRAINT pk_contactgroup PRIMARY KEY (id), + UNIQUE (external_uuid) ); CREATE TABLE contactgroup_member ( diff --git a/schema/pgsql/upgrades/NAMEIT.sql b/schema/pgsql/upgrades/NAMEIT.sql new file mode 100644 index 00000000..b8ef5f85 --- /dev/null +++ b/schema/pgsql/upgrades/NAMEIT.sql @@ -0,0 +1,15 @@ +CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; + +ALTER TABLE contact ADD COLUMN external_uuid uuid UNIQUE; +ALTER TABLE contactgroup ADD COLUMN external_uuid uuid UNIQUE; +ALTER TABLE channel ADD COLUMN external_uuid uuid UNIQUE; + +UPDATE contact SET external_uuid = uuid_generate_v4() WHERE external_uuid IS NULL; +UPDATE contactgroup SET external_uuid = uuid_generate_v4() WHERE external_uuid IS NULL; +UPDATE channel SET external_uuid = uuid_generate_v4() WHERE external_uuid IS NULL; + +ALTER TABLE contact ALTER COLUMN external_uuid SET NOT NULL; +ALTER TABLE contactgroup ALTER COLUMN external_uuid SET NOT NULL; +ALTER TABLE channel ALTER COLUMN external_uuid SET NOT NULL; + +DROP EXTENSION "uuid-ossp";