Commit e67946e47f979323cdd0620b414cfe36ce99811b

Authored by Andrii Shvaika
1 parent 51966f00

Upgrade script implementation

... ... @@ -15,30 +15,32 @@
15 15 --
16 16
17 17 CREATE TABLE IF NOT EXISTS device_profile (
18   - id uuid NOT NULL CONSTRAINT device_profile_pkey PRIMARY KEY,
19   - created_time bigint NOT NULL,
20   - name varchar(255),
21   - type varchar(255),
22   - profile_data varchar,
23   - description varchar,
24   - search_text varchar(255),
25   - is_default boolean,
26   - tenant_id uuid,
27   - default_rule_chain_id uuid,
28   - CONSTRAINT device_profile_name_unq_key UNIQUE (tenant_id, name)
  18 + id uuid NOT NULL CONSTRAINT device_profile_pkey PRIMARY KEY,
  19 + created_time bigint NOT NULL,
  20 + name varchar(255),
  21 + type varchar(255),
  22 + transport_type varchar(255),
  23 + profile_data jsonb,
  24 + description varchar,
  25 + search_text varchar(255),
  26 + is_default boolean,
  27 + tenant_id uuid,
  28 + default_rule_chain_id uuid,
  29 + CONSTRAINT device_profile_name_unq_key UNIQUE (tenant_id, name),
  30 + CONSTRAINT fk_default_rule_chain_device_profile FOREIGN KEY (default_rule_chain_id) REFERENCES rule_chain(id)
29 31 );
30 32
31 33 CREATE TABLE IF NOT EXISTS tenant_profile (
32   - id uuid NOT NULL CONSTRAINT tenant_profile_pkey PRIMARY KEY,
33   - created_time bigint NOT NULL,
34   - name varchar(255),
35   - profile_data varchar,
36   - description varchar,
37   - search_text varchar(255),
38   - is_default boolean,
39   - isolated_tb_core boolean,
40   - isolated_tb_rule_engine boolean,
41   - CONSTRAINT tenant_profile_name_unq_key UNIQUE (name)
  34 + id uuid NOT NULL CONSTRAINT tenant_profile_pkey PRIMARY KEY,
  35 + created_time bigint NOT NULL,
  36 + name varchar(255),
  37 + profile_data jsonb,
  38 + description varchar,
  39 + search_text varchar(255),
  40 + is_default boolean,
  41 + isolated_tb_core boolean,
  42 + isolated_tb_rule_engine boolean,
  43 + CONSTRAINT tenant_profile_name_unq_key UNIQUE (name)
42 44 );
43 45
44 46 CREATE OR REPLACE PROCEDURE update_tenant_profiles()
... ... @@ -71,9 +73,9 @@ CREATE OR REPLACE PROCEDURE update_device_profiles()
71 73 LANGUAGE plpgsql AS
72 74 $$
73 75 BEGIN
74   - UPDATE device as d SET device_profile_id = p.id, device_data = '{"configuration":{"type":"DEFAULT"}}'
  76 + UPDATE device as d SET device_profile_id = p.id, device_data = '{"configuration":{"type":"DEFAULT"}, "transportConfiguration":{"type":"DEFAULT"}}'
75 77 FROM
76   - (SELECT id, tenant_id from device_profile WHERE is_default = true) as p
77   - WHERE d.device_profile_id IS NULL AND p.tenant_id = d.tenant_id;
  78 + (SELECT id, tenant_id, name from device_profile) as p
  79 + WHERE d.device_profile_id IS NULL AND p.tenant_id = d.tenant_id AND d.type = p.name;
78 80 END;
79 81 $$;
... ...
... ... @@ -20,11 +20,13 @@ import org.springframework.beans.factory.annotation.Autowired;
20 20 import org.springframework.beans.factory.annotation.Value;
21 21 import org.springframework.context.annotation.Profile;
22 22 import org.springframework.stereotype.Service;
  23 +import org.thingsboard.server.common.data.EntitySubtype;
