Commit 699e94933d81d6f50f9f0cd5a177aaba77126389

Authored by 黄 x
1 parent d7be38d3

feat: 增加TB进行Install的数据库文件

  1 +--
  2 +-- Copyright © 2016-2021 The Thingsboard Authors
  3 +--
  4 +-- Licensed under the Apache License, Version 2.0 (the "License");
  5 +-- you may not use this file except in compliance with the License.
  6 +-- You may obtain a copy of the License at
  7 +--
  8 +-- http://www.apache.org/licenses/LICENSE-2.0
  9 +--
  10 +-- Unless required by applicable law or agreed to in writing, software
  11 +-- distributed under the License is distributed on an "AS IS" BASIS,
  12 +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 +-- See the License for the specific language governing permissions and
  14 +-- limitations under the License.
  15 +--
  16 +
  17 +SET DATABASE SQL SYNTAX PGS TRUE;
  18 +SET DATABASE TRANSACTION CONTROL MVCC;
  19 +
  20 +CREATE TABLE IF NOT EXISTS admin_settings (
  21 + id uuid NOT NULL CONSTRAINT admin_settings_pkey PRIMARY KEY,
  22 + created_time bigint NOT NULL,
  23 + json_value varchar,
  24 + key varchar(255)
  25 +);
  26 +
  27 +CREATE TABLE IF NOT EXISTS alarm (
  28 + id uuid NOT NULL CONSTRAINT alarm_pkey PRIMARY KEY,
  29 + created_time bigint NOT NULL,
  30 + ack_ts bigint,
  31 + clear_ts bigint,
  32 + additional_info varchar,
  33 + end_ts bigint,
  34 + originator_id uuid,
  35 + originator_type integer,
  36 + propagate boolean,
  37 + severity varchar(255),
  38 + start_ts bigint,
  39 + status varchar(255),
  40 + tenant_id uuid,
  41 + customer_id uuid,
  42 + propagate_relation_types varchar,
  43 + type varchar(255)
  44 +);
  45 +
  46 +CREATE TABLE IF NOT EXISTS asset (
  47 + id uuid NOT NULL CONSTRAINT asset_pkey PRIMARY KEY,
  48 + created_time bigint NOT NULL,
  49 + additional_info varchar,
  50 + customer_id uuid,
  51 + name varchar(255),
  52 + label varchar(255),
  53 + search_text varchar(255),
  54 + tenant_id uuid,
  55 + type varchar(255),
  56 + CONSTRAINT asset_name_unq_key UNIQUE (tenant_id, name)
  57 +);
  58 +
  59 +CREATE TABLE IF NOT EXISTS audit_log (
  60 + id uuid NOT NULL CONSTRAINT audit_log_pkey PRIMARY KEY,
  61 + created_time bigint NOT NULL,
  62 + tenant_id uuid,
  63 + customer_id uuid,
  64 + entity_id uuid,
  65 + entity_type varchar(255),
  66 + entity_name varchar(255),
  67 + user_id uuid,
  68 + user_name varchar(255),
  69 + action_type varchar(255),
  70 + action_data varchar(1000000),
  71 + action_status varchar(255),
  72 + action_failure_details varchar(1000000)
  73 +);
  74 +
  75 +CREATE TABLE IF NOT EXISTS attribute_kv (
  76 + entity_type varchar(255),
  77 + entity_id uuid,
  78 + attribute_type varchar(255),
  79 + attribute_key varchar(255),
  80 + bool_v boolean,
  81 + str_v varchar(10000000),
  82 + long_v bigint,
  83 + dbl_v double precision,
  84 + json_v varchar(10000000),
  85 + last_update_ts bigint,
  86 + CONSTRAINT attribute_kv_pkey PRIMARY KEY (entity_type, entity_id, attribute_type, attribute_key)
  87 +);
  88 +
  89 +CREATE TABLE IF NOT EXISTS component_descriptor (
  90 + id uuid NOT NULL CONSTRAINT component_descriptor_pkey PRIMARY KEY,
  91 + created_time bigint NOT NULL,
  92 + actions varchar(255),
  93 + clazz varchar UNIQUE,
  94 + configuration_descriptor varchar,
  95 + name varchar(255),
  96 + scope varchar(255),
  97 + search_text varchar(255),
  98 + type varchar(255)
  99 +);
  100 +
  101 +CREATE TABLE IF NOT EXISTS customer (
  102 + id uuid NOT NULL CONSTRAINT customer_pkey PRIMARY KEY,
  103 + created_time bigint NOT NULL,
  104 + additional_info varchar,
  105 + address varchar,
  106 + address2 varchar,
  107 + city varchar(255),
  108 + country varchar(255),
  109 + email varchar(255),
  110 + phone varchar(255),
  111 + search_text varchar(255),
  112 + state varchar(255),
  113 + tenant_id uuid,
  114 + title varchar(255),
  115 + zip varchar(255)
  116 +);
  117 +
  118 +CREATE TABLE IF NOT EXISTS dashboard (
  119 + id uuid NOT NULL CONSTRAINT dashboard_pkey PRIMARY KEY,
  120 + created_time bigint NOT NULL,
  121 + configuration varchar(10000000),
  122 + assigned_customers varchar(1000000),
  123 + search_text varchar(255),
  124 + tenant_id uuid,
  125 + title varchar(255),
  126 + mobile_hide boolean DEFAULT false,
  127 + mobile_order int,
  128 + image varchar(1000000)
  129 +);
  130 +
  131 +CREATE TABLE IF NOT EXISTS rule_chain (
  132 + id uuid NOT NULL CONSTRAINT rule_chain_pkey PRIMARY KEY,
  133 + created_time bigint NOT NULL,
  134 + additional_info varchar,
  135 + configuration varchar(10000000),
  136 + name varchar(255),
  137 + type varchar(255),
  138 + first_rule_node_id uuid,
  139 + root boolean,
  140 + debug_mode boolean,
  141 + search_text varchar(255),
  142 + tenant_id uuid
  143 +);
  144 +
  145 +CREATE TABLE IF NOT EXISTS rule_node (
  146 + id uuid NOT NULL CONSTRAINT rule_node_pkey PRIMARY KEY,
  147 + created_time bigint NOT NULL,
  148 + rule_chain_id uuid,
  149 + additional_info varchar,
  150 + configuration varchar(10000000),
  151 + type varchar(255),
  152 + name varchar(255),
  153 + debug_mode boolean,
  154 + search_text varchar(255)
  155 +);
  156 +
  157 +CREATE TABLE IF NOT EXISTS rule_node_state (
  158 + id uuid NOT NULL CONSTRAINT rule_node_state_pkey PRIMARY KEY,
  159 + created_time bigint NOT NULL,
  160 + rule_node_id uuid NOT NULL,
  161 + entity_type varchar(32) NOT NULL,
  162 + entity_id uuid NOT NULL,
  163 + state_data varchar(16384) NOT NULL,
  164 + CONSTRAINT rule_node_state_unq_key UNIQUE (rule_node_id, entity_id),
  165 + CONSTRAINT fk_rule_node_state_node_id FOREIGN KEY (rule_node_id) REFERENCES rule_node(id) ON DELETE CASCADE
  166 +);
  167 +
  168 +CREATE TABLE IF NOT EXISTS ota_package (
  169 + id uuid NOT NULL CONSTRAINT ota_package_pkey PRIMARY KEY,
  170 + created_time bigint NOT NULL,
  171 + tenant_id uuid NOT NULL,
  172 + device_profile_id uuid ,
  173 + type varchar(32) NOT NULL,
  174 + title varchar(255) NOT NULL,
  175 + version varchar(255) NOT NULL,
  176 + tag varchar(255),
  177 + url varchar(255),
  178 + file_name varchar(255),
  179 + content_type varchar(255),
  180 + checksum_algorithm varchar(32),
  181 + checksum varchar(1020),
  182 + data binary,
  183 + data_size bigint,
  184 + additional_info varchar,
  185 + search_text varchar(255),
  186 + CONSTRAINT ota_package_tenant_title_version_unq_key UNIQUE (tenant_id, title, version)
  187 +);
  188 +
  189 +CREATE TABLE IF NOT EXISTS device_profile (
  190 + id uuid NOT NULL CONSTRAINT device_profile_pkey PRIMARY KEY,
  191 + created_time bigint NOT NULL,
  192 + name varchar(255),
  193 + type varchar(255),
  194 + image varchar(1000000),
  195 + transport_type varchar(255),
  196 + provision_type varchar(255),
  197 + profile_data jsonb,
  198 + description varchar,
  199 + search_text varchar(255),
  200 + is_default boolean,
  201 + tenant_id uuid,
  202 + firmware_id uuid,
  203 + software_id uuid,
  204 + default_rule_chain_id uuid,
  205 + default_dashboard_id uuid,
  206 + default_queue_name varchar(255),
  207 + provision_device_key varchar,
  208 + CONSTRAINT device_profile_name_unq_key UNIQUE (tenant_id, name),
  209 + CONSTRAINT device_provision_key_unq_key UNIQUE (provision_device_key),
  210 + CONSTRAINT fk_default_rule_chain_device_profile FOREIGN KEY (default_rule_chain_id) REFERENCES rule_chain(id),
  211 + CONSTRAINT fk_default_dashboard_device_profile FOREIGN KEY (default_dashboard_id) REFERENCES dashboard(id),
  212 + CONSTRAINT fk_firmware_device_profile FOREIGN KEY (firmware_id) REFERENCES ota_package(id),
  213 + CONSTRAINT fk_software_device_profile FOREIGN KEY (software_id) REFERENCES ota_package(id)
  214 +);
  215 +
  216 +CREATE TABLE IF NOT EXISTS device (
  217 + id uuid NOT NULL CONSTRAINT device_pkey PRIMARY KEY,
  218 + created_time bigint NOT NULL,
  219 + additional_info varchar,
  220 + customer_id uuid,
  221 + device_profile_id uuid NOT NULL,
  222 + device_data jsonb,
  223 + type varchar(255),
  224 + name varchar(255),
  225 + label varchar(255),
  226 + search_text varchar(255),
  227 + tenant_id uuid,
  228 + firmware_id uuid,
  229 + software_id uuid,
  230 + CONSTRAINT device_name_unq_key UNIQUE (tenant_id, name),
  231 + CONSTRAINT fk_device_profile FOREIGN KEY (device_profile_id) REFERENCES device_profile(id),
  232 + CONSTRAINT fk_firmware_device FOREIGN KEY (firmware_id) REFERENCES ota_package(id),
  233 + CONSTRAINT fk_software_device FOREIGN KEY (software_id) REFERENCES ota_package(id)
  234 +);
  235 +
  236 +CREATE TABLE IF NOT EXISTS device_credentials (
  237 + id uuid NOT NULL CONSTRAINT device_credentials_pkey PRIMARY KEY,
  238 + created_time bigint NOT NULL,
  239 + credentials_id varchar,
  240 + credentials_type varchar(255),
  241 + credentials_value varchar,
  242 + device_id uuid,
  243 + CONSTRAINT device_credentials_id_unq_key UNIQUE (credentials_id),
  244 + CONSTRAINT device_credentials_device_id_unq_key UNIQUE (device_id)
  245 +);
  246 +
  247 +CREATE TABLE IF NOT EXISTS event (
  248 + id uuid NOT NULL CONSTRAINT event_pkey PRIMARY KEY,
  249 + created_time bigint NOT NULL,
  250 + body varchar(10000000),
  251 + entity_id uuid,
  252 + entity_type varchar(255),
  253 + event_type varchar(255),
  254 + event_uid varchar(255),
  255 + tenant_id uuid,
  256 + ts bigint NOT NULL,
  257 + CONSTRAINT event_unq_key UNIQUE (tenant_id, entity_type, entity_id, event_type, event_uid)
  258 +);
  259 +
  260 +CREATE TABLE IF NOT EXISTS relation (
  261 + from_id uuid,
  262 + from_type varchar(255),
  263 + to_id uuid,
  264 + to_type varchar(255),
  265 + relation_type_group varchar(255),
  266 + relation_type varchar(255),
  267 + additional_info varchar,
  268 + CONSTRAINT relation_pkey PRIMARY KEY (from_id, from_type, relation_type_group, relation_type, to_id, to_type)
  269 +);
  270 +
  271 +CREATE TABLE IF NOT EXISTS tb_user (
  272 + id uuid NOT NULL CONSTRAINT tb_user_pkey PRIMARY KEY,
  273 + created_time bigint NOT NULL,
  274 + additional_info varchar,
  275 + authority varchar(255),
  276 + customer_id uuid,
  277 + email varchar(255) UNIQUE,
  278 + first_name varchar(255),
  279 + last_name varchar(255),
  280 + search_text varchar(255),
  281 + tenant_id uuid
  282 +);
  283 +
  284 +CREATE TABLE IF NOT EXISTS tenant_profile (
  285 + id uuid NOT NULL CONSTRAINT tenant_profile_pkey PRIMARY KEY,
  286 + created_time bigint NOT NULL,
  287 + name varchar(255),
  288 + profile_data jsonb,
  289 + description varchar,
  290 + search_text varchar(255),
  291 + is_default boolean,
  292 + isolated_tb_core boolean,
  293 + isolated_tb_rule_engine boolean,
  294 + CONSTRAINT tenant_profile_name_unq_key UNIQUE (name)
  295 +);
  296 +
  297 +CREATE TABLE IF NOT EXISTS tenant (
  298 + id uuid NOT NULL CONSTRAINT tenant_pkey PRIMARY KEY,
  299 + created_time bigint NOT NULL,
  300 + additional_info varchar,
  301 + tenant_profile_id uuid NOT NULL,
  302 + address varchar,
  303 + address2 varchar,
  304 + city varchar(255),
  305 + country varchar(255),
  306 + email varchar(255),
  307 + phone varchar(255),
  308 + region varchar(255),
  309 + search_text varchar(255),
  310 + state varchar(255),
  311 + title varchar(255),
  312 + zip varchar(255),
  313 + CONSTRAINT fk_tenant_profile FOREIGN KEY (tenant_profile_id) REFERENCES tenant_profile(id)
  314 +);
  315 +
  316 +CREATE TABLE IF NOT EXISTS user_credentials (
  317 + id uuid NOT NULL CONSTRAINT user_credentials_pkey PRIMARY KEY,
  318 + created_time bigint NOT NULL,
  319 + activate_token varchar(255) UNIQUE,
  320 + enabled boolean,
  321 + password varchar(255),
  322 + reset_token varchar(255) UNIQUE,
  323 + user_id uuid UNIQUE
  324 +);
  325 +
  326 +CREATE TABLE IF NOT EXISTS widget_type (
  327 + id uuid NOT NULL CONSTRAINT widget_type_pkey PRIMARY KEY,
  328 + created_time bigint NOT NULL,
  329 + alias varchar(255),
  330 + bundle_alias varchar(255),
  331 + descriptor varchar(1000000),
  332 + name varchar(255),
  333 + tenant_id uuid,
  334 + image varchar(1000000),
  335 + description varchar(255)
  336 +);
  337 +
  338 +CREATE TABLE IF NOT EXISTS widgets_bundle (
  339 + id uuid NOT NULL CONSTRAINT widgets_bundle_pkey PRIMARY KEY,
  340 + created_time bigint NOT NULL,
  341 + alias varchar(255),
  342 + search_text varchar(255),
  343 + tenant_id uuid,
  344 + title varchar(255),
  345 + image varchar(1000000),
  346 + description varchar(255)
  347 +);
  348 +
  349 +CREATE TABLE IF NOT EXISTS entity_view (
  350 + id uuid NOT NULL CONSTRAINT entity_view_pkey PRIMARY KEY,
  351 + created_time bigint NOT NULL,
  352 + entity_id uuid,
  353 + entity_type varchar(255),
  354 + tenant_id uuid,
  355 + customer_id uuid,
  356 + type varchar(255),
  357 + name varchar(255),
  358 + keys varchar(10000000),
  359 + start_ts bigint,
  360 + end_ts bigint,
  361 + search_text varchar(255),
  362 + additional_info varchar
  363 +);
  364 +
  365 +CREATE TABLE IF NOT EXISTS ts_kv_latest (
  366 + entity_id uuid NOT NULL,
  367 + key int NOT NULL,
  368 + ts bigint NOT NULL,
  369 + bool_v boolean,
  370 + str_v varchar(10000000),
  371 + long_v bigint,
  372 + dbl_v double precision,
  373 + json_v varchar(10000000),
  374 + CONSTRAINT ts_kv_latest_pkey PRIMARY KEY (entity_id, key)
  375 +);
  376 +
  377 +CREATE TABLE IF NOT EXISTS ts_kv_dictionary (
  378 + key varchar(255) NOT NULL,
  379 + key_id int GENERATED BY DEFAULT AS IDENTITY(start with 0 increment by 1) UNIQUE,
  380 + CONSTRAINT ts_key_id_pkey PRIMARY KEY (key)
  381 +);
  382 +
  383 +CREATE TABLE IF NOT EXISTS oauth2_params (
  384 + id uuid NOT NULL CONSTRAINT oauth2_params_pkey PRIMARY KEY,
  385 + enabled boolean,
  386 + tenant_id uuid,
  387 + created_time bigint NOT NULL
  388 +);
  389 +
  390 +CREATE TABLE IF NOT EXISTS oauth2_registration (
  391 + id uuid NOT NULL CONSTRAINT oauth2_registration_pkey PRIMARY KEY,
  392 + oauth2_params_id uuid NOT NULL,
  393 + created_time bigint NOT NULL,
  394 + additional_info varchar,
  395 + client_id varchar(255),
  396 + client_secret varchar(2048),
  397 + authorization_uri varchar(255),
  398 + token_uri varchar(255),
  399 + scope varchar(255),
  400 + platforms varchar(255),
  401 + user_info_uri varchar(255),
  402 + user_name_attribute_name varchar(255),
  403 + jwk_set_uri varchar(255),
  404 + client_authentication_method varchar(255),
  405 + login_button_label varchar(255),
  406 + login_button_icon varchar(255),
  407 + allow_user_creation boolean,
  408 + activate_user boolean,
  409 + type varchar(31),
  410 + basic_email_attribute_key varchar(31),
  411 + basic_first_name_attribute_key varchar(31),
  412 + basic_last_name_attribute_key varchar(31),
  413 + basic_tenant_name_strategy varchar(31),
  414 + basic_tenant_name_pattern varchar(255),
  415 + basic_customer_name_pattern varchar(255),
  416 + basic_default_dashboard_name varchar(255),
  417 + basic_always_full_screen boolean,
  418 + custom_url varchar(255),
  419 + custom_username varchar(255),
  420 + custom_password varchar(255),
  421 + custom_send_token boolean,
  422 + CONSTRAINT fk_registration_oauth2_params FOREIGN KEY (oauth2_params_id) REFERENCES oauth2_params(id) ON DELETE CASCADE
  423 +);
  424 +
  425 +CREATE TABLE IF NOT EXISTS oauth2_domain (
  426 + id uuid NOT NULL CONSTRAINT oauth2_domain_pkey PRIMARY KEY,
  427 + oauth2_params_id uuid NOT NULL,
  428 + created_time bigint NOT NULL,
  429 + domain_name varchar(255),
  430 + domain_scheme varchar(31),
  431 + CONSTRAINT fk_domain_oauth2_params FOREIGN KEY (oauth2_params_id) REFERENCES oauth2_params(id) ON DELETE CASCADE,
  432 + CONSTRAINT oauth2_domain_unq_key UNIQUE (oauth2_params_id, domain_name, domain_scheme)
  433 +);
  434 +
  435 +CREATE TABLE IF NOT EXISTS oauth2_mobile (
  436 + id uuid NOT NULL CONSTRAINT oauth2_mobile_pkey PRIMARY KEY,
  437 + oauth2_params_id uuid NOT NULL,
  438 + created_time bigint NOT NULL,
  439 + pkg_name varchar(255),
  440 + app_secret varchar(2048),
  441 + CONSTRAINT fk_mobile_oauth2_params FOREIGN KEY (oauth2_params_id) REFERENCES oauth2_params(id) ON DELETE CASCADE,
  442 + CONSTRAINT oauth2_mobile_unq_key UNIQUE (oauth2_params_id, pkg_name)
  443 +);
  444 +
  445 +CREATE TABLE IF NOT EXISTS oauth2_client_registration_template (
  446 + id uuid NOT NULL CONSTRAINT oauth2_client_registration_template_pkey PRIMARY KEY,
  447 + created_time bigint NOT NULL,
  448 + additional_info varchar,
  449 + provider_id varchar(255),
  450 + authorization_uri varchar(255),
  451 + token_uri varchar(255),
  452 + scope varchar(255),
  453 + user_info_uri varchar(255),
  454 + user_name_attribute_name varchar(255),
  455 + jwk_set_uri varchar(255),
  456 + client_authentication_method varchar(255),
  457 + type varchar(31),
  458 + basic_email_attribute_key varchar(31),
  459 + basic_first_name_attribute_key varchar(31),
  460 + basic_last_name_attribute_key varchar(31),
  461 + basic_tenant_name_strategy varchar(31),
  462 + basic_tenant_name_pattern varchar(255),
  463 + basic_customer_name_pattern varchar(255),
  464 + basic_default_dashboard_name varchar(255),
  465 + basic_always_full_screen boolean,
  466 + comment varchar,
  467 + login_button_icon varchar(255),
  468 + login_button_label varchar(255),
  469 + help_link varchar(255),
  470 + CONSTRAINT oauth2_template_provider_id_unq_key UNIQUE (provider_id)
  471 +);
  472 +
  473 +-- Deprecated
  474 +CREATE TABLE IF NOT EXISTS oauth2_client_registration_info (
  475 + id uuid NOT NULL CONSTRAINT oauth2_client_registration_info_pkey PRIMARY KEY,
  476 + enabled boolean,
  477 + created_time bigint NOT NULL,
  478 + additional_info varchar,
  479 + client_id varchar(255),
  480 + client_secret varchar(255),
  481 + authorization_uri varchar(255),
  482 + token_uri varchar(255),
  483 + scope varchar(255),
  484 + user_info_uri varchar(255),
  485 + user_name_attribute_name varchar(255),
  486 + jwk_set_uri varchar(255),
  487 + client_authentication_method varchar(255),
  488 + login_button_label varchar(255),
  489 + login_button_icon varchar(255),
  490 + allow_user_creation boolean,
  491 + activate_user boolean,
  492 + type varchar(31),
  493 + basic_email_attribute_key varchar(31),
  494 + basic_first_name_attribute_key varchar(31),
  495 + basic_last_name_attribute_key varchar(31),
  496 + basic_tenant_name_strategy varchar(31),
  497 + basic_tenant_name_pattern varchar(255),
  498 + basic_customer_name_pattern varchar(255),
  499 + basic_default_dashboard_name varchar(255),
  500 + basic_always_full_screen boolean,
  501 + custom_url varchar(255),
  502 + custom_username varchar(255),
  503 + custom_password varchar(255),
  504 + custom_send_token boolean
  505 +);
  506 +
  507 +-- Deprecated
  508 +CREATE TABLE IF NOT EXISTS oauth2_client_registration (
  509 + id uuid NOT NULL CONSTRAINT oauth2_client_registration_pkey PRIMARY KEY,
  510 + created_time bigint NOT NULL,
  511 + domain_name varchar(255),
  512 + domain_scheme varchar(31),
  513 + client_registration_info_id uuid
  514 +);
  515 +
  516 +CREATE TABLE IF NOT EXISTS api_usage_state (
  517 + id uuid NOT NULL CONSTRAINT usage_record_pkey PRIMARY KEY,
  518 + created_time bigint NOT NULL,
  519 + tenant_id uuid,
  520 + entity_type varchar(32),
  521 + entity_id uuid,
  522 + transport varchar(32),
  523 + db_storage varchar(32),
  524 + re_exec varchar(32),
  525 + js_exec varchar(32),
  526 + email_exec varchar(32),
  527 + sms_exec varchar(32),
  528 + alarm_exec varchar(32),
  529 + CONSTRAINT api_usage_state_unq_key UNIQUE (tenant_id, entity_id)
  530 +);
  531 +
  532 +CREATE TABLE IF NOT EXISTS resource (
  533 + id uuid NOT NULL CONSTRAINT resource_pkey PRIMARY KEY,
  534 + created_time bigint NOT NULL,
  535 + tenant_id uuid NOT NULL,
  536 + title varchar(255) NOT NULL,
  537 + resource_type varchar(32) NOT NULL,
  538 + resource_key varchar(255) NOT NULL,
  539 + search_text varchar(255),
  540 + file_name varchar(255) NOT NULL,
  541 + data varchar,
  542 + CONSTRAINT resource_unq_key UNIQUE (tenant_id, resource_type, resource_key)
  543 +);
  544 +
  545 +CREATE TABLE IF NOT EXISTS edge (
  546 + id uuid NOT NULL CONSTRAINT edge_pkey PRIMARY KEY,
  547 + created_time bigint NOT NULL,
  548 + additional_info varchar,
  549 + customer_id uuid,
  550 + root_rule_chain_id uuid,
  551 + type varchar(255),
  552 + name varchar(255),
  553 + label varchar(255),
  554 + routing_key varchar(255),
  555 + secret varchar(255),
  556 + edge_license_key varchar(30),
  557 + cloud_endpoint varchar(255),
  558 + search_text varchar(255),
  559 + tenant_id uuid,
  560 + CONSTRAINT edge_name_unq_key UNIQUE (tenant_id, name),
  561 + CONSTRAINT edge_routing_key_unq_key UNIQUE (routing_key)
  562 +);
  563 +
  564 +CREATE TABLE IF NOT EXISTS edge_event (
  565 + id uuid NOT NULL CONSTRAINT edge_event_pkey PRIMARY KEY,
  566 + created_time bigint NOT NULL,
  567 + edge_id uuid,
  568 + edge_event_type varchar(255),
  569 + edge_event_uid varchar(255),
  570 + entity_id uuid,
  571 + edge_event_action varchar(255),
  572 + body varchar(10000000),
  573 + tenant_id uuid,
  574 + ts bigint NOT NULL
  575 +);
  576 +
  577 +CREATE TABLE IF NOT EXISTS rpc (
  578 + id uuid NOT NULL CONSTRAINT rpc_pkey PRIMARY KEY,
  579 + created_time bigint NOT NULL,
  580 + tenant_id uuid NOT NULL,
  581 + device_id uuid NOT NULL,
  582 + expiration_time bigint NOT NULL,
  583 + request varchar(10000000) NOT NULL,
  584 + response varchar(10000000),
  585 + additional_info varchar(10000000),
  586 + status varchar(255) NOT NULL
  587 +);
