Skip to content

Commit bf27922

Browse files
nilmergsukhwinder33445
authored andcommitted
mysql: Introduce external_uuid to contact, contactgroup and channel
The `UUID()` function exists since MySQL 8.0 and is available in MariaDB 10.2.2. Both current minimum requirements for Icinga DB.
1 parent e3f06c9 commit bf27922

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

schema/mysql/schema.sql

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CREATE TABLE available_channel_type (
1010

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

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

2628
CREATE INDEX idx_channel_changed_at ON channel(changed_at);
2729

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

3740
CONSTRAINT pk_contact PRIMARY KEY (id),
41+
CONSTRAINT uk_contact_external_uuid UNIQUE (external_uuid),
3842

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

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

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

69-
CONSTRAINT pk_contactgroup PRIMARY KEY (id)
74+
CONSTRAINT pk_contactgroup PRIMARY KEY (id),
75+
CONSTRAINT uk_contactgroup_external_uuid UNIQUE (external_uuid)
7076
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
7177

7278
CREATE INDEX idx_contactgroup_changed_at ON contactgroup(changed_at);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
ALTER TABLE contact ADD COLUMN external_uuid char(36);
2+
ALTER TABLE contactgroup ADD COLUMN external_uuid char(36);
3+
ALTER TABLE channel ADD COLUMN external_uuid char(36);
4+
5+
UPDATE contact SET external_uuid = UUID() WHERE external_uuid IS NULL;
6+
UPDATE contactgroup SET external_uuid = UUID() WHERE external_uuid IS NULL;
7+
UPDATE channel SET external_uuid = UUID() WHERE external_uuid IS NULL;
8+
9+
ALTER TABLE contact MODIFY COLUMN external_uuid char(36) NOT NULL;
10+
ALTER TABLE contactgroup MODIFY COLUMN external_uuid char(36) NOT NULL;
11+
ALTER TABLE channel MODIFY COLUMN external_uuid char(36) NOT NULL;
12+
13+
ALTER TABLE contact ADD CONSTRAINT uk_contact_external_uuid UNIQUE (external_uuid);
14+
ALTER TABLE contactgroup ADD CONSTRAINT uk_contactgroup_external_uuid UNIQUE (external_uuid);
15+
ALTER TABLE channel ADD CONSTRAINT uk_channel_external_uuid UNIQUE (external_uuid);

0 commit comments

Comments
 (0)