Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion schema/mysql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CREATE TABLE available_channel_type (

CREATE TABLE channel (
id bigint NOT NULL AUTO_INCREMENT,
external_uuid char(36) NOT NULL, -- used for external references, lower case
name text NOT NULL COLLATE utf8mb4_unicode_ci,
type varchar(255) NOT NULL, -- 'email', 'sms', ...
config mediumtext, -- JSON with channel-specific attributes
Expand All @@ -20,13 +21,15 @@ CREATE TABLE channel (
deleted enum('n', 'y') NOT NULL DEFAULT 'n',

CONSTRAINT pk_channel PRIMARY KEY (id),
CONSTRAINT uk_channel_external_uuid UNIQUE (external_uuid),
CONSTRAINT fk_channel_available_channel_type FOREIGN KEY (type) REFERENCES available_channel_type(type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

CREATE INDEX idx_channel_changed_at ON channel(changed_at);

CREATE TABLE contact (
id bigint NOT NULL AUTO_INCREMENT,
external_uuid char(36) NOT NULL, -- used for external references, lower case
full_name text NOT NULL COLLATE utf8mb4_unicode_ci,
username varchar(254) COLLATE utf8mb4_unicode_ci, -- reference to web user
default_channel_id bigint NOT NULL,
Expand All @@ -35,6 +38,7 @@ CREATE TABLE contact (
deleted enum('n', 'y') NOT NULL DEFAULT 'n',

CONSTRAINT pk_contact PRIMARY KEY (id),
CONSTRAINT uk_contact_external_uuid UNIQUE (external_uuid),

-- As the username is unique, it must be NULLed for deletion via "deleted = 'y'"
CONSTRAINT uk_contact_username UNIQUE (username),
Expand All @@ -61,12 +65,14 @@ CREATE INDEX idx_contact_address_changed_at ON contact_address(changed_at);

CREATE TABLE contactgroup (
id bigint NOT NULL AUTO_INCREMENT,
external_uuid char(36) NOT NULL, -- used for external references, lower case
name text NOT NULL COLLATE utf8mb4_unicode_ci,

changed_at bigint NOT NULL,
deleted enum('n', 'y') NOT NULL DEFAULT 'n',

CONSTRAINT pk_contactgroup PRIMARY KEY (id)
CONSTRAINT pk_contactgroup PRIMARY KEY (id),
CONSTRAINT uk_contactgroup_external_uuid UNIQUE (external_uuid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

CREATE INDEX idx_contactgroup_changed_at ON contactgroup(changed_at);
Expand Down
15 changes: 15 additions & 0 deletions schema/mysql/upgrades/0.2.0-external-uuid.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ALTER TABLE contact ADD COLUMN external_uuid char(36) AFTER id;
ALTER TABLE contactgroup ADD COLUMN external_uuid char(36) AFTER id;
ALTER TABLE channel ADD COLUMN external_uuid char(36) AFTER id;

UPDATE contact SET external_uuid = UUID() WHERE external_uuid IS NULL;
UPDATE contactgroup SET external_uuid = UUID() WHERE external_uuid IS NULL;
UPDATE channel SET external_uuid = UUID() WHERE external_uuid IS NULL;

ALTER TABLE contact MODIFY COLUMN external_uuid char(36) NOT NULL;
ALTER TABLE contactgroup MODIFY COLUMN external_uuid char(36) NOT NULL;
ALTER TABLE channel MODIFY COLUMN external_uuid char(36) NOT NULL;

ALTER TABLE contact ADD CONSTRAINT uk_contact_external_uuid UNIQUE (external_uuid);
ALTER TABLE contactgroup ADD CONSTRAINT uk_contactgroup_external_uuid UNIQUE (external_uuid);
ALTER TABLE channel ADD CONSTRAINT uk_channel_external_uuid UNIQUE (external_uuid);
8 changes: 7 additions & 1 deletion schema/pgsql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ CREATE TABLE available_channel_type (

CREATE TABLE channel (
id bigserial,
external_uuid uuid NOT NULL, -- used for external references
name citext NOT NULL,
type varchar(255) NOT NULL, -- 'email', 'sms', ...
config text, -- JSON with channel-specific attributes
Expand All @@ -52,13 +53,15 @@ CREATE TABLE channel (
deleted boolenum NOT NULL DEFAULT 'n',

CONSTRAINT pk_channel PRIMARY KEY (id),
CONSTRAINT uk_channel_external_uuid UNIQUE (external_uuid),
CONSTRAINT fk_channel_available_channel_type FOREIGN KEY (type) REFERENCES available_channel_type(type)
);

CREATE INDEX idx_channel_changed_at ON channel(changed_at);

CREATE TABLE contact (
id bigserial,
external_uuid uuid NOT NULL, -- used for external references
full_name citext NOT NULL,
username citext, -- reference to web user
default_channel_id bigint NOT NULL,
Expand All @@ -67,6 +70,7 @@ CREATE TABLE contact (
deleted boolenum NOT NULL DEFAULT 'n',

CONSTRAINT pk_contact PRIMARY KEY (id),
CONSTRAINT uk_contact_external_uuid UNIQUE (external_uuid),

-- As the username is unique, it must be NULLed for deletion via "deleted = 'y'"
CONSTRAINT uk_contact_username UNIQUE (username),
Expand Down Expand Up @@ -94,12 +98,14 @@ CREATE INDEX idx_contact_address_changed_at ON contact_address(changed_at);

CREATE TABLE contactgroup (
id bigserial,
external_uuid uuid NOT NULL, -- used for external references
name citext NOT NULL,

changed_at bigint NOT NULL,
deleted boolenum NOT NULL DEFAULT 'n',

CONSTRAINT pk_contactgroup PRIMARY KEY (id)
CONSTRAINT pk_contactgroup PRIMARY KEY (id),
CONSTRAINT uk_contactgroup_external_uuid UNIQUE (external_uuid)
);

CREATE INDEX idx_contactgroup_changed_at ON contactgroup(changed_at);
Expand Down
15 changes: 15 additions & 0 deletions schema/pgsql/upgrades/0.2.0-external-uuid.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

ALTER TABLE contact ADD COLUMN external_uuid uuid CONSTRAINT uk_contact_external_uuid UNIQUE;
ALTER TABLE contactgroup ADD COLUMN external_uuid uuid CONSTRAINT uk_contactgroup_external_uuid UNIQUE;
ALTER TABLE channel ADD COLUMN external_uuid uuid CONSTRAINT uk_channel_external_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";
Loading