... ...
  1 +--
  2 +-- Copyright © 2016-2021 The Thingsboard Authors
  3 +--
  4 +-- Licensed under the Apache License, Version 2.0 (the "License");
  5 +-- you may not use this file except in compliance with the License.
  6 +-- You may obtain a copy of the License at
  7 +--
  8 +-- http://www.apache.org/licenses/LICENSE-2.0
  9 +--
  10 +-- Unless required by applicable law or agreed to in writing, software
  11 +-- distributed under the License is distributed on an "AS IS" BASIS,
  12 +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 +-- See the License for the specific language governing permissions and
  14 +-- limitations under the License.
  15 +--
  16 +
  17 +-- This file describes PostgreSQL-specific indexes that not supported by hsql
  18 +-- It is not a stand-alone file! Run schema-entities-idx.sql before!
  19 +-- Note: Hibernate DESC order translates to native SQL "ORDER BY .. DESC NULLS LAST"
  20 +-- While creating index PostgreSQL transforms short notation (ts DESC) to the full (DESC NULLS FIRST)
  21 +-- That difference between NULLS LAST and NULLS FIRST prevents to hit index while querying latest by ts
  22 +-- That why we need to define DESC index explicitly as (ts DESC NULLS LAST)
  23 +
  24 +CREATE INDEX IF NOT EXISTS idx_event_ts
  25 + ON public.event
  26 + (ts DESC NULLS LAST)
  27 + WITH (FILLFACTOR=95);
  28 +
  29 +COMMENT ON INDEX public.idx_event_ts
  30 + IS 'This index helps to delete events by TTL using timestamp';
  31 +
  32 +CREATE INDEX IF NOT EXISTS idx_event_tenant_entity_type_entity_event_type_created_time_des
  33 + ON public.event
  34 + (tenant_id ASC, entity_type ASC, entity_id ASC, event_type ASC, created_time DESC NULLS LAST)
  35 + WITH (FILLFACTOR=95);
  36 +
  37 +COMMENT ON INDEX public.idx_event_tenant_entity_type_entity_event_type_created_time_des
  38 + IS 'This index helps to open latest events on UI fast';
