thingsboard/dao/src/main/resources/sql/schema-entities.sql

916 lines
30 KiB
MySQL
Raw Normal View History

--
2024-01-09 10:46:16 +02:00
-- Copyright © 2016-2024 The Thingsboard Authors
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
2017-06-01 16:50:37 +03:00
CREATE TABLE IF NOT EXISTS tb_schema_settings
(
schema_version bigint NOT NULL,
CONSTRAINT tb_schema_settings_pkey PRIMARY KEY (schema_version)
);
CREATE OR REPLACE PROCEDURE insert_tb_schema_settings()
LANGUAGE plpgsql AS
$$
BEGIN
IF (SELECT COUNT(*) FROM tb_schema_settings) = 0 THEN
2024-04-11 15:57:06 +03:00
INSERT INTO tb_schema_settings (schema_version) VALUES (3006004);
END IF;
END;
$$;
call insert_tb_schema_settings();
CREATE TABLE IF NOT EXISTS admin_settings (
2020-06-22 11:10:28 +03:00
id uuid NOT NULL CONSTRAINT admin_settings_pkey PRIMARY KEY,
tenant_id uuid NOT NULL,
2020-06-22 11:10:28 +03:00
created_time bigint NOT NULL,
2017-07-01 20:39:00 +03:00
json_value varchar,
2017-07-17 17:32:32 +03:00
key varchar(255)
);
CREATE TABLE IF NOT EXISTS alarm (
2020-06-22 11:10:28 +03:00
id uuid NOT NULL CONSTRAINT alarm_pkey PRIMARY KEY,
created_time bigint NOT NULL,
ack_ts bigint,
clear_ts bigint,
2017-07-01 20:39:00 +03:00
additional_info varchar,
end_ts bigint,
2020-06-22 11:10:28 +03:00
originator_id uuid,
originator_type integer,
propagate boolean,
2017-07-17 17:32:32 +03:00
severity varchar(255),
start_ts bigint,
assign_ts bigint DEFAULT 0,
assignee_id uuid,
2020-06-22 11:10:28 +03:00
tenant_id uuid,
2021-05-07 16:05:54 +03:00
customer_id uuid,
2019-12-08 23:52:29 +02:00
propagate_relation_types varchar,
type varchar(255),
propagate_to_owner boolean,
2023-02-20 17:11:49 +02:00
propagate_to_tenant boolean,
acknowledged boolean,
cleared boolean
);
CREATE TABLE IF NOT EXISTS alarm_comment (
id uuid NOT NULL,
created_time bigint NOT NULL,
alarm_id uuid NOT NULL,
user_id uuid,
type varchar(255) NOT NULL,
comment varchar(10000),
CONSTRAINT fk_alarm_comment_alarm_id FOREIGN KEY (alarm_id) REFERENCES alarm(id) ON DELETE CASCADE
) PARTITION BY RANGE (created_time);
2021-12-07 18:50:16 +02:00
CREATE TABLE IF NOT EXISTS entity_alarm (
tenant_id uuid NOT NULL,
entity_type varchar(32),
entity_id uuid NOT NULL,
created_time bigint NOT NULL,
alarm_type varchar(255) NOT NULL,
customer_id uuid,
alarm_id uuid,
2021-12-09 15:12:03 +02:00
CONSTRAINT entity_alarm_pkey PRIMARY KEY (entity_id, alarm_id),
2021-12-07 18:50:16 +02:00
CONSTRAINT fk_entity_alarm_id FOREIGN KEY (alarm_id) REFERENCES alarm(id) ON DELETE CASCADE
);
2018-02-09 17:17:44 +02:00
CREATE TABLE IF NOT EXISTS audit_log (
id uuid NOT NULL,
2020-06-22 11:10:28 +03:00
created_time bigint NOT NULL,
tenant_id uuid,
customer_id uuid,
entity_id uuid,
2018-02-09 17:17:44 +02:00
entity_type varchar(255),
entity_name varchar(255),
2020-06-22 11:10:28 +03:00
user_id uuid,
2018-02-09 17:17:44 +02:00
user_name varchar(255),
action_type varchar(255),
2018-02-21 20:05:03 +02:00
action_data varchar(1000000),
2018-02-09 17:17:44 +02:00
action_status varchar(255),
2018-02-21 20:05:03 +02:00
action_failure_details varchar(1000000)
) PARTITION BY RANGE (created_time);
2018-02-09 17:17:44 +02:00
2024-07-29 21:31:14 +02:00
CREATE SEQUENCE IF NOT EXISTS attribute_kv_version_seq cache 1;
2024-06-26 15:51:32 +02:00
2017-06-13 16:29:40 +03:00
CREATE TABLE IF NOT EXISTS attribute_kv (
2020-06-22 11:10:28 +03:00
entity_id uuid,
attribute_type int,
attribute_key int,
2017-06-09 19:52:26 +03:00
bool_v boolean,
2017-08-17 16:38:13 +03:00
str_v varchar(10000000),
2017-06-09 19:52:26 +03:00
long_v bigint,
dbl_v double precision,
json_v json,
2017-06-09 19:52:26 +03:00
last_update_ts bigint,
version bigint default 0,
CONSTRAINT attribute_kv_pkey PRIMARY KEY (entity_id, attribute_type, attribute_key)
2017-06-09 19:52:26 +03:00
);
CREATE TABLE IF NOT EXISTS component_descriptor (
2020-06-22 11:10:28 +03:00
id uuid NOT NULL CONSTRAINT component_descriptor_pkey PRIMARY KEY,
created_time bigint NOT NULL,
2017-07-17 17:32:32 +03:00
actions varchar(255),
2018-10-30 13:32:55 +02:00
clazz varchar UNIQUE,
2017-07-01 20:39:00 +03:00
configuration_descriptor varchar,
configuration_version int DEFAULT 0,
2017-07-17 17:32:32 +03:00
name varchar(255),
scope varchar(255),
type varchar(255),
clustering_mode varchar(255),
has_queue_name boolean DEFAULT false
);
CREATE TABLE IF NOT EXISTS customer (
id uuid NOT NULL CONSTRAINT customer_pkey PRIMARY KEY,
created_time bigint NOT NULL,
additional_info varchar,
address varchar,
address2 varchar,
city varchar(255),
country varchar(255),
email varchar(255),
phone varchar(255),
state varchar(255),
tenant_id uuid,
title varchar(255),
zip varchar(255),
2022-07-15 18:00:33 +03:00
external_id uuid,
Replacement of local cache in rule nodes with cached entity service APIs. (#9552) * added customer cache && refactored assign/unassing to/from customer nodes * refactoring of customer action nodes & added common tests * added tests for upgrade * updated controller & service tests for customer api * replaced findCustomerByTenantIdAndTitle with cacheable method * add new line to end of ../upgrade/3.6.2/schema_update.sql file * added cache for find user by tenant id and email & relation action nodes refactoring * fixed synchronization issue & refactored logic & refactored node upgrade tests * fixed testSaveUserWithSameEmail test in the UserControllerTest. Removed empty space & added exclamation mark * updated tests for create relation node && added two tests for delete relation node * replaced Lists with EnumSets where it was possible & replaced MethodSource with EnumSource where it was possible & added test for upgrade for both create and delete relation nodes * updated upgrade tests & added tests for delete relation node * refactoring * updated create & delete relation nodes descriptions * updated default config for delete relation node * minor improvements for custom action nodes after self review * added upgrade for customers with the same titles * replaced parameterized test annotation when deleteForSingleEntity is false * fix typo in reSaveCustomerWithNewTitle upgrade logic * fixes after review & additional improvements * fixed testAssignAndUnassignEntityViewToCustomer test * updated upgrade logic & changed logic of public customers lookup * fixed dao method & added test * renamed method to findPublicCustomerByTenantId * fix typo in the find public customer query * added @Serial annotation for serialVersionUID & rollback saveAndFlush to save in UserServiceImpl * fixes to async find or create logic * fixed after review * fix typo * set Transactional annotation to findOrCreatePublicCustomer
2024-04-10 12:31:24 +03:00
is_public boolean,
2024-07-17 16:51:07 +03:00
version BIGINT DEFAULT 1,
Replacement of local cache in rule nodes with cached entity service APIs. (#9552) * added customer cache && refactored assign/unassing to/from customer nodes * refactoring of customer action nodes & added common tests * added tests for upgrade * updated controller & service tests for customer api * replaced findCustomerByTenantIdAndTitle with cacheable method * add new line to end of ../upgrade/3.6.2/schema_update.sql file * added cache for find user by tenant id and email & relation action nodes refactoring * fixed synchronization issue & refactored logic & refactored node upgrade tests * fixed testSaveUserWithSameEmail test in the UserControllerTest. Removed empty space & added exclamation mark * updated tests for create relation node && added two tests for delete relation node * replaced Lists with EnumSets where it was possible & replaced MethodSource with EnumSource where it was possible & added test for upgrade for both create and delete relation nodes * updated upgrade tests & added tests for delete relation node * refactoring * updated create & delete relation nodes descriptions * updated default config for delete relation node * minor improvements for custom action nodes after self review * added upgrade for customers with the same titles * replaced parameterized test annotation when deleteForSingleEntity is false * fix typo in reSaveCustomerWithNewTitle upgrade logic * fixes after review & additional improvements * fixed testAssignAndUnassignEntityViewToCustomer test * updated upgrade logic & changed logic of public customers lookup * fixed dao method & added test * renamed method to findPublicCustomerByTenantId * fix typo in the find public customer query * added @Serial annotation for serialVersionUID & rollback saveAndFlush to save in UserServiceImpl * fixes to async find or create logic * fixed after review * fix typo * set Transactional annotation to findOrCreatePublicCustomer
2024-04-10 12:31:24 +03:00
CONSTRAINT customer_title_unq_key UNIQUE (tenant_id, title),
2022-07-15 18:00:33 +03:00
CONSTRAINT customer_external_id_unq_key UNIQUE (tenant_id, external_id)
);
CREATE TABLE IF NOT EXISTS dashboard (
id uuid NOT NULL CONSTRAINT dashboard_pkey PRIMARY KEY,
created_time bigint NOT NULL,
configuration varchar,
assigned_customers varchar(1000000),
tenant_id uuid,
title varchar(255),
mobile_hide boolean DEFAULT false,
mobile_order int,
image varchar(1000000),
2022-07-15 18:00:33 +03:00
external_id uuid,
2024-07-17 16:51:07 +03:00
version BIGINT DEFAULT 1,
2022-07-15 18:00:33 +03:00
CONSTRAINT dashboard_external_id_unq_key UNIQUE (tenant_id, external_id)
);
2020-09-04 12:53:22 +03:00
CREATE TABLE IF NOT EXISTS rule_chain (
id uuid NOT NULL CONSTRAINT rule_chain_pkey PRIMARY KEY,
created_time bigint NOT NULL,
additional_info varchar,
configuration varchar(10000000),
name varchar(255),
type varchar(255),
first_rule_node_id uuid,
root boolean,
debug_mode boolean,
tenant_id uuid,
2022-07-15 18:00:33 +03:00
external_id uuid,
2024-07-17 16:51:07 +03:00
version BIGINT DEFAULT 1,
2022-07-15 18:00:33 +03:00
CONSTRAINT rule_chain_external_id_unq_key UNIQUE (tenant_id, external_id)
2020-09-04 12:53:22 +03:00
);
CREATE TABLE IF NOT EXISTS rule_node (
id uuid NOT NULL CONSTRAINT rule_node_pkey PRIMARY KEY,
created_time bigint NOT NULL,
rule_chain_id uuid,
additional_info varchar,
configuration_version int DEFAULT 0,
2020-09-04 12:53:22 +03:00
configuration varchar(10000000),
type varchar(255),
name varchar(255),
debug_mode boolean,
singleton_mode boolean,
queue_name varchar(255),
2022-06-17 16:10:29 +03:00
external_id uuid
2020-09-04 12:53:22 +03:00
);
2020-10-02 17:55:40 +03:00
CREATE TABLE IF NOT EXISTS rule_node_state (
id uuid NOT NULL CONSTRAINT rule_node_state_pkey PRIMARY KEY,
created_time bigint NOT NULL,
rule_node_id uuid NOT NULL,
entity_type varchar(32) NOT NULL,
entity_id uuid NOT NULL,
state_data varchar(16384) NOT NULL,
CONSTRAINT rule_node_state_unq_key UNIQUE (rule_node_id, entity_id),
CONSTRAINT fk_rule_node_state_node_id FOREIGN KEY (rule_node_id) REFERENCES rule_node(id) ON DELETE CASCADE
);
2021-05-31 16:43:33 +03:00
CREATE TABLE IF NOT EXISTS ota_package (
id uuid NOT NULL CONSTRAINT ota_package_pkey PRIMARY KEY,
2021-04-08 11:45:29 +03:00
created_time bigint NOT NULL,
tenant_id uuid NOT NULL,
device_profile_id uuid ,
2021-04-28 12:44:57 +03:00
type varchar(32) NOT NULL,
2021-04-08 11:45:29 +03:00
title varchar(255) NOT NULL,
version varchar(255) NOT NULL,
2021-07-22 17:44:45 +03:00
tag varchar(255),
url varchar(255),
file_name varchar(255),
content_type varchar(255),
checksum_algorithm varchar(32),
checksum varchar(1020),
data oid,
2021-04-23 14:35:47 +03:00
data_size bigint,
additional_info varchar,
2021-05-31 16:43:33 +03:00
CONSTRAINT ota_package_tenant_title_version_unq_key UNIQUE (tenant_id, title, version)
2021-04-08 10:30:30 +03:00
);
2022-07-15 18:00:33 +03:00
CREATE TABLE IF NOT EXISTS queue (
id uuid NOT NULL CONSTRAINT queue_pkey PRIMARY KEY,
created_time bigint NOT NULL,
tenant_id uuid,
name varchar(255),
topic varchar(255),
poll_interval int,
partitions int,
consumer_per_partition boolean,
pack_processing_timeout bigint,
submit_strategy varchar(255),
2022-05-10 19:44:58 +02:00
processing_strategy varchar(255),
additional_info varchar
);
2022-09-23 15:37:17 +03:00
CREATE TABLE IF NOT EXISTS asset_profile (
2022-09-23 19:26:50 +03:00
id uuid NOT NULL CONSTRAINT asset_profile_pkey PRIMARY KEY,
created_time bigint NOT NULL,
name varchar(255),
2022-09-23 15:37:17 +03:00
image varchar(1000000),
description varchar,
is_default boolean,
tenant_id uuid,
default_rule_chain_id uuid,
default_dashboard_id uuid,
default_queue_name varchar(255),
default_edge_rule_chain_id uuid,
2022-09-23 15:37:17 +03:00
external_id uuid,
2024-07-17 16:51:07 +03:00
version BIGINT DEFAULT 1,
2022-09-23 15:37:17 +03:00
CONSTRAINT asset_profile_name_unq_key UNIQUE (tenant_id, name),
CONSTRAINT asset_profile_external_id_unq_key UNIQUE (tenant_id, external_id),
CONSTRAINT fk_default_rule_chain_asset_profile FOREIGN KEY (default_rule_chain_id) REFERENCES rule_chain(id),
CONSTRAINT fk_default_dashboard_asset_profile FOREIGN KEY (default_dashboard_id) REFERENCES dashboard(id),
CONSTRAINT fk_default_edge_rule_chain_asset_profile FOREIGN KEY (default_edge_rule_chain_id) REFERENCES rule_chain(id)
2022-09-23 15:37:17 +03:00
);
CREATE TABLE IF NOT EXISTS asset (
2022-09-23 19:26:50 +03:00
id uuid NOT NULL CONSTRAINT asset_pkey PRIMARY KEY,
created_time bigint NOT NULL,
additional_info varchar,
customer_id uuid,
asset_profile_id uuid NOT NULL,
name varchar(255),
2022-09-23 15:37:17 +03:00
label varchar(255),
tenant_id uuid,
type varchar(255),
external_id uuid,
2024-07-17 16:51:07 +03:00
version BIGINT DEFAULT 1,
2022-09-23 15:37:17 +03:00
CONSTRAINT asset_name_unq_key UNIQUE (tenant_id, name),
CONSTRAINT asset_external_id_unq_key UNIQUE (tenant_id, external_id),
CONSTRAINT fk_asset_profile FOREIGN KEY (asset_profile_id) REFERENCES asset_profile(id)
);
2020-08-18 18:58:53 +03:00
CREATE TABLE IF NOT EXISTS device_profile (
id uuid NOT NULL CONSTRAINT device_profile_pkey PRIMARY KEY,
created_time bigint NOT NULL,
name varchar(255),
type varchar(255),
image varchar(1000000),
transport_type varchar(255),
provision_type varchar(255),
profile_data jsonb,
description varchar,
is_default boolean,
tenant_id uuid,
firmware_id uuid,
software_id uuid,
default_rule_chain_id uuid,
default_dashboard_id uuid,
2022-07-11 18:51:38 +03:00
default_queue_name varchar(255),
provision_device_key varchar,
default_edge_rule_chain_id uuid,
external_id uuid,
2024-07-17 16:51:07 +03:00
version BIGINT DEFAULT 1,
CONSTRAINT device_profile_name_unq_key UNIQUE (tenant_id, name),
CONSTRAINT device_provision_key_unq_key UNIQUE (provision_device_key),
2022-07-15 18:00:33 +03:00
CONSTRAINT device_profile_external_id_unq_key UNIQUE (tenant_id, external_id),
CONSTRAINT fk_default_rule_chain_device_profile FOREIGN KEY (default_rule_chain_id) REFERENCES rule_chain(id),
CONSTRAINT fk_default_dashboard_device_profile FOREIGN KEY (default_dashboard_id) REFERENCES dashboard(id),
CONSTRAINT fk_firmware_device_profile FOREIGN KEY (firmware_id) REFERENCES ota_package(id),
CONSTRAINT fk_software_device_profile FOREIGN KEY (software_id) REFERENCES ota_package(id),
CONSTRAINT fk_default_edge_rule_chain_device_profile FOREIGN KEY (default_edge_rule_chain_id) REFERENCES rule_chain(id)
2020-08-18 18:58:53 +03:00
);
DO
$$
BEGIN
IF NOT EXISTS(SELECT 1 FROM pg_constraint WHERE conname = 'fk_device_profile_ota_package') THEN
ALTER TABLE ota_package
ADD CONSTRAINT fk_device_profile_ota_package
FOREIGN KEY (device_profile_id) REFERENCES device_profile (id)
ON DELETE CASCADE;
END IF;
END;
$$;
-- We will use one-to-many relation in the first release and extend this feature in case of user requests
-- CREATE TABLE IF NOT EXISTS device_profile_firmware (
-- device_profile_id uuid NOT NULL,
-- firmware_id uuid NOT NULL,
-- CONSTRAINT device_profile_firmware_unq_key UNIQUE (device_profile_id, firmware_id),
-- CONSTRAINT fk_device_profile_firmware_device_profile FOREIGN KEY (device_profile_id) REFERENCES device_profile(id) ON DELETE CASCADE,
-- CONSTRAINT fk_device_profile_firmware_firmware FOREIGN KEY (firmware_id) REFERENCES firmware(id) ON DELETE CASCADE,
-- );
2020-08-20 19:44:29 +03:00
CREATE TABLE IF NOT EXISTS device (
id uuid NOT NULL CONSTRAINT device_pkey PRIMARY KEY,
created_time bigint NOT NULL,
additional_info varchar,
customer_id uuid,
device_profile_id uuid NOT NULL,
device_data jsonb,
type varchar(255),
name varchar(255),
label varchar(255),
tenant_id uuid,
firmware_id uuid,
software_id uuid,
external_id uuid,
2024-07-17 16:51:07 +03:00
version BIGINT DEFAULT 1,
CONSTRAINT device_name_unq_key UNIQUE (tenant_id, name),
2022-07-15 18:00:33 +03:00
CONSTRAINT device_external_id_unq_key UNIQUE (tenant_id, external_id),
CONSTRAINT fk_device_profile FOREIGN KEY (device_profile_id) REFERENCES device_profile(id),
CONSTRAINT fk_firmware_device FOREIGN KEY (firmware_id) REFERENCES ota_package(id),
CONSTRAINT fk_software_device FOREIGN KEY (software_id) REFERENCES ota_package(id)
2020-08-20 19:44:29 +03:00
);
CREATE TABLE IF NOT EXISTS device_credentials (
2020-06-22 11:10:28 +03:00
id uuid NOT NULL CONSTRAINT device_credentials_pkey PRIMARY KEY,
created_time bigint NOT NULL,
2017-07-17 17:32:32 +03:00
credentials_id varchar,
credentials_type varchar(255),
2017-07-17 21:19:31 +03:00
credentials_value varchar,
2020-06-22 11:10:28 +03:00
device_id uuid,
2024-07-17 16:51:07 +03:00
version BIGINT DEFAULT 1,
CONSTRAINT device_credentials_id_unq_key UNIQUE (credentials_id),
CONSTRAINT device_credentials_device_id_unq_key UNIQUE (device_id)
);
2022-07-22 18:53:41 +03:00
CREATE TABLE IF NOT EXISTS rule_node_debug_event (
id uuid NOT NULL,
2022-07-22 18:53:41 +03:00
tenant_id uuid NOT NULL ,
2020-04-22 16:16:58 +03:00
ts bigint NOT NULL,
2022-07-22 18:53:41 +03:00
entity_id uuid NOT NULL,
service_id varchar,
e_type varchar,
e_entity_id uuid,
e_entity_type varchar,
e_msg_id uuid,
e_msg_type varchar,
e_data_type varchar,
e_relation_type varchar,
e_data varchar,
e_metadata varchar,
e_error varchar
2022-07-22 18:53:41 +03:00
) PARTITION BY RANGE (ts);
CREATE TABLE IF NOT EXISTS rule_chain_debug_event (
id uuid NOT NULL,
2022-07-22 18:53:41 +03:00
tenant_id uuid NOT NULL,
ts bigint NOT NULL,
entity_id uuid NOT NULL,
service_id varchar NOT NULL,
e_message varchar,
e_error varchar
) PARTITION BY RANGE (ts);
CREATE TABLE IF NOT EXISTS stats_event (
id uuid NOT NULL,
2022-07-22 18:53:41 +03:00
tenant_id uuid NOT NULL,
ts bigint NOT NULL,
entity_id uuid NOT NULL,
service_id varchar NOT NULL,
e_messages_processed bigint NOT NULL,
e_errors_occurred bigint NOT NULL
2022-07-22 18:53:41 +03:00
) PARTITION BY RANGE (ts);
CREATE TABLE IF NOT EXISTS lc_event (
id uuid NOT NULL,
2022-07-22 18:53:41 +03:00
tenant_id uuid NOT NULL,
ts bigint NOT NULL,
entity_id uuid NOT NULL,
service_id varchar NOT NULL,
e_type varchar NOT NULL,
e_success boolean NOT NULL,
e_error varchar
) PARTITION BY RANGE (ts);
CREATE TABLE IF NOT EXISTS error_event (
id uuid NOT NULL,
2022-07-22 18:53:41 +03:00
tenant_id uuid NOT NULL,
ts bigint NOT NULL,
entity_id uuid NOT NULL,
service_id varchar NOT NULL,
e_method varchar NOT NULL,
e_error varchar
) PARTITION BY RANGE (ts);
2024-07-29 21:31:14 +02:00
CREATE SEQUENCE IF NOT EXISTS relation_version_seq cache 1;
2024-07-24 13:35:29 +02:00
CREATE TABLE IF NOT EXISTS relation (
2020-06-22 11:10:28 +03:00
from_id uuid,
2017-07-17 17:32:32 +03:00
from_type varchar(255),
2020-06-22 11:10:28 +03:00
to_id uuid,
2017-07-17 17:32:32 +03:00
to_type varchar(255),
relation_type_group varchar(255),
relation_type varchar(255),
2017-07-01 20:39:00 +03:00
additional_info varchar,
2024-07-24 13:35:29 +02:00
version bigint default 0,
2018-12-20 14:53:16 +01:00
CONSTRAINT relation_pkey PRIMARY KEY (from_id, from_type, relation_type_group, relation_type, to_id, to_type)
);
CREATE TABLE IF NOT EXISTS tb_user (
2020-06-22 11:10:28 +03:00
id uuid NOT NULL CONSTRAINT tb_user_pkey PRIMARY KEY,
created_time bigint NOT NULL,
2017-07-01 20:39:00 +03:00
additional_info varchar,
2017-07-17 17:32:32 +03:00
authority varchar(255),
2020-06-22 11:10:28 +03:00
customer_id uuid,
2017-07-17 17:32:32 +03:00
email varchar(255) UNIQUE,
first_name varchar(255),
last_name varchar(255),
phone varchar(255),
2024-07-01 13:11:59 +03:00
tenant_id uuid,
2024-07-17 16:51:07 +03:00
version BIGINT DEFAULT 1
);
2020-08-20 19:44:29 +03:00
CREATE TABLE IF NOT EXISTS tenant_profile (
id uuid NOT NULL CONSTRAINT tenant_profile_pkey PRIMARY KEY,
created_time bigint NOT NULL,
name varchar(255),
profile_data jsonb,
2020-08-20 19:44:29 +03:00
description varchar,
is_default boolean,
isolated_tb_core boolean,
isolated_tb_rule_engine boolean,
CONSTRAINT tenant_profile_name_unq_key UNIQUE (name)
);
CREATE TABLE IF NOT EXISTS tenant (
2020-06-22 11:10:28 +03:00
id uuid NOT NULL CONSTRAINT tenant_pkey PRIMARY KEY,
created_time bigint NOT NULL,
2017-07-01 20:39:00 +03:00
additional_info varchar,
2020-08-18 18:58:53 +03:00
tenant_profile_id uuid NOT NULL,
2017-07-17 17:32:32 +03:00
address varchar,
address2 varchar,
city varchar(255),
country varchar(255),
email varchar(255),
phone varchar(255),
region varchar(255),
state varchar(255),
title varchar(255),
zip varchar(255),
2024-07-22 14:27:26 +03:00
version BIGINT DEFAULT 1,
2020-08-20 19:44:29 +03:00
CONSTRAINT fk_tenant_profile FOREIGN KEY (tenant_profile_id) REFERENCES tenant_profile(id)
2020-08-18 18:58:53 +03:00
);
CREATE TABLE IF NOT EXISTS user_credentials (
2020-06-22 11:10:28 +03:00
id uuid NOT NULL CONSTRAINT user_credentials_pkey PRIMARY KEY,
created_time bigint NOT NULL,
2017-07-17 17:32:32 +03:00
activate_token varchar(255) UNIQUE,
activate_token_exp_time BIGINT,
enabled boolean,
2017-07-17 17:32:32 +03:00
password varchar(255),
reset_token varchar(255) UNIQUE,
reset_token_exp_time BIGINT,
user_id uuid UNIQUE,
additional_info varchar DEFAULT '{}'
);
CREATE TABLE IF NOT EXISTS widget_type (
2020-06-22 11:10:28 +03:00
id uuid NOT NULL CONSTRAINT widget_type_pkey PRIMARY KEY,
created_time bigint NOT NULL,
2023-08-18 16:14:21 +03:00
fqn varchar(512),
2017-07-03 11:34:11 +03:00
descriptor varchar(1000000),
2017-07-17 17:32:32 +03:00
name varchar(255),
tenant_id uuid,
image varchar(1000000),
scada boolean NOT NULL DEFAULT false,
2023-08-18 16:14:21 +03:00
deprecated boolean NOT NULL DEFAULT false,
description varchar(1024),
tags text[],
external_id uuid,
2024-07-17 16:51:07 +03:00
version BIGINT DEFAULT 1,
CONSTRAINT uq_widget_type_fqn UNIQUE (tenant_id, fqn),
CONSTRAINT widget_type_external_id_unq_key UNIQUE (tenant_id, external_id)
);
CREATE TABLE IF NOT EXISTS widgets_bundle (
2020-06-22 11:10:28 +03:00
id uuid NOT NULL CONSTRAINT widgets_bundle_pkey PRIMARY KEY,
created_time bigint NOT NULL,
2017-07-17 17:32:32 +03:00
alias varchar(255),
2020-06-22 11:10:28 +03:00
tenant_id uuid,
title varchar(255),
image varchar(1000000),
scada boolean NOT NULL DEFAULT false,
description varchar(1024),
2023-10-31 10:30:55 +02:00
widgets_bundle_order int,
2022-07-15 18:00:33 +03:00
external_id uuid,
2024-07-17 16:51:07 +03:00
version BIGINT DEFAULT 1,
CONSTRAINT uq_widgets_bundle_alias UNIQUE (tenant_id, alias),
2022-07-15 18:00:33 +03:00
CONSTRAINT widgets_bundle_external_id_unq_key UNIQUE (tenant_id, external_id)
2018-03-12 12:28:16 +02:00
);
CREATE TABLE IF NOT EXISTS widgets_bundle_widget (
widgets_bundle_id uuid NOT NULL,
widget_type_id uuid NOT NULL,
widget_type_order int NOT NULL DEFAULT 0,
CONSTRAINT widgets_bundle_widget_pkey PRIMARY KEY (widgets_bundle_id, widget_type_id),
CONSTRAINT fk_widgets_bundle FOREIGN KEY (widgets_bundle_id) REFERENCES widgets_bundle(id) ON DELETE CASCADE,
CONSTRAINT fk_widget_type FOREIGN KEY (widget_type_id) REFERENCES widget_type(id) ON DELETE CASCADE
);
2018-10-09 18:51:10 +03:00
CREATE TABLE IF NOT EXISTS entity_view (
2020-06-22 11:10:28 +03:00
id uuid NOT NULL CONSTRAINT entity_view_pkey PRIMARY KEY,
created_time bigint NOT NULL,
entity_id uuid,
2018-09-05 19:29:42 +03:00
entity_type varchar(255),
2020-06-22 11:10:28 +03:00
tenant_id uuid,
customer_id uuid,
2018-10-09 18:51:10 +03:00
type varchar(255),
2018-09-05 19:29:42 +03:00
name varchar(255),
2018-11-09 12:49:51 +02:00
keys varchar(10000000),
start_ts bigint,
end_ts bigint,
2022-06-13 13:47:45 +03:00
additional_info varchar,
2022-07-15 18:00:33 +03:00
external_id uuid,
2024-07-17 16:51:07 +03:00
version BIGINT DEFAULT 1,
2022-07-15 18:00:33 +03:00
CONSTRAINT entity_view_external_id_unq_key UNIQUE (tenant_id, external_id)
);
2020-04-22 13:03:53 +03:00
2024-07-29 21:31:14 +02:00
CREATE SEQUENCE IF NOT EXISTS ts_kv_latest_version_seq cache 1;
2024-06-26 15:51:32 +02:00
2020-07-07 18:02:29 +03:00
CREATE TABLE IF NOT EXISTS ts_kv_latest
(
entity_id uuid NOT NULL,
key int NOT NULL,
ts bigint NOT NULL,
bool_v boolean,
str_v varchar(10000000),
long_v bigint,
dbl_v double precision,
json_v json,
version bigint default 0,
2020-07-07 18:02:29 +03:00
CONSTRAINT ts_kv_latest_pkey PRIMARY KEY (entity_id, key)
);
CREATE TABLE IF NOT EXISTS key_dictionary
2020-07-07 18:02:29 +03:00
(
key varchar(255) NOT NULL,
key_id serial UNIQUE,
CONSTRAINT key_dictionary_id_pkey PRIMARY KEY (key)
2020-07-07 18:02:29 +03:00
);
CREATE TABLE IF NOT EXISTS oauth2_client (
id uuid NOT NULL CONSTRAINT oauth2_client_pkey PRIMARY KEY,
created_time bigint NOT NULL,
tenant_id uuid NOT NULL,
title varchar(100) NOT NULL,
additional_info varchar,
client_id varchar(255),
2021-06-16 18:46:26 +03:00
client_secret varchar(2048),
authorization_uri varchar(255),
token_uri varchar(255),
scope varchar(255),
platforms varchar(255),
user_info_uri varchar(255),
user_name_attribute_name varchar(255),
jwk_set_uri varchar(255),
client_authentication_method varchar(255),
login_button_label varchar(255),
login_button_icon varchar(255),
allow_user_creation boolean,
activate_user boolean,
type varchar(31),
basic_email_attribute_key varchar(31),
basic_first_name_attribute_key varchar(31),
basic_last_name_attribute_key varchar(31),
basic_tenant_name_strategy varchar(31),
basic_tenant_name_pattern varchar(255),
basic_customer_name_pattern varchar(255),
basic_default_dashboard_name varchar(255),
basic_always_full_screen boolean,
custom_url varchar(255),
custom_username varchar(255),
custom_password varchar(255),
custom_send_token boolean
);
CREATE TABLE IF NOT EXISTS domain (
id uuid NOT NULL CONSTRAINT domain_pkey PRIMARY KEY,
created_time bigint NOT NULL,
tenant_id uuid NOT NULL,
2024-08-16 18:00:34 +03:00
name varchar(255) UNIQUE,
oauth2_enabled boolean,
edge_enabled boolean
);
CREATE TABLE IF NOT EXISTS mobile_app (
id uuid NOT NULL CONSTRAINT mobile_app_pkey PRIMARY KEY,
created_time bigint NOT NULL,
tenant_id uuid,
pkg_name varchar(255) UNIQUE,
app_secret varchar(2048),
platform_type varchar(32),
status varchar(32),
version_info varchar(16384),
store_info varchar(16384),
2024-10-17 16:30:16 +03:00
CONSTRAINT pkg_name_platform_unique UNIQUE (pkg_name, platform_type)
);
CREATE TABLE IF NOT EXISTS mobile_app_bundle (
2024-10-08 17:57:46 +03:00
id uuid NOT NULL CONSTRAINT mobile_app_bundle_pkey PRIMARY KEY,
created_time bigint NOT NULL,
tenant_id uuid,
title varchar(255),
description varchar(1024),
android_app_id uuid UNIQUE,
ios_app_id uuid UNIQUE,
layout_config varchar(16384),
oauth2_enabled boolean,
CONSTRAINT fk_android_app_id FOREIGN KEY (android_app_id) REFERENCES mobile_app(id),
CONSTRAINT fk_ios_app_id FOREIGN KEY (ios_app_id) REFERENCES mobile_app(id)
);
CREATE TABLE IF NOT EXISTS domain_oauth2_client (
domain_id uuid NOT NULL,
oauth2_client_id uuid NOT NULL,
CONSTRAINT fk_domain FOREIGN KEY (domain_id) REFERENCES domain(id) ON DELETE CASCADE,
CONSTRAINT fk_oauth2_client FOREIGN KEY (oauth2_client_id) REFERENCES oauth2_client(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS mobile_app_bundle_oauth2_client (
mobile_app_bundle_id uuid NOT NULL,
oauth2_client_id uuid NOT NULL,
CONSTRAINT fk_domain FOREIGN KEY (mobile_app_bundle_id) REFERENCES mobile_app_bundle(id) ON DELETE CASCADE,
CONSTRAINT fk_oauth2_client FOREIGN KEY (oauth2_client_id) REFERENCES oauth2_client(id) ON DELETE CASCADE
);
2020-08-12 17:31:34 +03:00
CREATE TABLE IF NOT EXISTS oauth2_client_registration_template (
id uuid NOT NULL CONSTRAINT oauth2_client_registration_template_pkey PRIMARY KEY,
created_time bigint NOT NULL,
additional_info varchar,
2020-08-12 17:31:34 +03:00
provider_id varchar(255),
authorization_uri varchar(255),
token_uri varchar(255),
scope varchar(255),
user_info_uri varchar(255),
user_name_attribute_name varchar(255),
jwk_set_uri varchar(255),
client_authentication_method varchar(255),
2020-10-08 18:22:16 +03:00
type varchar(31),
2020-08-12 17:31:34 +03:00
basic_email_attribute_key varchar(31),
basic_first_name_attribute_key varchar(31),
basic_last_name_attribute_key varchar(31),
basic_tenant_name_strategy varchar(31),
basic_tenant_name_pattern varchar(255),
basic_customer_name_pattern varchar(255),
basic_default_dashboard_name varchar(255),
basic_always_full_screen boolean,
comment varchar,
login_button_icon varchar(255),
login_button_label varchar(255),
help_link varchar(255),
2020-08-12 17:31:34 +03:00
CONSTRAINT oauth2_template_provider_id_unq_key UNIQUE (provider_id)
);
CREATE TABLE IF NOT EXISTS api_usage_state (
2020-10-19 15:39:15 +03:00
id uuid NOT NULL CONSTRAINT usage_record_pkey PRIMARY KEY,
created_time bigint NOT NULL,
tenant_id uuid,
entity_type varchar(32),
entity_id uuid,
2020-11-10 16:19:44 +02:00
transport varchar(32),
db_storage varchar(32),
re_exec varchar(32),
js_exec varchar(32),
2023-09-21 17:03:04 +03:00
tbel_exec varchar(32),
2020-11-20 18:02:09 +02:00
email_exec varchar(32),
sms_exec varchar(32),
2021-05-11 16:17:37 +03:00
alarm_exec varchar(32),
CONSTRAINT api_usage_state_unq_key UNIQUE (tenant_id, entity_id)
2020-10-19 15:39:15 +03:00
);
CREATE TABLE IF NOT EXISTS resource (
id uuid NOT NULL CONSTRAINT resource_pkey PRIMARY KEY,
created_time bigint NOT NULL,
tenant_id uuid NOT NULL,
title varchar(255) NOT NULL,
resource_type varchar(32) NOT NULL,
resource_sub_type varchar(32),
resource_key varchar(255) NOT NULL,
search_text varchar(255),
file_name varchar(255) NOT NULL,
data bytea,
2023-06-08 17:44:20 +03:00
etag varchar,
descriptor varchar,
preview bytea,
2023-12-14 16:24:36 +02:00
is_public boolean default true,
2023-12-14 17:57:41 +02:00
public_resource_key varchar(32) unique,
2023-11-16 15:13:00 +02:00
external_id uuid,
CONSTRAINT resource_unq_key UNIQUE (tenant_id, resource_type, resource_key)
);
2019-09-17 13:05:27 +03:00
CREATE TABLE IF NOT EXISTS edge (
id uuid NOT NULL CONSTRAINT edge_pkey PRIMARY KEY,
2020-10-12 16:54:05 +03:00
created_time bigint NOT NULL,
2019-09-17 13:05:27 +03:00
additional_info varchar,
customer_id uuid,
root_rule_chain_id uuid,
type varchar(255),
2019-09-17 13:05:27 +03:00
name varchar(255),
label varchar(255),
2019-10-28 12:13:01 +02:00
routing_key varchar(255),
secret varchar(255),
tenant_id uuid,
2024-07-17 16:51:07 +03:00
version BIGINT DEFAULT 1,
CONSTRAINT edge_name_unq_key UNIQUE (tenant_id, name),
CONSTRAINT edge_routing_key_unq_key UNIQUE (routing_key)
);
2020-06-16 16:49:27 +03:00
CREATE TABLE IF NOT EXISTS edge_event (
seq_id INT GENERATED ALWAYS AS IDENTITY,
2022-11-07 14:42:49 +02:00
id uuid NOT NULL,
2020-10-12 16:54:05 +03:00
created_time bigint NOT NULL,
edge_id uuid,
2020-06-16 16:49:27 +03:00
edge_event_type varchar(255),
2020-09-17 12:53:38 +03:00
edge_event_uid varchar(255),
entity_id uuid,
2020-06-16 16:49:27 +03:00
edge_event_action varchar(255),
2020-09-17 12:53:38 +03:00
body varchar(10000000),
tenant_id uuid,
2020-06-16 16:49:27 +03:00
ts bigint NOT NULL
2022-11-07 14:42:49 +02:00
) PARTITION BY RANGE(created_time);
ALTER TABLE IF EXISTS edge_event ALTER COLUMN seq_id SET CYCLE;
2021-06-11 11:10:06 +03:00
CREATE TABLE IF NOT EXISTS rpc (
id uuid NOT NULL CONSTRAINT rpc_pkey PRIMARY KEY,
created_time bigint NOT NULL,
tenant_id uuid NOT NULL,
device_id uuid NOT NULL,
expiration_time bigint NOT NULL,
request varchar(10000000) NOT NULL,
response varchar(10000000),
2021-08-04 18:03:54 +03:00
additional_info varchar(10000000),
2021-06-11 11:10:06 +03:00
status varchar(255) NOT NULL
);
2020-07-07 18:02:29 +03:00
CREATE OR REPLACE FUNCTION to_uuid(IN entity_id varchar, OUT uuid_id uuid) AS
$$
BEGIN
uuid_id := substring(entity_id, 8, 8) || '-' || substring(entity_id, 4, 4) || '-1' || substring(entity_id, 1, 3) ||
'-' || substring(entity_id, 16, 4) || '-' || substring(entity_id, 20, 12);
END;
$$ LANGUAGE plpgsql;
2020-08-12 16:31:45 +03:00
CREATE OR REPLACE PROCEDURE cleanup_edge_events_by_ttl(IN ttl bigint, INOUT deleted bigint)
LANGUAGE plpgsql AS
$$
DECLARE
ttl_ts bigint;
ttl_deleted_count bigint DEFAULT 0;
BEGIN
IF ttl > 0 THEN
ttl_ts := (EXTRACT(EPOCH FROM current_timestamp) * 1000 - ttl::bigint * 1000)::bigint;
EXECUTE format(
'WITH deleted AS (DELETE FROM edge_event WHERE ts < %L::bigint RETURNING *) SELECT count(*) FROM deleted', ttl_ts) into ttl_deleted_count;
END IF;
RAISE NOTICE 'Edge events removed by ttl: %', ttl_deleted_count;
deleted := ttl_deleted_count;
END
$$;
CREATE TABLE IF NOT EXISTS user_auth_settings (
id uuid NOT NULL CONSTRAINT user_auth_settings_pkey PRIMARY KEY,
created_time bigint NOT NULL,
user_id uuid UNIQUE NOT NULL CONSTRAINT fk_user_auth_settings_user_id REFERENCES tb_user(id),
two_fa_settings varchar
);
2022-10-31 14:04:29 +02:00
CREATE TABLE IF NOT EXISTS notification_target (
id UUID NOT NULL CONSTRAINT notification_target_pkey PRIMARY KEY,
created_time BIGINT NOT NULL,
tenant_id UUID NOT NULL,
2022-10-31 14:04:29 +02:00
name VARCHAR(255) NOT NULL,
configuration VARCHAR(10000) NOT NULL,
external_id UUID,
CONSTRAINT uq_notification_target_name UNIQUE (tenant_id, name),
CONSTRAINT uq_notification_target_external_id UNIQUE (tenant_id, external_id)
);
CREATE TABLE IF NOT EXISTS notification_template (
id UUID NOT NULL CONSTRAINT notification_template_pkey PRIMARY KEY,
created_time BIGINT NOT NULL,
tenant_id UUID NOT NULL,
name VARCHAR(255) NOT NULL,
notification_type VARCHAR(50) NOT NULL,
configuration VARCHAR(10000000) NOT NULL,
external_id UUID,
CONSTRAINT uq_notification_template_name UNIQUE (tenant_id, name),
CONSTRAINT uq_notification_template_external_id UNIQUE (tenant_id, external_id)
2022-10-31 14:04:29 +02:00
);
2022-11-06 18:51:49 +02:00
CREATE TABLE IF NOT EXISTS notification_rule (
id UUID NOT NULL CONSTRAINT notification_rule_pkey PRIMARY KEY,
created_time BIGINT NOT NULL,
tenant_id UUID NOT NULL,
name VARCHAR(255) NOT NULL,
2023-05-11 14:57:40 +03:00
enabled BOOLEAN NOT NULL DEFAULT true,
template_id UUID NOT NULL CONSTRAINT fk_notification_rule_template_id REFERENCES notification_template(id),
2023-01-17 16:30:47 +02:00
trigger_type VARCHAR(50) NOT NULL,
trigger_config VARCHAR(1000) NOT NULL,
recipients_config VARCHAR(10000) NOT NULL,
additional_config VARCHAR(255),
external_id UUID,
CONSTRAINT uq_notification_rule_name UNIQUE (tenant_id, name),
CONSTRAINT uq_notification_rule_external_id UNIQUE (tenant_id, external_id)
2022-11-06 18:51:49 +02:00
);
2022-10-31 14:04:29 +02:00
CREATE TABLE IF NOT EXISTS notification_request (
id UUID NOT NULL CONSTRAINT notification_request_pkey PRIMARY KEY,
created_time BIGINT NOT NULL,
tenant_id UUID NOT NULL,
targets VARCHAR(10000) NOT NULL,
template_id UUID,
template VARCHAR(10000000),
info VARCHAR(1000000),
additional_config VARCHAR(1000),
2022-11-07 13:50:16 +02:00
originator_entity_id UUID,
originator_entity_type VARCHAR(32),
rule_id UUID NULL,
2022-12-15 15:50:01 +02:00
status VARCHAR(32),
2023-01-06 17:50:34 +02:00
stats VARCHAR(10000)
2022-10-31 14:04:29 +02:00
);
CREATE TABLE IF NOT EXISTS notification (
id UUID NOT NULL,
created_time BIGINT NOT NULL,
request_id UUID,
recipient_id UUID NOT NULL,
type VARCHAR(50) NOT NULL,
delivery_method VARCHAR(50) NOT NULL,
2023-01-06 17:50:34 +02:00
subject VARCHAR(255),
2023-03-16 13:12:04 +02:00
body VARCHAR(1000) NOT NULL,
2023-01-23 11:48:44 +02:00
additional_config VARCHAR(1000),
2022-10-31 14:04:29 +02:00
status VARCHAR(32)
) PARTITION BY RANGE (created_time);
CREATE TABLE IF NOT EXISTS user_settings (
user_id uuid NOT NULL,
type VARCHAR(50) NOT NULL,
2023-12-28 13:03:19 +02:00
settings jsonb,
CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES tb_user(id) ON DELETE CASCADE,
CONSTRAINT user_settings_pkey PRIMARY KEY (user_id, type)
);
CREATE TABLE IF NOT EXISTS alarm_types (
tenant_id uuid NOT NULL,
type varchar(255) NOT NULL,
CONSTRAINT tenant_id_type_unq_key UNIQUE (tenant_id, type),
CONSTRAINT fk_entity_tenant_id FOREIGN KEY (tenant_id) REFERENCES tenant(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS queue_stats (
id uuid NOT NULL CONSTRAINT queue_stats_pkey PRIMARY KEY,
created_time bigint NOT NULL,
tenant_id uuid NOT NULL,
queue_name varchar(255) NOT NULL,
service_id varchar(255) NOT NULL,
CONSTRAINT queue_stats_name_unq_key UNIQUE (tenant_id, queue_name, service_id)
);
CREATE TABLE IF NOT EXISTS qr_code_settings (
id uuid NOT NULL CONSTRAINT mobile_app_settings_pkey PRIMARY KEY,
created_time bigint NOT NULL,
2024-05-07 12:09:36 +03:00
tenant_id uuid NOT NULL,
use_default_app boolean,
mobile_app_bundle_id uuid,
2024-05-07 12:09:36 +03:00
qr_code_config VARCHAR(100000),
2024-10-10 14:08:12 +03:00
CONSTRAINT qr_code_settings_tenant_id_unq_key UNIQUE (tenant_id)
2024-05-07 12:09:36 +03:00
);