23 24 import org.thingsboard.server.common.data.Tenant;
24 25 import org.thingsboard.server.common.data.page.PageData;
25 26 import org.thingsboard.server.common.data.page.PageLink;
26 27 import org.thingsboard.server.dao.dashboard.DashboardService;
27 28 import org.thingsboard.server.dao.device.DeviceProfileService;
  29 +import org.thingsboard.server.dao.device.DeviceService;
28 30 import org.thingsboard.server.dao.tenant.TenantService;
29 31 import org.thingsboard.server.service.install.sql.SqlDbHelper;
30 32
... ... @@ -39,6 +41,7 @@ import java.sql.SQLException;
39 41 import java.sql.SQLSyntaxErrorException;
40 42 import java.sql.SQLWarning;
41 43 import java.sql.Statement;
  44 +import java.util.List;
42 45
43 46 import static org.thingsboard.server.service.install.DatabaseHelper.ADDITIONAL_INFO;
44 47 import static org.thingsboard.server.service.install.DatabaseHelper.ASSIGNED_CUSTOMERS;
... ... @@ -88,6 +91,9 @@ public class SqlDatabaseUpgradeService implements DatabaseEntitiesUpgradeService
88 91 private TenantService tenantService;
89 92
90 93 @Autowired
  94 + private DeviceService deviceService;
  95 +
  96 + @Autowired