... ...
  1 +--
  2 +-- Copyright © 2016-2021 The Thingsboard Authors
  3 +--
  4 +-- Licensed under the Apache License, Version 2.0 (the "License");
  5 +-- you may not use this file except in compliance with the License.
  6 +-- You may obtain a copy of the License at
  7 +--
  8 +-- http://www.apache.org/licenses/LICENSE-2.0
  9 +--
  10 +-- Unless required by applicable law or agreed to in writing, software
  11 +-- distributed under the License is distributed on an "AS IS" BASIS,
  12 +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 +-- See the License for the specific language governing permissions and
  14 +-- limitations under the License.
  15 +--
  16 +
  17 +CREATE INDEX IF NOT EXISTS idx_alarm_originator_alarm_type ON alarm(originator_id, type, start_ts DESC);
  18 +
  19 +CREATE INDEX IF NOT EXISTS idx_alarm_originator_created_time ON alarm(originator_id, created_time DESC);
  20 +
  21 +CREATE INDEX IF NOT EXISTS idx_alarm_tenant_created_time ON alarm(tenant_id, created_time DESC);
  22 +
  23 +CREATE INDEX IF NOT EXISTS idx_alarm_tenant_alarm_type_created_time ON alarm(tenant_id, type, created_time DESC);
  24 +
  25 +CREATE INDEX IF NOT EXISTS idx_relation_to_id ON relation(relation_type_group, to_type, to_id);
  26 +
  27 +CREATE INDEX IF NOT EXISTS idx_relation_from_id ON relation(relation_type_group, from_type, from_id);
  28 +
  29 +CREATE INDEX IF NOT EXISTS idx_device_customer_id ON device(tenant_id, customer_id);
  30 +
  31 +CREATE INDEX IF NOT EXISTS idx_device_customer_id_and_type ON device(tenant_id, customer_id, type);
  32 +
  33 +CREATE INDEX IF NOT EXISTS idx_device_type ON device(tenant_id, type);
  34 +
  35 +CREATE INDEX IF NOT EXISTS idx_device_device_profile_id ON device(tenant_id, device_profile_id);
  36 +
  37 +CREATE INDEX IF NOT EXISTS idx_asset_customer_id ON asset(tenant_id, customer_id);
  38 +
  39 +CREATE INDEX IF NOT EXISTS idx_asset_customer_id_and_type ON asset(tenant_id, customer_id, type);
  40 +
  41 +CREATE INDEX IF NOT EXISTS idx_asset_type ON asset(tenant_id, type);
  42 +
  43 +CREATE INDEX IF NOT EXISTS idx_attribute_kv_by_key_and_last_update_ts ON attribute_kv(entity_id, attribute_key, last_update_ts desc);
  44 +
  45 +CREATE INDEX IF NOT EXISTS idx_audit_log_tenant_id_and_created_time ON audit_log(tenant_id, created_time);
  46 +
  47 +CREATE INDEX IF NOT EXISTS idx_rpc_tenant_id_device_id ON rpc(tenant_id, device_id);
... ...
  1 +--
  2 +-- Copyright © 2016-2021 The Thingsboard Authors
  3 +--
  4 +-- Licensed under the Apache License, Version 2.0 (the "License");
  5 +-- you may not use this file except in compliance with the License.
  6 +-- You may obtain a copy of the License at
  7 +--
  8 +-- http://www.apache.org/licenses/LICENSE-2.0
  9 +--
  10 +-- Unless required by applicable law or agreed to in writing, software
  11 +-- distributed under the License is distributed on an "AS IS" BASIS,
  12 +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 +-- See the License for the specific language governing permissions and
  14 +-- limitations under the License.
  15 +--
  16 +
  17 +CREATE TABLE IF NOT EXISTS tb_schema_settings
  18 +(
  19 + schema_version bigint NOT NULL,
  20 + CONSTRAINT tb_schema_settings_pkey PRIMARY KEY (schema_version)
  21 +);
  22 +
  23 +CREATE OR REPLACE PROCEDURE insert_tb_schema_settings()
  24 + LANGUAGE plpgsql AS
  25 +$$
  26 +BEGIN
  27 + IF (SELECT COUNT(*) FROM tb_schema_settings) = 0 THEN
  28 + INSERT INTO tb_schema_settings (schema_version) VALUES (3003000);
  29 + END IF;
  30 +END;
  31 +$$;
  32 +
  33 +call insert_tb_schema_settings();
  34 +
  35 +CREATE TABLE IF NOT EXISTS admin_settings (
  36 + id uuid NOT NULL CONSTRAINT admin_settings_pkey PRIMARY KEY,
  37 + created_time bigint NOT NULL,
  38 + json_value varchar,
  39 + key varchar(255)
  40 +);
  41 +
  42 +CREATE TABLE IF NOT EXISTS alarm (
  43 + id uuid NOT NULL CONSTRAINT alarm_pkey PRIMARY KEY,
  44 + created_time bigint NOT NULL,
  45 + ack_ts bigint,
  46 + clear_ts bigint,
  47 + additional_info varchar,
  48 + end_ts bigint,
  49 + originator_id uuid,
  50 + originator_type integer,
  51 + propagate boolean,
  52 + severity varchar(255),
  53 + start_ts bigint,
  54 + status varchar(255),
  55 + tenant_id uuid,
  56 + customer_id uuid,
  57 + propagate_relation_types varchar,
  58 + type varchar(255)
  59 +);
  60 +
  61 +CREATE TABLE IF NOT EXISTS asset (
  62 + id uuid NOT NULL CONSTRAINT asset_pkey PRIMARY KEY,
  63 + created_time bigint NOT NULL,
  64 + additional_info varchar,
  65 + customer_id uuid,
  66 + name varchar(255),
  67 + label varchar(255),
  68 + search_text varchar(255),
  69 + tenant_id uuid,
  70 + type varchar(255),
  71 + CONSTRAINT asset_name_unq_key UNIQUE (tenant_id, name)
  72 +);
  73 +
  74 +CREATE TABLE IF NOT EXISTS audit_log (
  75 + id uuid NOT NULL CONSTRAINT audit_log_pkey PRIMARY KEY,
  76 + created_time bigint NOT NULL,
  77 + tenant_id uuid,
  78 + customer_id uuid,
  79 + entity_id uuid,
  80 + entity_type varchar(255),
  81 + entity_name varchar(255),
  82 + user_id uuid,
  83 + user_name varchar(255),
  84 + action_type varchar(255),
  85 + action_data varchar(1000000),
  86 + action_status varchar(255),
  87 + action_failure_details varchar(1000000)
  88 +);
  89 +
  90 +CREATE TABLE IF NOT EXISTS attribute_kv (
  91 + entity_type varchar(255),
  92 + entity_id uuid,
  93 + attribute_type varchar(255),
  94 + attribute_key varchar(255),
  95 + bool_v boolean,
  96 + str_v varchar(10000000),
  97 + long_v bigint,
  98 + dbl_v double precision,
  99 + json_v json,
  100 + last_update_ts bigint,
  101 + CONSTRAINT attribute_kv_pkey PRIMARY KEY (entity_type, entity_id, attribute_type, attribute_key)
  102 +);
  103 +
  104 +CREATE TABLE IF NOT EXISTS component_descriptor (
  105 + id uuid NOT NULL CONSTRAINT component_descriptor_pkey PRIMARY KEY,
  106 + created_time bigint NOT NULL,
  107 + actions varchar(255),
  108 + clazz varchar UNIQUE,
  109 + configuration_descriptor varchar,
  110 + name varchar(255),
  111 + scope varchar(255),
  112 + search_text varchar(255),
  113 + type varchar(255)
  114 +);
  115 +
  116 +CREATE TABLE IF NOT EXISTS customer (
  117 + id uuid NOT NULL CONSTRAINT customer_pkey PRIMARY KEY,
  118 + created_time bigint NOT NULL,
  119 + additional_info varchar,
  120 + address varchar,
  121 + address2 varchar,
  122 + city varchar(255),
  123 + country varchar(255),
  124 + email varchar(255),
  125 + phone varchar(255),
  126 + search_text varchar(255),
  127 + state varchar(255),
  128 + tenant_id uuid,
  129 + title varchar(255),
  130 + zip varchar(255)
  131 +);
  132 +
  133 +CREATE TABLE IF NOT EXISTS dashboard (
  134 + id uuid NOT NULL CONSTRAINT dashboard_pkey PRIMARY KEY,
  135 + created_time bigint NOT NULL,
  136 + configuration varchar,
  137 + assigned_customers varchar(1000000),
  138 + search_text varchar(255),
  139 + tenant_id uuid,
  140 + title varchar(255),
  141 + mobile_hide boolean DEFAULT false,
  142 + mobile_order int,
  143 + image varchar(1000000)
  144 +);
  145 +
  146 +CREATE TABLE IF NOT EXISTS rule_chain (
  147 + id uuid NOT NULL CONSTRAINT rule_chain_pkey PRIMARY KEY,
  148 + created_time bigint NOT NULL,
  149 + additional_info varchar,
  150 + configuration varchar(10000000),
  151 + name varchar(255),
  152 + type varchar(255),
  153 + first_rule_node_id uuid,
  154 + root boolean,
  155 + debug_mode boolean,
  156 + search_text varchar(255),
  157 + tenant_id uuid
  158 +);
  159 +
  160 +CREATE TABLE IF NOT EXISTS rule_node (
  161 + id uuid NOT NULL CONSTRAINT rule_node_pkey PRIMARY KEY,
  162 + created_time bigint NOT NULL,
  163 + rule_chain_id uuid,
  164 + additional_info varchar,
  165 + configuration varchar(10000000),
  166 + type varchar(255),
  167 + name varchar(255),
  168 + debug_mode boolean,
  169 + search_text varchar(255)
  170 +);
  171 +
  172 +CREATE TABLE IF NOT EXISTS rule_node_state (
  173 + id uuid NOT NULL CONSTRAINT rule_node_state_pkey PRIMARY KEY,
  174 + created_time bigint NOT NULL,
  175 + rule_node_id uuid NOT NULL,
  176 + entity_type varchar(32) NOT NULL,
  177 + entity_id uuid NOT NULL,
  178 + state_data varchar(16384) NOT NULL,
  179 + CONSTRAINT rule_node_state_unq_key UNIQUE (rule_node_id, entity_id),
  180 + CONSTRAINT fk_rule_node_state_node_id FOREIGN KEY (rule_node_id) REFERENCES rule_node(id) ON DELETE CASCADE
  181 +);
  182 +
  183 +CREATE TABLE IF NOT EXISTS ota_package (
  184 + id uuid NOT NULL CONSTRAINT ota_package_pkey PRIMARY KEY,
  185 + created_time bigint NOT NULL,
  186 + tenant_id uuid NOT NULL,
  187 + device_profile_id uuid ,
  188 + type varchar(32) NOT NULL,
  189 + title varchar(255) NOT NULL,
  190 + version varchar(255) NOT NULL,
  191 + tag varchar(255),
  192 + url varchar(255),
  193 + file_name varchar(255),
  194 + content_type varchar(255),
  195 + checksum_algorithm varchar(32),
  196 + checksum varchar(1020),
  197 + data oid,
  198 + data_size bigint,
  199 + additional_info varchar,
  200 + search_text varchar(255),
  201 + CONSTRAINT ota_package_tenant_title_version_unq_key UNIQUE (tenant_id, title, version)
  202 +-- CONSTRAINT fk_device_profile_firmware FOREIGN KEY (device_profile_id) REFERENCES device_profile(id) ON DELETE CASCADE
  203 +);
  204 +
  205 +CREATE TABLE IF NOT EXISTS device_profile (
  206 + id uuid NOT NULL CONSTRAINT device_profile_pkey PRIMARY KEY,
  207 + created_time bigint NOT NULL,
  208 + name varchar(255),
  209 + type varchar(255),
  210 + image varchar(1000000),
  211 + transport_type varchar(255),
  212 + provision_type varchar(255),
  213 + profile_data jsonb,
  214 + description varchar,
  215 + search_text varchar(255),
  216 + is_default boolean,
  217 + tenant_id uuid,
  218 + firmware_id uuid,
  219 + software_id uuid,
  220 + default_rule_chain_id uuid,
  221 + default_dashboard_id uuid,
  222 + default_queue_name varchar(255),
  223 + provision_device_key varchar,
  224 + CONSTRAINT device_profile_name_unq_key UNIQUE (tenant_id, name),
  225 + CONSTRAINT device_provision_key_unq_key UNIQUE (provision_device_key),
  226 + CONSTRAINT fk_default_rule_chain_device_profile FOREIGN KEY (default_rule_chain_id) REFERENCES rule_chain(id),
  227 + CONSTRAINT fk_default_dashboard_device_profile FOREIGN KEY (default_dashboard_id) REFERENCES dashboard(id),
  228 + CONSTRAINT fk_firmware_device_profile FOREIGN KEY (firmware_id) REFERENCES ota_package(id),
  229 + CONSTRAINT fk_software_device_profile FOREIGN KEY (software_id) REFERENCES ota_package(id)
  230 +);
  231 +
  232 +-- We will use one-to-many relation in the first release and extend this feature in case of user requests
  233 +-- CREATE TABLE IF NOT EXISTS device_profile_firmware (
  234 +-- device_profile_id uuid NOT NULL,
  235 +-- firmware_id uuid NOT NULL,
  236 +-- CONSTRAINT device_profile_firmware_unq_key UNIQUE (device_profile_id, firmware_id),
  237 +-- CONSTRAINT fk_device_profile_firmware_device_profile FOREIGN KEY (device_profile_id) REFERENCES device_profile(id) ON DELETE CASCADE,
  238 +-- CONSTRAINT fk_device_profile_firmware_firmware FOREIGN KEY (firmware_id) REFERENCES firmware(id) ON DELETE CASCADE,
  239 +-- );
  240 +
  241 +CREATE TABLE IF NOT EXISTS device (
  242 + id uuid NOT NULL CONSTRAINT device_pkey PRIMARY KEY,
  243 + created_time bigint NOT NULL,
  244 + additional_info varchar,
  245 + customer_id uuid,
  246 + device_profile_id uuid NOT NULL,
  247 + device_data jsonb,
  248 + type varchar(255),
  249 + name varchar(255),
  250 + label varchar(255),
  251 + search_text varchar(255),
  252 + tenant_id uuid,
  253 + firmware_id uuid,
  254 + software_id uuid,
  255 + CONSTRAINT device_name_unq_key UNIQUE (tenant_id, name),
  256 + CONSTRAINT fk_device_profile FOREIGN KEY (device_profile_id) REFERENCES device_profile(id),
  257 + CONSTRAINT fk_firmware_device FOREIGN KEY (firmware_id) REFERENCES ota_package(id),
  258 + CONSTRAINT fk_software_device FOREIGN KEY (software_id) REFERENCES ota_package(id)
  259 +);
  260 +
  261 +CREATE TABLE IF NOT EXISTS device_credentials (
  262 + id uuid NOT NULL CONSTRAINT device_credentials_pkey PRIMARY KEY,
  263 + created_time bigint NOT NULL,
  264 + credentials_id varchar,
  265 + credentials_type varchar(255),
  266 + credentials_value varchar,
  267 + device_id uuid,
  268 + CONSTRAINT device_credentials_id_unq_key UNIQUE (credentials_id),
  269 + CONSTRAINT device_credentials_device_id_unq_key UNIQUE (device_id)
  270 +);
  271 +
  272 +CREATE TABLE IF NOT EXISTS event (
  273 + id uuid NOT NULL CONSTRAINT event_pkey PRIMARY KEY,
  274 + created_time bigint NOT NULL,
  275 + body varchar(10000000),
  276 + entity_id uuid,
  277 + entity_type varchar(255),
  278 + event_type varchar(255),
  279 + event_uid varchar(255),
  280 + tenant_id uuid,
  281 + ts bigint NOT NULL,
  282 + CONSTRAINT event_unq_key UNIQUE (tenant_id, entity_type, entity_id, event_type, event_uid)
  283 +);
  284 +
  285 +CREATE TABLE IF NOT EXISTS relation (
  286 + from_id uuid,
  287 + from_type varchar(255),
  288 + to_id uuid,
  289 + to_type varchar(255),
  290 + relation_type_group varchar(255),
  291 + relation_type varchar(255),
  292 + additional_info varchar,
  293 + CONSTRAINT relation_pkey PRIMARY KEY (from_id, from_type, relation_type_group, relation_type, to_id, to_type)
  294 +);
  295 +-- ) PARTITION BY LIST (relation_type_group);
  296 +--
  297 +-- CREATE TABLE other_relations PARTITION OF relation DEFAULT;
  298 +-- CREATE TABLE common_relations PARTITION OF relation FOR VALUES IN ('COMMON');
  299 +-- CREATE TABLE alarm_relations PARTITION OF relation FOR VALUES IN ('ALARM');
  300 +-- CREATE TABLE dashboard_relations PARTITION OF relation FOR VALUES IN ('DASHBOARD');
  301 +-- CREATE TABLE rule_relations PARTITION OF relation FOR VALUES IN ('RULE_CHAIN', 'RULE_NODE');
  302 +
  303 +CREATE TABLE IF NOT EXISTS tb_user (
  304 + id uuid NOT NULL CONSTRAINT tb_user_pkey PRIMARY KEY,
  305 + created_time bigint NOT NULL,
  306 + additional_info varchar,
  307 + authority varchar(255),
  308 + customer_id uuid,
  309 + email varchar(255) UNIQUE,
  310 + first_name varchar(255),
  311 + last_name varchar(255),
  312 + search_text varchar(255),
  313 + tenant_id uuid
  314 +);
  315 +
  316 +CREATE TABLE IF NOT EXISTS tenant_profile (
  317 + id uuid NOT NULL CONSTRAINT tenant_profile_pkey PRIMARY KEY,
  318 + created_time bigint NOT NULL,
  319 + name varchar(255),
  320 + profile_data jsonb,
  321 + description varchar,
  322 + search_text varchar(255),
  323 + is_default boolean,
  324 + isolated_tb_core boolean,
  325 + isolated_tb_rule_engine boolean,
  326 + CONSTRAINT tenant_profile_name_unq_key UNIQUE (name)
  327 +);
  328 +
  329 +CREATE TABLE IF NOT EXISTS tenant (
  330 + id uuid NOT NULL CONSTRAINT tenant_pkey PRIMARY KEY,
  331 + created_time bigint NOT NULL,
  332 + additional_info varchar,
  333 + tenant_profile_id uuid NOT NULL,
  334 + address varchar,
  335 + address2 varchar,
  336 + city varchar(255),
  337 + country varchar(255),
  338 + email varchar(255),
  339 + phone varchar(255),
  340 + region varchar(255),
  341 + search_text varchar(255),
  342 + state varchar(255),
  343 + title varchar(255),
  344 + zip varchar(255),
  345 + CONSTRAINT fk_tenant_profile FOREIGN KEY (tenant_profile_id) REFERENCES tenant_profile(id)
  346 +);
  347 +
  348 +CREATE TABLE IF NOT EXISTS user_credentials (
  349 + id uuid NOT NULL CONSTRAINT user_credentials_pkey PRIMARY KEY,
  350 + created_time bigint NOT NULL,
  351 + activate_token varchar(255) UNIQUE,
  352 + enabled boolean,
  353 + password varchar(255),
  354 + reset_token varchar(255) UNIQUE,
  355 + user_id uuid UNIQUE
  356 +);
  357 +
  358 +CREATE TABLE IF NOT EXISTS widget_type (
  359 + id uuid NOT NULL CONSTRAINT widget_type_pkey PRIMARY KEY,
  360 + created_time bigint NOT NULL,
  361 + alias varchar(255),
  362 + bundle_alias varchar(255),
  363 + descriptor varchar(1000000),
  364 + name varchar(255),
  365 + tenant_id uuid,
  366 + image varchar(1000000),
  367 + description varchar(255)
  368 +);
  369 +
  370 +CREATE TABLE IF NOT EXISTS widgets_bundle (
  371 + id uuid NOT NULL CONSTRAINT widgets_bundle_pkey PRIMARY KEY,
  372 + created_time bigint NOT NULL,
  373 + alias varchar(255),
  374 + search_text varchar(255),
  375 + tenant_id uuid,
  376 + title varchar(255),
  377 + image varchar(1000000),
  378 + description varchar(255)
  379 +);
  380 +
  381 +CREATE TABLE IF NOT EXISTS entity_view (
  382 + id uuid NOT NULL CONSTRAINT entity_view_pkey PRIMARY KEY,
  383 + created_time bigint NOT NULL,
  384 + entity_id uuid,
  385 + entity_type varchar(255),
  386 + tenant_id uuid,
  387 + customer_id uuid,
  388 + type varchar(255),
  389 + name varchar(255),
  390 + keys varchar(10000000),
  391 + start_ts bigint,
  392 + end_ts bigint,
  393 + search_text varchar(255),
  394 + additional_info varchar
  395 +);
  396 +
  397 +CREATE TABLE IF NOT EXISTS ts_kv_latest
  398 +(
  399 + entity_id uuid NOT NULL,
  400 + key int NOT NULL,
  401 + ts bigint NOT NULL,
  402 + bool_v boolean,
  403 + str_v varchar(10000000),
  404 + long_v bigint,
  405 + dbl_v double precision,
  406 + json_v json,
  407 + CONSTRAINT ts_kv_latest_pkey PRIMARY KEY (entity_id, key)
  408 +);
  409 +
  410 +CREATE TABLE IF NOT EXISTS ts_kv_dictionary
  411 +(
  412 + key varchar(255) NOT NULL,
  413 + key_id serial UNIQUE,
  414 + CONSTRAINT ts_key_id_pkey PRIMARY KEY (key)
  415 +);
  416 +
  417 +CREATE TABLE IF NOT EXISTS oauth2_params (
  418 + id uuid NOT NULL CONSTRAINT oauth2_params_pkey PRIMARY KEY,
  419 + enabled boolean,
  420 + tenant_id uuid,
  421 + created_time bigint NOT NULL
  422 +);
  423 +
  424 +CREATE TABLE IF NOT EXISTS oauth2_registration (
  425 + id uuid NOT NULL CONSTRAINT oauth2_registration_pkey PRIMARY KEY,
  426 + oauth2_params_id uuid NOT NULL,
  427 + created_time bigint NOT NULL,
  428 + additional_info varchar,
  429 + client_id varchar(255),
  430 + client_secret varchar(2048),
  431 + authorization_uri varchar(255),
  432 + token_uri varchar(255),
  433 + scope varchar(255),
  434 + platforms varchar(255),
  435 + user_info_uri varchar(255),
  436 + user_name_attribute_name varchar(255),
  437 + jwk_set_uri varchar(255),
  438 + client_authentication_method varchar(255),
  439 + login_button_label varchar(255),
  440 + login_button_icon varchar(255),
  441 + allow_user_creation boolean,
  442 + activate_user boolean,
  443 + type varchar(31),
  444 + basic_email_attribute_key varchar(31),
  445 + basic_first_name_attribute_key varchar(31),
  446 + basic_last_name_attribute_key varchar(31),
  447 + basic_tenant_name_strategy varchar(31),
  448 + basic_tenant_name_pattern varchar(255),
  449 + basic_customer_name_pattern varchar(255),
  450 + basic_default_dashboard_name varchar(255),
  451 + basic_always_full_screen boolean,
  452 + custom_url varchar(255),
  453 + custom_username varchar(255),
  454 + custom_password varchar(255),
  455 + custom_send_token boolean,
  456 + CONSTRAINT fk_registration_oauth2_params FOREIGN KEY (oauth2_params_id) REFERENCES oauth2_params(id) ON DELETE CASCADE
  457 +);
  458 +
  459 +CREATE TABLE IF NOT EXISTS oauth2_domain (
  460 + id uuid NOT NULL CONSTRAINT oauth2_domain_pkey PRIMARY KEY,
  461 + oauth2_params_id uuid NOT NULL,
  462 + created_time bigint NOT NULL,
  463 + domain_name varchar(255),
  464 + domain_scheme varchar(31),
  465 + CONSTRAINT fk_domain_oauth2_params FOREIGN KEY (oauth2_params_id) REFERENCES oauth2_params(id) ON DELETE CASCADE,
  466 + CONSTRAINT oauth2_domain_unq_key UNIQUE (oauth2_params_id, domain_name, domain_scheme)
  467 +);
  468 +
  469 +CREATE TABLE IF NOT EXISTS oauth2_mobile (
  470 + id uuid NOT NULL CONSTRAINT oauth2_mobile_pkey PRIMARY KEY,
  471 + oauth2_params_id uuid NOT NULL,
  472 + created_time bigint NOT NULL,
  473 + pkg_name varchar(255),
  474 + app_secret varchar(2048),
  475 + CONSTRAINT fk_mobile_oauth2_params FOREIGN KEY (oauth2_params_id) REFERENCES oauth2_params(id) ON DELETE CASCADE,
  476 + CONSTRAINT oauth2_mobile_unq_key UNIQUE (oauth2_params_id, pkg_name)
  477 +);
  478 +
  479 +CREATE TABLE IF NOT EXISTS oauth2_client_registration_template (
  480 + id uuid NOT NULL CONSTRAINT oauth2_client_registration_template_pkey PRIMARY KEY,
  481 + created_time bigint NOT NULL,
  482 + additional_info varchar,
  483 + provider_id varchar(255),
  484 + authorization_uri varchar(255),
  485 + token_uri varchar(255),
  486 + scope varchar(255),
  487 + user_info_uri varchar(255),
  488 + user_name_attribute_name varchar(255),
  489 + jwk_set_uri varchar(255),
  490 + client_authentication_method varchar(255),
  491 + type varchar(31),
  492 + basic_email_attribute_key varchar(31),
  493 + basic_first_name_attribute_key varchar(31),
  494 + basic_last_name_attribute_key varchar(31),
  495 + basic_tenant_name_strategy varchar(31),
  496 + basic_tenant_name_pattern varchar(255),
  497 + basic_customer_name_pattern varchar(255),
  498 + basic_default_dashboard_name varchar(255),
  499 + basic_always_full_screen boolean,
  500 + comment varchar,
  501 + login_button_icon varchar(255),
  502 + login_button_label varchar(255),
  503 + help_link varchar(255),
  504 + CONSTRAINT oauth2_template_provider_id_unq_key UNIQUE (provider_id)
  505 +);
  506 +
  507 +-- Deprecated
  508 +CREATE TABLE IF NOT EXISTS oauth2_client_registration_info (
  509 + id uuid NOT NULL CONSTRAINT oauth2_client_registration_info_pkey PRIMARY KEY,
  510 + enabled boolean,
  511 + created_time bigint NOT NULL,
  512 + additional_info varchar,
  513 + client_id varchar(255),
  514 + client_secret varchar(255),
  515 + authorization_uri varchar(255),
  516 + token_uri varchar(255),
  517 + scope varchar(255),
  518 + user_info_uri varchar(255),
  519 + user_name_attribute_name varchar(255),
  520 + jwk_set_uri varchar(255),
  521 + client_authentication_method varchar(255),
  522 + login_button_label varchar(255),
  523 + login_button_icon varchar(255),
  524 + allow_user_creation boolean,
  525 + activate_user boolean,
  526 + type varchar(31),
  527 + basic_email_attribute_key varchar(31),
  528 + basic_first_name_attribute_key varchar(31),
  529 + basic_last_name_attribute_key varchar(31),
  530 + basic_tenant_name_strategy varchar(31),
  531 + basic_tenant_name_pattern varchar(255),
  532 + basic_customer_name_pattern varchar(255),
  533 + basic_default_dashboard_name varchar(255),
  534 + basic_always_full_screen boolean,
  535 + custom_url varchar(255),
  536 + custom_username varchar(255),
  537 + custom_password varchar(255),
  538 + custom_send_token boolean
  539 +);
  540 +
  541 +-- Deprecated
  542 +CREATE TABLE IF NOT EXISTS oauth2_client_registration (
  543 + id uuid NOT NULL CONSTRAINT oauth2_client_registration_pkey PRIMARY KEY,
  544 + created_time bigint NOT NULL,
  545 + domain_name varchar(255),
  546 + domain_scheme varchar(31),
  547 + client_registration_info_id uuid
  548 +);
  549 +
  550 +CREATE TABLE IF NOT EXISTS api_usage_state (
  551 + id uuid NOT NULL CONSTRAINT usage_record_pkey PRIMARY KEY,
  552 + created_time bigint NOT NULL,
  553 + tenant_id uuid,
  554 + entity_type varchar(32),
  555 + entity_id uuid,
  556 + transport varchar(32),
  557 + db_storage varchar(32),
  558 + re_exec varchar(32),
  559 + js_exec varchar(32),
  560 + email_exec varchar(32),
  561 + sms_exec varchar(32),
  562 + alarm_exec varchar(32),
  563 + CONSTRAINT api_usage_state_unq_key UNIQUE (tenant_id, entity_id)
  564 +);
  565 +
  566 +CREATE TABLE IF NOT EXISTS resource (
  567 + id uuid NOT NULL CONSTRAINT resource_pkey PRIMARY KEY,
  568 + created_time bigint NOT NULL,
  569 + tenant_id uuid NOT NULL,
  570 + title varchar(255) NOT NULL,
  571 + resource_type varchar(32) NOT NULL,
  572 + resource_key varchar(255) NOT NULL,
  573 + search_text varchar(255),
  574 + file_name varchar(255) NOT NULL,
  575 + data varchar,
  576 + CONSTRAINT resource_unq_key UNIQUE (tenant_id, resource_type, resource_key)
  577 +);
  578 +
  579 +CREATE TABLE IF NOT EXISTS edge (
  580 + id uuid NOT NULL CONSTRAINT edge_pkey PRIMARY KEY,
  581 + created_time bigint NOT NULL,
  582 + additional_info varchar,
  583 + customer_id uuid,
  584 + root_rule_chain_id uuid,
  585 + type varchar(255),
  586 + name varchar(255),
  587 + label varchar(255),
  588 + routing_key varchar(255),
  589 + secret varchar(255),
  590 + edge_license_key varchar(30),
  591 + cloud_endpoint varchar(255),
  592 + search_text varchar(255),
  593 + tenant_id uuid,
  594 + CONSTRAINT edge_name_unq_key UNIQUE (tenant_id, name),
  595 + CONSTRAINT edge_routing_key_unq_key UNIQUE (routing_key)
  596 +);
  597 +
  598 +CREATE TABLE IF NOT EXISTS edge_event (
  599 + id uuid NOT NULL CONSTRAINT edge_event_pkey PRIMARY KEY,
  600 + created_time bigint NOT NULL,
  601 + edge_id uuid,
  602 + edge_event_type varchar(255),
  603 + edge_event_uid varchar(255),
  604 + entity_id uuid,
  605 + edge_event_action varchar(255),
  606 + body varchar(10000000),
  607 + tenant_id uuid,
  608 + ts bigint NOT NULL
  609 +);
  610 +
  611 +CREATE TABLE IF NOT EXISTS rpc (
  612 + id uuid NOT NULL CONSTRAINT rpc_pkey PRIMARY KEY,
  613 + created_time bigint NOT NULL,
  614 + tenant_id uuid NOT NULL,
  615 + device_id uuid NOT NULL,
  616 + expiration_time bigint NOT NULL,
  617 + request varchar(10000000) NOT NULL,
  618 + response varchar(10000000),
  619 + additional_info varchar(10000000),
  620 + status varchar(255) NOT NULL
  621 +);
  622 +
  623 +CREATE OR REPLACE PROCEDURE cleanup_events_by_ttl(IN ttl bigint, IN debug_ttl bigint, INOUT deleted bigint)
  624 + LANGUAGE plpgsql AS
  625 +$$
  626 +DECLARE
  627 + ttl_ts bigint;
  628 + debug_ttl_ts bigint;
  629 + ttl_deleted_count bigint DEFAULT 0;
  630 + debug_ttl_deleted_count bigint DEFAULT 0;
  631 +BEGIN
  632 + IF ttl > 0 THEN
  633 + ttl_ts := (EXTRACT(EPOCH FROM current_timestamp) * 1000 - ttl::bigint * 1000)::bigint;
  634 + EXECUTE format(
  635 + 'WITH deleted AS (DELETE FROM event WHERE ts < %L::bigint AND (event_type != %L::varchar AND event_type != %L::varchar) RETURNING *) SELECT count(*) FROM deleted', ttl_ts, 'DEBUG_RULE_NODE', 'DEBUG_RULE_CHAIN') into ttl_deleted_count;
  636 + END IF;
  637 + IF debug_ttl > 0 THEN
  638 + debug_ttl_ts := (EXTRACT(EPOCH FROM current_timestamp) * 1000 - debug_ttl::bigint * 1000)::bigint;
  639 + EXECUTE format(
  640 + 'WITH deleted AS (DELETE FROM event WHERE ts < %L::bigint AND (event_type = %L::varchar OR event_type = %L::varchar) RETURNING *) SELECT count(*) FROM deleted', debug_ttl_ts, 'DEBUG_RULE_NODE', 'DEBUG_RULE_CHAIN') into debug_ttl_deleted_count;
  641 + END IF;
  642 + RAISE NOTICE 'Events removed by ttl: %', ttl_deleted_count;
  643 + RAISE NOTICE 'Debug Events removed by ttl: %', debug_ttl_deleted_count;
  644 + deleted := ttl_deleted_count + debug_ttl_deleted_count;
  645 +END
  646 +$$;
  647 +
  648 +CREATE OR REPLACE FUNCTION to_uuid(IN entity_id varchar, OUT uuid_id uuid) AS
  649 +$$
  650 +BEGIN
  651 + uuid_id := substring(entity_id, 8, 8) || '-' || substring(entity_id, 4, 4) || '-1' || substring(entity_id, 1, 3) ||
  652 + '-' || substring(entity_id, 16, 4) || '-' || substring(entity_id, 20, 12);
  653 +END;
  654 +$$ LANGUAGE plpgsql;
  655 +
  656 +
  657 +CREATE OR REPLACE PROCEDURE cleanup_edge_events_by_ttl(IN ttl bigint, INOUT deleted bigint)
  658 + LANGUAGE plpgsql AS
  659 +$$
  660 +DECLARE
  661 + ttl_ts bigint;
  662 + ttl_deleted_count bigint DEFAULT 0;
  663 +BEGIN
  664 + IF ttl > 0 THEN
  665 + ttl_ts := (EXTRACT(EPOCH FROM current_timestamp) * 1000 - ttl::bigint * 1000)::bigint;
  666 + EXECUTE format(
  667 + 'WITH deleted AS (DELETE FROM edge_event WHERE ts < %L::bigint RETURNING *) SELECT count(*) FROM deleted', ttl_ts) into ttl_deleted_count;
  668 + END IF;
  669 + RAISE NOTICE 'Edge events removed by ttl: %', ttl_deleted_count;
  670 + deleted := ttl_deleted_count;
  671 +END
  672 +$$;