91 97 private DeviceProfileService deviceProfileService;
92 98
93 99
... ... @@ -348,7 +354,14 @@ public class SqlDatabaseUpgradeService implements DatabaseEntitiesUpgradeService
348 354 do {
349 355 pageData = tenantService.findTenants(pageLink);
350 356 for (Tenant tenant : pageData.getData()) {
  357 + List<EntitySubtype> deviceTypes = deviceService.findDeviceTypesByTenantId(tenant.getId()).get();
351 358 deviceProfileService.findOrCreateDefaultDeviceProfile(tenant.getId());
  359 + for (EntitySubtype deviceType : deviceTypes) {
  360 + try {
  361 + deviceProfileService.createDeviceProfile(tenant.getId(), deviceType.getType());
  362 + } catch (Exception e) {
  363 + }
  364 + }
352 365 }
353 366 pageLink = pageLink.nextPageLink();
354 367 } while (pageData.hasNext());
... ...
... ... @@ -326,7 +326,7 @@ public abstract class AbstractWebTest {
326 326 deviceProfileData.setTransportConfiguration(transportConfiguration);
327 327 deviceProfile.setProfileData(deviceProfileData);
328 328 deviceProfile.setDefault(false);
329   - deviceProfile.setDefaultRuleChainId(new RuleChainId(Uuids.timeBased()));
  329 + deviceProfile.setDefaultRuleChainId(null);
330 330 return deviceProfile;
331 331 }
332 332
... ...
... ... @@ -28,7 +28,8 @@ import java.util.Arrays;
28 28 @ClasspathSuite.ClassnameFilters({
29 29 // "org.thingsboard.server.controller.sql.WebsocketApiSqlTest",
30 30 // "org.thingsboard.server.controller.sql.EntityQueryControllerSqlTest",
31   - "org.thingsboard.server.controller.sql.*Test",
  31 + "org.thingsboard.server.controller.sql.DeviceProfileControllerSqlTest",
  32 +// "org.thingsboard.server.controller.sql.*Test",
32 33 })
33 34 public class ControllerSqlTestSuite {
34 35
... ...
... ... @@ -41,6 +41,8 @@ public interface DeviceProfileService {
41 41
42 42 DeviceProfile createDefaultDeviceProfile(TenantId tenantId);
43 43
  44 + DeviceProfile createDeviceProfile(TenantId tenantId, String profileName);
  45 +
44 46 DeviceProfile findDefaultDeviceProfile(TenantId tenantId);
45 47
46 48 DeviceProfileInfo findDefaultDeviceProfileInfo(TenantId tenantId);
... ...
... ... @@ -165,11 +165,21 @@ public class DeviceProfileServiceImpl extends AbstractEntityService implements D
165 165 @Override
166 166 public DeviceProfile createDefaultDeviceProfile(TenantId tenantId) {
167 167 log.trace("Executing createDefaultDeviceProfile tenantId [{}]", tenantId);
  168 + return doCreateDefaultDeviceProfile(tenantId, "Default", true);
  169 + }
  170 +
  171 + @Override
  172 + public DeviceProfile createDeviceProfile(TenantId tenantId, String profileName) {
  173 + log.trace("Executing createDefaultDeviceProfile tenantId [{}], profileName [{}]", tenantId, profileName);
  174 + return doCreateDefaultDeviceProfile(tenantId, profileName, false);
  175 + }
  176 +
  177 + private DeviceProfile doCreateDefaultDeviceProfile(TenantId tenantId, String profileName, boolean defaultProfile) {
168 178 validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
169 179 DeviceProfile deviceProfile = new DeviceProfile();
170 180 deviceProfile.setTenantId(tenantId);
171   - deviceProfile.setDefault(true);
172   - deviceProfile.setName("Default");
  181 + deviceProfile.setDefault(defaultProfile);
  182 + deviceProfile.setName(profileName);
173 183 deviceProfile.setType(DeviceProfileType.DEFAULT);
174 184 deviceProfile.setTransportType(DeviceTransportType.DEFAULT);
175 185 deviceProfile.setDescription("Default device profile");
... ...
... ... @@ -18,6 +18,7 @@ package org.thingsboard.server.dao.rule;
18 18 import com.google.common.util.concurrent.ListenableFuture;
19 19 import lombok.extern.slf4j.Slf4j;
20 20 import org.apache.commons.lang3.StringUtils;
  21 +import org.hibernate.exception.ConstraintViolationException;
21 22 import org.springframework.beans.factory.annotation.Autowired;
22 23 import org.springframework.stereotype.Service;
23 24 import org.thingsboard.server.common.data.BaseData;
... ... @@ -358,12 +359,21 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC
358 359 }
359 360
360 361 private void checkRuleNodesAndDelete(TenantId tenantId, RuleChainId ruleChainId) {
  362 + try{
  363 + ruleChainDao.removeById(tenantId, ruleChainId.getId());
  364 + } catch (Exception t) {
  365 + ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
  366 + if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("fk_default_rule_chain_device_profile")) {
  367 + throw new DataValidationException("The rule chain referenced by the device profiles cannot be deleted!");
  368 + } else {
  369 + throw t;
  370 + }
  371 + }
361 372 List<EntityRelation> nodeRelations = getRuleChainToNodeRelations(tenantId, ruleChainId);
362 373 for (EntityRelation relation : nodeRelations) {
363 374 deleteRuleNode(tenantId, relation.getTo());
364 375 }
365 376 deleteEntityRelations(tenantId, ruleChainId);
366   - ruleChainDao.removeById(tenantId, ruleChainId.getId());
367 377 }
368 378
369 379 private List<EntityRelation> getRuleChainToNodeRelations(TenantId tenantId, RuleChainId ruleChainId) {
... ...
... ... @@ -121,6 +121,30 @@ CREATE TABLE IF NOT EXISTS dashboard (
121 121 title varchar(255)
122 122 );
123 123
  124 +CREATE TABLE IF NOT EXISTS rule_chain (
  125 + id uuid NOT NULL CONSTRAINT rule_chain_pkey PRIMARY KEY,
  126 + created_time bigint NOT NULL,
  127 + additional_info varchar,
  128 + configuration varchar(10000000),
  129 + name varchar(255),
  130 + first_rule_node_id uuid,
  131 + root boolean,
  132 + debug_mode boolean,
  133 + search_text varchar(255),
  134 + tenant_id uuid
  135 +);
  136 +
  137 +CREATE TABLE IF NOT EXISTS rule_node (
  138 + id uuid NOT NULL CONSTRAINT rule_node_pkey PRIMARY KEY,
  139 + created_time bigint NOT NULL,
  140 + rule_chain_id uuid,
  141 + additional_info varchar,
  142 + configuration varchar(10000000),
  143 + type varchar(255),
  144 + name varchar(255),
  145 + debug_mode boolean,
  146 + search_text varchar(255)
  147 +);
124 148
125 149 CREATE TABLE IF NOT EXISTS device_profile (
126 150 id uuid NOT NULL CONSTRAINT device_profile_pkey PRIMARY KEY,
... ... @@ -134,7 +158,8 @@ CREATE TABLE IF NOT EXISTS device_profile (
134 158 is_default boolean,
135 159 tenant_id uuid,
136 160 default_rule_chain_id uuid,
137   - CONSTRAINT device_profile_name_unq_key UNIQUE (tenant_id, name)
  161 + CONSTRAINT device_profile_name_unq_key UNIQUE (tenant_id, name),
  162 + CONSTRAINT fk_default_rule_chain_device_profile FOREIGN KEY (default_rule_chain_id) REFERENCES rule_chain(id)
138 163 );
139 164
140 165 CREATE TABLE IF NOT EXISTS device (
... ... @@ -262,31 +287,6 @@ CREATE TABLE IF NOT EXISTS widgets_bundle (
262 287 title varchar(255)
263 288 );
264 289
265   -CREATE TABLE IF NOT EXISTS rule_chain (
266   - id uuid NOT NULL CONSTRAINT rule_chain_pkey PRIMARY KEY,
267   - created_time bigint NOT NULL,
268   - additional_info varchar,
269   - configuration varchar(10000000),
270   - name varchar(255),
271   - first_rule_node_id uuid,
272   - root boolean,
273   - debug_mode boolean,
274   - search_text varchar(255),
275   - tenant_id uuid
276   -);
277   -
278   -CREATE TABLE IF NOT EXISTS rule_node (
279   - id uuid NOT NULL CONSTRAINT rule_node_pkey PRIMARY KEY,
280   - created_time bigint NOT NULL,
281   - rule_chain_id uuid,
282   - additional_info varchar,
283   - configuration varchar(10000000),
284   - type varchar(255),
285   - name varchar(255),
286   - debug_mode boolean,
287   - search_text varchar(255)
288   -);
289   -
290 290 CREATE TABLE IF NOT EXISTS entity_view (
291 291 id uuid NOT NULL CONSTRAINT entity_view_pkey PRIMARY KEY,
292 292 created_time bigint NOT NULL,
... ...
... ... @@ -139,6 +139,31 @@ CREATE TABLE IF NOT EXISTS dashboard (
139 139 title varchar(255)
140 140 );
141 141
  142 +CREATE TABLE IF NOT EXISTS rule_chain (
  143 + id uuid NOT NULL CONSTRAINT rule_chain_pkey PRIMARY KEY,
  144 + created_time bigint NOT NULL,
  145 + additional_info varchar,
  146 + configuration varchar(10000000),
  147 + name varchar(255),
  148 + first_rule_node_id uuid,
  149 + root boolean,
  150 + debug_mode boolean,
  151 + search_text varchar(255),
  152 + tenant_id uuid
  153 +);
  154 +
  155 +CREATE TABLE IF NOT EXISTS rule_node (
  156 + id uuid NOT NULL CONSTRAINT rule_node_pkey PRIMARY KEY,
  157 + created_time bigint NOT NULL,
  158 + rule_chain_id uuid,
  159 + additional_info varchar,
  160 + configuration varchar(10000000),
  161 + type varchar(255),
  162 + name varchar(255),
  163 + debug_mode boolean,
  164 + search_text varchar(255)
  165 +);
  166 +
142 167 CREATE TABLE IF NOT EXISTS device_profile (
143 168 id uuid NOT NULL CONSTRAINT device_profile_pkey PRIMARY KEY,
144 169 created_time bigint NOT NULL,
... ... @@ -151,7 +176,8 @@ CREATE TABLE IF NOT EXISTS device_profile (
151 176 is_default boolean,
152 177 tenant_id uuid,
153 178 default_rule_chain_id uuid,
154   - CONSTRAINT device_profile_name_unq_key UNIQUE (tenant_id, name)
  179 + CONSTRAINT device_profile_name_unq_key UNIQUE (tenant_id, name),
  180 + CONSTRAINT fk_default_rule_chain_device_profile FOREIGN KEY (default_rule_chain_id) REFERENCES rule_chain(id)
155 181 );
156 182
157 183 CREATE TABLE IF NOT EXISTS device (
... ... @@ -286,31 +312,6 @@ CREATE TABLE IF NOT EXISTS widgets_bundle (
286 312 title varchar(255)
287 313 );
288 314
289   -CREATE TABLE IF NOT EXISTS rule_chain (
290   - id uuid NOT NULL CONSTRAINT rule_chain_pkey PRIMARY KEY,
291   - created_time bigint NOT NULL,
292   - additional_info varchar,
293   - configuration varchar(10000000),
294   - name varchar(255),
295   - first_rule_node_id uuid,
296   - root boolean,
297   - debug_mode boolean,
298   - search_text varchar(255),
299   - tenant_id uuid
300   -);
301   -
302   -CREATE TABLE IF NOT EXISTS rule_node (
303   - id uuid NOT NULL CONSTRAINT rule_node_pkey PRIMARY KEY,
304   - created_time bigint NOT NULL,
305   - rule_chain_id uuid,
306   - additional_info varchar,
307   - configuration varchar(10000000),
308   - type varchar(255),
309   - name varchar(255),
310   - debug_mode boolean,
311   - search_text varchar(255)
312   -);
313   -
314 315 CREATE TABLE IF NOT EXISTS entity_view (
315 316 id uuid NOT NULL CONSTRAINT entity_view_pkey PRIMARY KEY,
316 317 created_time bigint NOT NULL,
... ...
... ... @@ -41,6 +41,7 @@ import org.thingsboard.server.common.data.id.HasId;
41 41 import org.thingsboard.server.common.data.id.RuleChainId;
42 42 import org.thingsboard.server.common.data.id.TenantId;
43 43 import org.thingsboard.server.common.data.id.UUIDBased;
  44 +import org.thingsboard.server.common.data.rule.RuleChain;
44 45 import org.thingsboard.server.dao.alarm.AlarmService;
45 46 import org.thingsboard.server.dao.asset.AssetService;
46 47 import org.thingsboard.server.dao.audit.AuditLogLevelFilter;
... ... @@ -208,7 +209,7 @@ public abstract class AbstractServiceTest {
208 209 deviceProfileData.setTransportConfiguration(transportConfiguration);
209 210 deviceProfile.setProfileData(deviceProfileData);
210 211 deviceProfile.setDefault(false);
211   - deviceProfile.setDefaultRuleChainId(new RuleChainId(Uuids.timeBased()));
  212 + deviceProfile.setDefaultRuleChainId(null);
212 213 return deviceProfile;
213 214 }
214 215
... ...
... ... @@ -18,9 +18,9 @@ DROP TABLE IF EXISTS ts_kv_latest;
18 18 DROP TABLE IF EXISTS user_credentials;
19 19 DROP TABLE IF EXISTS widget_type;
20 20 DROP TABLE IF EXISTS widgets_bundle;
21   -DROP TABLE IF EXISTS rule_node;
22   -DROP TABLE IF EXISTS rule_chain;
23 21 DROP TABLE IF EXISTS entity_view;
24 22 DROP TABLE IF EXISTS device_profile;
25 23 DROP TABLE IF EXISTS tenant_profile;
  24 +DROP TABLE IF EXISTS rule_node;
  25 +DROP TABLE IF EXISTS rule_chain;
26 26 DROP FUNCTION IF EXISTS to_uuid;
... ...
... ... @@ -18,9 +18,9 @@ DROP TABLE IF EXISTS ts_kv_dictionary;
18 18 DROP TABLE IF EXISTS user_credentials;
19 19 DROP TABLE IF EXISTS widget_type;
20 20 DROP TABLE IF EXISTS widgets_bundle;
21   -DROP TABLE IF EXISTS rule_node;
22   -DROP TABLE IF EXISTS rule_chain;
23 21 DROP TABLE IF EXISTS entity_view;
24 22 DROP TABLE IF EXISTS device_profile;
25 23 DROP TABLE IF EXISTS tenant_profile;
  24 +DROP TABLE IF EXISTS rule_node;
  25 +DROP TABLE IF EXISTS rule_chain;
26 26 DROP TABLE IF EXISTS tb_schema_settings;
... ...