... ...
  1 +--
  2 +-- Copyright © 2016-2021 The Thingsboard Authors
  3 +--
  4 +-- Licensed under the Apache License, Version 2.0 (the "License");
  5 +-- you may not use this file except in compliance with the License.
  6 +-- You may obtain a copy of the License at
  7 +--
  8 +-- http://www.apache.org/licenses/LICENSE-2.0
  9 +--
  10 +-- Unless required by applicable law or agreed to in writing, software
  11 +-- distributed under the License is distributed on an "AS IS" BASIS,
  12 +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 +-- See the License for the specific language governing permissions and
  14 +-- limitations under the License.
  15 +--
  16 +
  17 +CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;
  18 +
  19 +CREATE TABLE IF NOT EXISTS ts_kv (
  20 + entity_id uuid NOT NULL,
  21 + key int NOT NULL,
  22 + ts bigint NOT NULL,
  23 + bool_v boolean,
  24 + str_v varchar(10000000),
  25 + long_v bigint,
  26 + dbl_v double precision,
  27 + json_v json,
  28 + CONSTRAINT ts_kv_pkey PRIMARY KEY (entity_id, key, ts)
  29 +);
  30 +
  31 +CREATE TABLE IF NOT EXISTS ts_kv_dictionary (
  32 + key varchar(255) NOT NULL,
  33 + key_id serial UNIQUE,
  34 + CONSTRAINT ts_key_id_pkey PRIMARY KEY (key)
  35 +);
  36 +
  37 +CREATE TABLE IF NOT EXISTS ts_kv_latest (
  38 + entity_id uuid NOT NULL,
  39 + key int NOT NULL,
  40 + ts bigint NOT NULL,
  41 + bool_v boolean,
  42 + str_v varchar(10000000),
  43 + long_v bigint,
  44 + dbl_v double precision,
  45 + json_v json,
  46 + CONSTRAINT ts_kv_latest_pkey PRIMARY KEY (entity_id, key)
  47 +);
  48 +
  49 +CREATE OR REPLACE FUNCTION to_uuid(IN entity_id varchar, OUT uuid_id uuid) AS
  50 +$$
  51 +BEGIN
  52 + uuid_id := substring(entity_id, 8, 8) || '-' || substring(entity_id, 4, 4) || '-1' || substring(entity_id, 1, 3) ||
  53 + '-' || substring(entity_id, 16, 4) || '-' || substring(entity_id, 20, 12);
  54 +END;
  55 +$$ LANGUAGE plpgsql;
  56 +
  57 +CREATE OR REPLACE FUNCTION delete_device_records_from_ts_kv(tenant_id uuid, customer_id uuid, ttl bigint,
  58 + OUT deleted bigint) AS
  59 +$$
  60 +BEGIN
  61 + EXECUTE format(
  62 + 'WITH deleted AS (DELETE FROM ts_kv WHERE entity_id IN (SELECT device.id as entity_id FROM device WHERE tenant_id = %L and customer_id = %L) AND ts < %L::bigint RETURNING *) SELECT count(*) FROM deleted',
  63 + tenant_id, customer_id, ttl) into deleted;
  64 +END;
  65 +$$ LANGUAGE plpgsql;
  66 +
  67 +CREATE OR REPLACE FUNCTION delete_asset_records_from_ts_kv(tenant_id uuid, customer_id uuid, ttl bigint,
  68 + OUT deleted bigint) AS
  69 +$$
  70 +BEGIN
  71 + EXECUTE format(
  72 + 'WITH deleted AS (DELETE FROM ts_kv WHERE entity_id IN (SELECT asset.id as entity_id FROM asset WHERE tenant_id = %L and customer_id = %L) AND ts < %L::bigint RETURNING *) SELECT count(*) FROM deleted',
  73 + tenant_id, customer_id, ttl) into deleted;
  74 +END;
  75 +$$ LANGUAGE plpgsql;
  76 +
  77 +CREATE OR REPLACE FUNCTION delete_customer_records_from_ts_kv(tenant_id uuid, customer_id uuid, ttl bigint,
  78 + OUT deleted bigint) AS
  79 +$$
  80 +BEGIN
  81 + EXECUTE format(
  82 + 'WITH deleted AS (DELETE FROM ts_kv WHERE entity_id IN (SELECT customer.id as entity_id FROM customer WHERE tenant_id = %L and id = %L) AND ts < %L::bigint RETURNING *) SELECT count(*) FROM deleted',
  83 + tenant_id, customer_id, ttl) into deleted;
  84 +END;
  85 +$$ LANGUAGE plpgsql;
  86 +
  87 +CREATE OR REPLACE PROCEDURE cleanup_timeseries_by_ttl(IN null_uuid uuid,
  88 + IN system_ttl bigint, INOUT deleted bigint)
  89 + LANGUAGE plpgsql AS
  90 +$$
  91 +DECLARE
  92 + tenant_cursor CURSOR FOR select tenant.id as tenant_id
  93 + from tenant;
  94 + tenant_id_record uuid;
  95 + customer_id_record uuid;
  96 + tenant_ttl bigint;
  97 + customer_ttl bigint;
  98 + deleted_for_entities bigint;
  99 + tenant_ttl_ts bigint;
  100 + customer_ttl_ts bigint;
  101 +BEGIN
  102 + OPEN tenant_cursor;
  103 + FETCH tenant_cursor INTO tenant_id_record;
  104 + WHILE FOUND
  105 + LOOP
  106 + EXECUTE format(
  107 + 'select attribute_kv.long_v from attribute_kv where attribute_kv.entity_id = %L and attribute_kv.attribute_key = %L',
  108 + tenant_id_record, 'TTL') INTO tenant_ttl;
  109 + if tenant_ttl IS NULL THEN
  110 + tenant_ttl := system_ttl;
  111 + END IF;
  112 + IF tenant_ttl > 0 THEN
  113 + tenant_ttl_ts := (EXTRACT(EPOCH FROM current_timestamp) * 1000 - tenant_ttl::bigint * 1000)::bigint;
  114 + deleted_for_entities := delete_device_records_from_ts_kv(tenant_id_record, null_uuid, tenant_ttl_ts);
  115 + deleted := deleted + deleted_for_entities;
  116 + RAISE NOTICE '% telemetry removed for devices where tenant_id = %', deleted_for_entities, tenant_id_record;
  117 + deleted_for_entities := delete_asset_records_from_ts_kv(tenant_id_record, null_uuid, tenant_ttl_ts);
  118 + deleted := deleted + deleted_for_entities;
  119 + RAISE NOTICE '% telemetry removed for assets where tenant_id = %', deleted_for_entities, tenant_id_record;
  120 + END IF;
  121 + FOR customer_id_record IN
  122 + SELECT customer.id AS customer_id FROM customer WHERE customer.tenant_id = tenant_id_record
  123 + LOOP
  124 + EXECUTE format(
  125 + 'select attribute_kv.long_v from attribute_kv where attribute_kv.entity_id = %L and attribute_kv.attribute_key = %L',
  126 + customer_id_record, 'TTL') INTO customer_ttl;
  127 + IF customer_ttl IS NULL THEN
  128 + customer_ttl_ts := tenant_ttl_ts;
  129 + ELSE
  130 + IF customer_ttl > 0 THEN
  131 + customer_ttl_ts :=
  132 + (EXTRACT(EPOCH FROM current_timestamp) * 1000 -
  133 + customer_ttl::bigint * 1000)::bigint;
  134 + END IF;
  135 + END IF;
  136 + IF customer_ttl_ts IS NOT NULL AND customer_ttl_ts > 0 THEN
  137 + deleted_for_entities :=
  138 + delete_customer_records_from_ts_kv(tenant_id_record, customer_id_record,
  139 + customer_ttl_ts);
  140 + deleted := deleted + deleted_for_entities;
  141 + RAISE NOTICE '% telemetry removed for customer with id = % where tenant_id = %', deleted_for_entities, customer_id_record, tenant_id_record;
  142 + deleted_for_entities :=
  143 + delete_device_records_from_ts_kv(tenant_id_record, customer_id_record,
  144 + customer_ttl_ts);
  145 + deleted := deleted + deleted_for_entities;
  146 + RAISE NOTICE '% telemetry removed for devices where tenant_id = % and customer_id = %', deleted_for_entities, tenant_id_record, customer_id_record;
  147 + deleted_for_entities := delete_asset_records_from_ts_kv(tenant_id_record,
  148 + customer_id_record,
  149 + customer_ttl_ts);
  150 + deleted := deleted + deleted_for_entities;
  151 + RAISE NOTICE '% telemetry removed for assets where tenant_id = % and customer_id = %', deleted_for_entities, tenant_id_record, customer_id_record;
  152 + END IF;
  153 + END LOOP;
  154 + FETCH tenant_cursor INTO tenant_id_record;
  155 + END LOOP;
  156 +END
  157 +$$;
... ...
  1 +--
  2 +-- Copyright © 2016-2021 The Thingsboard Authors
  3 +--
  4 +-- Licensed under the Apache License, Version 2.0 (the "License");
  5 +-- you may not use this file except in compliance with the License.
  6 +-- You may obtain a copy of the License at
  7 +--
  8 +-- http://www.apache.org/licenses/LICENSE-2.0
  9 +--
  10 +-- Unless required by applicable law or agreed to in writing, software
  11 +-- distributed under the License is distributed on an "AS IS" BASIS,
  12 +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 +-- See the License for the specific language governing permissions and
  14 +-- limitations under the License.
  15 +--
  16 +
  17 +SET DATABASE SQL SYNTAX PGS TRUE;
  18 +SET DATABASE TRANSACTION CONTROL MVCC;
  19 +
  20 +CREATE TABLE IF NOT EXISTS ts_kv (
  21 + entity_id uuid NOT NULL,
  22 + key int NOT NULL,
  23 + ts bigint NOT NULL,
  24 + bool_v boolean,
  25 + str_v varchar(10000000),
  26 + long_v bigint,
  27 + dbl_v double precision,
  28 + json_v varchar(10000000),
  29 + CONSTRAINT ts_kv_pkey PRIMARY KEY (entity_id, key, ts)
  30 +);
  31 +
  32 +CREATE TABLE IF NOT EXISTS ts_kv_dictionary (
  33 + key varchar(255) NOT NULL,
  34 + key_id int GENERATED BY DEFAULT AS IDENTITY(start with 0 increment by 1) UNIQUE,
  35 + CONSTRAINT ts_key_id_pkey PRIMARY KEY (key)
  36 +);
  37 +
  38 +CREATE FUNCTION to_uuid(IN entity_id varchar)
  39 + RETURNS UUID
  40 + RETURN UUID(substring(entity_id, 8, 8) || '-' || substring(entity_id, 4, 4) || '-1' || substring(entity_id, 1, 3) ||
  41 + '-' || substring(entity_id, 16, 4) || '-' || substring(entity_id, 20, 12));
... ...
  1 +--
  2 +-- Copyright © 2016-2021 The Thingsboard Authors
  3 +--
  4 +-- Licensed under the Apache License, Version 2.0 (the "License");
  5 +-- you may not use this file except in compliance with the License.
  6 +-- You may obtain a copy of the License at
  7 +--
  8 +-- http://www.apache.org/licenses/LICENSE-2.0
  9 +--
  10 +-- Unless required by applicable law or agreed to in writing, software
  11 +-- distributed under the License is distributed on an "AS IS" BASIS,
  12 +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 +-- See the License for the specific language governing permissions and
  14 +-- limitations under the License.
  15 +--
  16 +
  17 +CREATE TABLE IF NOT EXISTS ts_kv
  18 +(
  19 + entity_id uuid NOT NULL,
  20 + key int NOT NULL,
  21 + ts bigint NOT NULL,
  22 + bool_v boolean,
  23 + str_v varchar(10000000),
  24 + long_v bigint,
  25 + dbl_v double precision,
  26 + json_v json,
  27 + CONSTRAINT ts_kv_pkey PRIMARY KEY (entity_id, key, ts)
  28 +) PARTITION BY RANGE (ts);
  29 +
  30 +CREATE TABLE IF NOT EXISTS ts_kv_dictionary
  31 +(
  32 + key varchar(255) NOT NULL,
  33 + key_id serial UNIQUE,
  34 + CONSTRAINT ts_key_id_pkey PRIMARY KEY (key)
  35 +);
  36 +
  37 +CREATE OR REPLACE PROCEDURE drop_partitions_by_max_ttl(IN partition_type varchar, IN system_ttl bigint, INOUT deleted bigint)
  38 + LANGUAGE plpgsql AS
  39 +$$
  40 +DECLARE
  41 + max_tenant_ttl bigint;
  42 + max_customer_ttl bigint;
  43 + max_ttl bigint;
  44 + date timestamp;
  45 + partition_by_max_ttl_date varchar;
  46 + partition_by_max_ttl_month varchar;
  47 + partition_by_max_ttl_day varchar;
  48 + partition_by_max_ttl_year varchar;
  49 + partition varchar;
  50 + partition_year integer;
  51 + partition_month integer;
  52 + partition_day integer;
  53 +
  54 +BEGIN
  55 + SELECT max(attribute_kv.long_v)
  56 + FROM tenant
  57 + INNER JOIN attribute_kv ON tenant.id = attribute_kv.entity_id
  58 + WHERE attribute_kv.attribute_key = 'TTL'
  59 + into max_tenant_ttl;
  60 + SELECT max(attribute_kv.long_v)
  61 + FROM customer
  62 + INNER JOIN attribute_kv ON customer.id = attribute_kv.entity_id
  63 + WHERE attribute_kv.attribute_key = 'TTL'
  64 + into max_customer_ttl;
  65 + max_ttl := GREATEST(system_ttl, max_customer_ttl, max_tenant_ttl);
  66 + if max_ttl IS NOT NULL AND max_ttl > 0 THEN
  67 + date := to_timestamp(EXTRACT(EPOCH FROM current_timestamp) - max_ttl);
  68 + partition_by_max_ttl_date := get_partition_by_max_ttl_date(partition_type, date);
  69 + RAISE NOTICE 'Date by max ttl: %', date;
  70 + RAISE NOTICE 'Partition by max ttl: %', partition_by_max_ttl_date;
  71 + IF partition_by_max_ttl_date IS NOT NULL THEN
  72 + CASE
  73 + WHEN partition_type = 'DAYS' THEN
  74 + partition_by_max_ttl_year := SPLIT_PART(partition_by_max_ttl_date, '_', 3);
  75 + partition_by_max_ttl_month := SPLIT_PART(partition_by_max_ttl_date, '_', 4);
  76 + partition_by_max_ttl_day := SPLIT_PART(partition_by_max_ttl_date, '_', 5);
  77 + WHEN partition_type = 'MONTHS' THEN
  78 + partition_by_max_ttl_year := SPLIT_PART(partition_by_max_ttl_date, '_', 3);
  79 + partition_by_max_ttl_month := SPLIT_PART(partition_by_max_ttl_date, '_', 4);
  80 + ELSE
  81 + partition_by_max_ttl_year := SPLIT_PART(partition_by_max_ttl_date, '_', 3);
  82 + END CASE;
  83 + IF partition_by_max_ttl_year IS NULL THEN
  84 + RAISE NOTICE 'Failed to remove partitions by max ttl date due to partition_by_max_ttl_year is null!';
  85 + ELSE
  86 + IF partition_type = 'YEARS' THEN
  87 + FOR partition IN SELECT tablename
  88 + FROM pg_tables
  89 + WHERE schemaname = 'public'
  90 + AND tablename like 'ts_kv_' || '%'
  91 + AND tablename != 'ts_kv_latest'
  92 + AND tablename != 'ts_kv_dictionary'
  93 + AND tablename != 'ts_kv_indefinite'
  94 + AND tablename != partition_by_max_ttl_date
  95 + LOOP
  96 + partition_year := SPLIT_PART(partition, '_', 3)::integer;
  97 + IF partition_year < partition_by_max_ttl_year::integer THEN
  98 + RAISE NOTICE 'Partition to delete by max ttl: %', partition;
  99 + EXECUTE format('DROP TABLE IF EXISTS %I', partition);
  100 + deleted := deleted + 1;
  101 + END IF;
  102 + END LOOP;
  103 + ELSE
  104 + IF partition_type = 'MONTHS' THEN
  105 + IF partition_by_max_ttl_month IS NULL THEN
  106 + RAISE NOTICE 'Failed to remove months partitions by max ttl date due to partition_by_max_ttl_month is null!';
  107 + ELSE
  108 + FOR partition IN SELECT tablename
  109 + FROM pg_tables
  110 + WHERE schemaname = 'public'
  111 + AND tablename like 'ts_kv_' || '%'
  112 + AND tablename != 'ts_kv_latest'
  113 + AND tablename != 'ts_kv_dictionary'
  114 + AND tablename != 'ts_kv_indefinite'
  115 + AND tablename != partition_by_max_ttl_date
  116 + LOOP
  117 + partition_year := SPLIT_PART(partition, '_', 3)::integer;
  118 + IF partition_year > partition_by_max_ttl_year::integer THEN
  119 + RAISE NOTICE 'Skip iteration! Partition: % is valid!', partition;
  120 + CONTINUE;
  121 + ELSE
  122 + IF partition_year < partition_by_max_ttl_year::integer THEN
  123 + RAISE NOTICE 'Partition to delete by max ttl: %', partition;
  124 + EXECUTE format('DROP TABLE IF EXISTS %I', partition);
  125 + deleted := deleted + 1;
  126 + ELSE
  127 + partition_month := SPLIT_PART(partition, '_', 4)::integer;
  128 + IF partition_year = partition_by_max_ttl_year::integer THEN
  129 + IF partition_month >= partition_by_max_ttl_month::integer THEN
  130 + RAISE NOTICE 'Skip iteration! Partition: % is valid!', partition;
  131 + CONTINUE;
  132 + ELSE
  133 + RAISE NOTICE 'Partition to delete by max ttl: %', partition;
  134 + EXECUTE format('DROP TABLE IF EXISTS %I', partition);
  135 + deleted := deleted + 1;
  136 + END IF;
  137 + END IF;
  138 + END IF;
  139 + END IF;
  140 + END LOOP;
  141 + END IF;
  142 + ELSE
  143 + IF partition_type = 'DAYS' THEN
  144 + IF partition_by_max_ttl_month IS NULL THEN
  145 + RAISE NOTICE 'Failed to remove days partitions by max ttl date due to partition_by_max_ttl_month is null!';
  146 + ELSE
  147 + IF partition_by_max_ttl_day IS NULL THEN
  148 + RAISE NOTICE 'Failed to remove days partitions by max ttl date due to partition_by_max_ttl_day is null!';
  149 + ELSE
  150 + FOR partition IN SELECT tablename
  151 + FROM pg_tables
  152 + WHERE schemaname = 'public'
  153 + AND tablename like 'ts_kv_' || '%'
  154 + AND tablename != 'ts_kv_latest'
  155 + AND tablename != 'ts_kv_dictionary'
  156 + AND tablename != 'ts_kv_indefinite'
  157 + AND tablename != partition_by_max_ttl_date
  158 + LOOP
  159 + partition_year := SPLIT_PART(partition, '_', 3)::integer;
  160 + IF partition_year > partition_by_max_ttl_year::integer THEN
  161 + RAISE NOTICE 'Skip iteration! Partition: % is valid!', partition;
  162 + CONTINUE;
  163 + ELSE
  164 + IF partition_year < partition_by_max_ttl_year::integer THEN
  165 + RAISE NOTICE 'Partition to delete by max ttl: %', partition;
  166 + EXECUTE format('DROP TABLE IF EXISTS %I', partition);
  167 + deleted := deleted + 1;
  168 + ELSE
  169 + partition_month := SPLIT_PART(partition, '_', 4)::integer;
  170 + IF partition_month > partition_by_max_ttl_month::integer THEN
  171 + RAISE NOTICE 'Skip iteration! Partition: % is valid!', partition;
  172 + CONTINUE;
  173 + ELSE
  174 + IF partition_month < partition_by_max_ttl_month::integer THEN
  175 + RAISE NOTICE 'Partition to delete by max ttl: %', partition;
  176 + EXECUTE format('DROP TABLE IF EXISTS %I', partition);
  177 + deleted := deleted + 1;
  178 + ELSE
  179 + partition_day := SPLIT_PART(partition, '_', 5)::integer;
  180 + IF partition_day >= partition_by_max_ttl_day::integer THEN
  181 + RAISE NOTICE 'Skip iteration! Partition: % is valid!', partition;
  182 + CONTINUE;
  183 + ELSE
  184 + IF partition_day < partition_by_max_ttl_day::integer THEN
  185 + RAISE NOTICE 'Partition to delete by max ttl: %', partition;
  186 + EXECUTE format('DROP TABLE IF EXISTS %I', partition);
  187 + deleted := deleted + 1;
  188 + END IF;
  189 + END IF;
  190 + END IF;
  191 + END IF;
  192 + END IF;
  193 + END IF;
  194 + END LOOP;
  195 + END IF;
  196 + END IF;
  197 + END IF;
  198 + END IF;
  199 + END IF;
  200 + END IF;
  201 + END IF;
  202 + END IF;
  203 +END
  204 +$$;
  205 +
  206 +CREATE OR REPLACE FUNCTION get_partition_by_max_ttl_date(IN partition_type varchar, IN date timestamp, OUT partition varchar) AS
  207 +$$
  208 +BEGIN
  209 + CASE
  210 + WHEN partition_type = 'DAYS' THEN
  211 + partition := 'ts_kv_' || to_char(date, 'yyyy') || '_' || to_char(date, 'MM') || '_' || to_char(date, 'dd');
  212 + WHEN partition_type = 'MONTHS' THEN
  213 + partition := 'ts_kv_' || to_char(date, 'yyyy') || '_' || to_char(date, 'MM');
  214 + WHEN partition_type = 'YEARS' THEN
  215 + partition := 'ts_kv_' || to_char(date, 'yyyy');
  216 + ELSE
  217 + partition := NULL;
  218 + END CASE;
  219 + IF partition IS NOT NULL THEN
  220 + IF NOT EXISTS(SELECT
  221 + FROM pg_tables
  222 + WHERE schemaname = 'public'
  223 + AND tablename = partition) THEN
  224 + partition := NULL;
  225 + RAISE NOTICE 'Failed to found partition by ttl';
  226 + END IF;
  227 + END IF;
  228 +END;
  229 +$$ LANGUAGE plpgsql;
  230 +
  231 +CREATE OR REPLACE FUNCTION to_uuid(IN entity_id varchar, OUT uuid_id uuid) AS
  232 +$$
  233 +BEGIN
  234 + uuid_id := substring(entity_id, 8, 8) || '-' || substring(entity_id, 4, 4) || '-1' || substring(entity_id, 1, 3) ||
  235 + '-' || substring(entity_id, 16, 4) || '-' || substring(entity_id, 20, 12);
  236 +END;
  237 +$$ LANGUAGE plpgsql;
  238 +
  239 +CREATE OR REPLACE FUNCTION delete_device_records_from_ts_kv(tenant_id uuid, customer_id uuid, ttl bigint,
  240 + OUT deleted bigint) AS
  241 +$$
  242 +BEGIN
  243 + EXECUTE format(
  244 + 'WITH deleted AS (DELETE FROM ts_kv WHERE entity_id IN (SELECT device.id as entity_id FROM device WHERE tenant_id = %L and customer_id = %L) AND ts < %L::bigint RETURNING *) SELECT count(*) FROM deleted',
  245 + tenant_id, customer_id, ttl) into deleted;
  246 +END;
  247 +$$ LANGUAGE plpgsql;
  248 +
  249 +CREATE OR REPLACE FUNCTION delete_asset_records_from_ts_kv(tenant_id uuid, customer_id uuid, ttl bigint,
  250 + OUT deleted bigint) AS
  251 +$$
  252 +BEGIN
  253 + EXECUTE format(
  254 + 'WITH deleted AS (DELETE FROM ts_kv WHERE entity_id IN (SELECT asset.id as entity_id FROM asset WHERE tenant_id = %L and customer_id = %L) AND ts < %L::bigint RETURNING *) SELECT count(*) FROM deleted',
  255 + tenant_id, customer_id, ttl) into deleted;
  256 +END;
  257 +$$ LANGUAGE plpgsql;
  258 +
  259 +CREATE OR REPLACE FUNCTION delete_customer_records_from_ts_kv(tenant_id uuid, customer_id uuid, ttl bigint,
  260 + OUT deleted bigint) AS
  261 +$$
  262 +BEGIN
  263 + EXECUTE format(
  264 + 'WITH deleted AS (DELETE FROM ts_kv WHERE entity_id IN (SELECT customer.id as entity_id FROM customer WHERE tenant_id = %L and id = %L) AND ts < %L::bigint RETURNING *) SELECT count(*) FROM deleted',
  265 + tenant_id, customer_id, ttl) into deleted;
  266 +END;
  267 +$$ LANGUAGE plpgsql;
  268 +
  269 +CREATE OR REPLACE PROCEDURE cleanup_timeseries_by_ttl(IN null_uuid uuid,
  270 + IN system_ttl bigint, INOUT deleted bigint)
  271 + LANGUAGE plpgsql AS
  272 +$$
  273 +DECLARE
  274 + tenant_cursor CURSOR FOR select tenant.id as tenant_id
  275 + from tenant;
  276 + tenant_id_record uuid;
  277 + customer_id_record uuid;
  278 + tenant_ttl bigint;
  279 + customer_ttl bigint;
  280 + deleted_for_entities bigint;
  281 + tenant_ttl_ts bigint;
  282 + customer_ttl_ts bigint;
  283 +BEGIN
  284 + OPEN tenant_cursor;
  285 + FETCH tenant_cursor INTO tenant_id_record;
  286 + WHILE FOUND
  287 + LOOP
  288 + EXECUTE format(
  289 + 'select attribute_kv.long_v from attribute_kv where attribute_kv.entity_id = %L and attribute_kv.attribute_key = %L',
  290 + tenant_id_record, 'TTL') INTO tenant_ttl;
  291 + if tenant_ttl IS NULL THEN
  292 + tenant_ttl := system_ttl;
  293 + END IF;
  294 + IF tenant_ttl > 0 THEN
  295 + tenant_ttl_ts := (EXTRACT(EPOCH FROM current_timestamp) * 1000 - tenant_ttl::bigint * 1000)::bigint;
  296 + deleted_for_entities := delete_device_records_from_ts_kv(tenant_id_record, null_uuid, tenant_ttl_ts);
  297 + deleted := deleted + deleted_for_entities;
  298 + RAISE NOTICE '% telemetry removed for devices where tenant_id = %', deleted_for_entities, tenant_id_record;
  299 + deleted_for_entities := delete_asset_records_from_ts_kv(tenant_id_record, null_uuid, tenant_ttl_ts);
  300 + deleted := deleted + deleted_for_entities;
  301 + RAISE NOTICE '% telemetry removed for assets where tenant_id = %', deleted_for_entities, tenant_id_record;
  302 + END IF;
  303 + FOR customer_id_record IN
  304 + SELECT customer.id AS customer_id FROM customer WHERE customer.tenant_id = tenant_id_record
  305 + LOOP
  306 + EXECUTE format(
  307 + 'select attribute_kv.long_v from attribute_kv where attribute_kv.entity_id = %L and attribute_kv.attribute_key = %L',
  308 + customer_id_record, 'TTL') INTO customer_ttl;
  309 + IF customer_ttl IS NULL THEN
  310 + customer_ttl_ts := tenant_ttl_ts;
  311 + ELSE
  312 + IF customer_ttl > 0 THEN
  313 + customer_ttl_ts :=
  314 + (EXTRACT(EPOCH FROM current_timestamp) * 1000 -
  315 + customer_ttl::bigint * 1000)::bigint;
  316 + END IF;
  317 + END IF;
  318 + IF customer_ttl_ts IS NOT NULL AND customer_ttl_ts > 0 THEN
  319 + deleted_for_entities :=
  320 + delete_customer_records_from_ts_kv(tenant_id_record, customer_id_record,
  321 + customer_ttl_ts);
  322 + deleted := deleted + deleted_for_entities;
  323 + RAISE NOTICE '% telemetry removed for customer with id = % where tenant_id = %', deleted_for_entities, customer_id_record, tenant_id_record;
  324 + deleted_for_entities :=
  325 + delete_device_records_from_ts_kv(tenant_id_record, customer_id_record,
  326 + customer_ttl_ts);
  327 + deleted := deleted + deleted_for_entities;
  328 + RAISE NOTICE '% telemetry removed for devices where tenant_id = % and customer_id = %', deleted_for_entities, tenant_id_record, customer_id_record;
  329 + deleted_for_entities := delete_asset_records_from_ts_kv(tenant_id_record,
  330 + customer_id_record,
  331 + customer_ttl_ts);
  332 + deleted := deleted + deleted_for_entities;
  333 + RAISE NOTICE '% telemetry removed for assets where tenant_id = % and customer_id = %', deleted_for_entities, tenant_id_record, customer_id_record;
  334 + END IF;
  335 + END LOOP;
  336 + FETCH tenant_cursor INTO tenant_id_record;
  337 + END LOOP;
  338 +END
  339 +$$;
... ...
  1 +--
  2 +-- Copyright © 2016-2021 The Thingsboard Authors
  3 +--
  4 +-- Licensed under the Apache License, Version 2.0 (the "License");
  5 +-- you may not use this file except in compliance with the License.
  6 +-- You may obtain a copy of the License at
  7 +--
  8 +-- http://www.apache.org/licenses/LICENSE-2.0
  9 +--
  10 +-- Unless required by applicable law or agreed to in writing, software
  11 +-- distributed under the License is distributed on an "AS IS" BASIS,
  12 +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 +-- See the License for the specific language governing permissions and
  14 +-- limitations under the License.
  15 +--
  16 +
  17 +DROP TYPE json IF EXISTS;
  18 +CREATE TYPE json AS varchar;
  19 +DROP TYPE jsonb IF EXISTS;
  20 +CREATE TYPE jsonb AS other;
... ...
  1 +--
  2 +-- Copyright © 2016-2020 The Thingsboard Authors
  3 +--
  4 +-- Licensed under the Apache License, Version 2.0 (the "License");
  5 +-- you may not use this file except in compliance with the License.
  6 +-- You may obtain a copy of the License at
  7 +--
  8 +-- http://www.apache.org/licenses/LICENSE-2.0
  9 +--
  10 +-- Unless required by applicable law or agreed to in writing, software
  11 +-- distributed under the License is distributed on an "AS IS" BASIS,
  12 +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 +-- See the License for the specific language governing permissions and
  14 +-- limitations under the License.
  15 +--
  16 +
  17 +/** SYSTEM **/
  18 +
  19 +/** System admin **/
  20 +INSERT INTO tb_user ( id, created_time, tenant_id, customer_id, email, search_text, authority )
  21 +VALUES ( '5a797660-4612-11e7-a919-92ebcb67fe33', 1592576748000, '13814000-1dd2-11b2-8080-808080808080', '13814000-1dd2-11b2-8080-808080808080', 'sysadmin@thingsboard.org',
  22 + 'sysadmin@thingsboard.org', 'SYS_ADMIN' );
  23 +
  24 +INSERT INTO user_credentials ( id, created_time, user_id, enabled, password )
  25 +VALUES ( '61441950-4612-11e7-a919-92ebcb67fe33', 1592576748000, '5a797660-4612-11e7-a919-92ebcb67fe33', true,
  26 + '$2a$10$5JTB8/hxWc9WAy62nCGSxeefl3KWmipA9nFpVdDa0/xfIseeBB4Bu' );
  27 +
  28 +/** System settings **/
  29 +INSERT INTO admin_settings ( id, created_time, key, json_value )
  30 +VALUES ( '6a2266e4-4612-11e7-a919-92ebcb67fe33', 1592576748000, 'general', '{
  31 + "baseUrl": "http://localhost:8080"
  32 +}' );
  33 +
  34 +INSERT INTO admin_settings ( id, created_time, key, json_value )
  35 +VALUES ( '6eaaefa6-4612-11e7-a919-92ebcb67fe33', 1592576748000, 'mail', '{
  36 + "mailFrom": "Thingsboard <sysadmin@localhost.localdomain>",
  37 + "smtpProtocol": "smtp",
  38 + "smtpHost": "localhost",
  39 + "smtpPort": "25",
  40 + "timeout": "10000",
  41 + "enableTls": false,
  42 + "tlsVersion": "TLSv1.2",
  43 + "username": "",
  44 + "password": ""
  45 +}' );
... ...
... ... @@ -519,9 +519,9 @@ spring:
519 519 database-platform: "${SPRING_JPA_DATABASE_PLATFORM:org.hibernate.dialect.PostgreSQLDialect}"
520 520 datasource:
521 521 driverClassName: "${SPRING_DRIVER_CLASS_NAME:org.postgresql.Driver}"
522   - url: "${SPRING_DATASOURCE_URL:jdbc:postgresql://localhost:5432/thingsboard}"
  522 + url: "${SPRING_DATASOURCE_URL:jdbc:postgresql://101.133.234.90:5432/thingsboard-3.3.2}"
523 523 username: "${SPRING_DATASOURCE_USERNAME:postgres}"
524   - password: "${SPRING_DATASOURCE_PASSWORD:postgres}"
  524 + password: "${SPRING_DATASOURCE_PASSWORD:Pgsql@yunteng}"
525 525 hikari:
526 526 maximumPoolSize: "${SPRING_DATASOURCE_MAXIMUM_POOL_SIZE:16}"
527 527
... ...