Browse Source

Merge branch 'ViacheslavKlimov-fix/ota-device-profile'

pull/6214/head
Igor Kulikov 4 years ago
parent
commit
464bf04042
  1. 27
      application/src/main/data/upgrade/3.3.3/schema_update.sql
  2. 2
      application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java
  3. 2
      application/src/main/java/org/thingsboard/server/service/install/SqlDatabaseUpgradeService.java
  4. 6
      dao/src/main/resources/sql/schema-entities.sql
  5. 22
      dao/src/test/java/org/thingsboard/server/dao/service/AbstractServiceTest.java
  6. 17
      dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceProfileServiceTest.java
  7. 1
      dao/src/test/java/org/thingsboard/server/dao/service/BaseOtaPackageServiceTest.java
  8. 2
      dao/src/test/resources/sql/system-test-psql.sql
  9. 2
      dao/src/test/resources/sql/system-test.sql
  10. 4
      ui-ngx/src/assets/locale/locale.constant-en_US.json

27
application/src/main/data/upgrade/3.3.3/schema_update.sql

@ -0,0 +1,27 @@
--
-- Copyright © 2016-2022 The Thingsboard Authors
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
DO
$$
BEGIN
IF NOT EXISTS(SELECT 1 FROM pg_constraint WHERE conname = 'fk_device_profile_ota_package') THEN
ALTER TABLE ota_package
ADD CONSTRAINT fk_device_profile_ota_package
FOREIGN KEY (device_profile_id) REFERENCES device_profile (id)
ON DELETE CASCADE;
END IF;
END;
$$;

2
application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java

@ -216,8 +216,8 @@ public class ThingsboardInstallService {
dataUpdateService.updateData("3.3.2");
case "3.3.3":
log.info("Upgrading ThingsBoard from version 3.3.3 to 3.3.4 ...");
log.info("Updating system data...");
databaseEntitiesUpgradeService.upgradeDatabase("3.3.3");
log.info("Updating system data...");
systemDataLoaderService.updateSystemWidgets();
break;

2
application/src/main/java/org/thingsboard/server/service/install/SqlDatabaseUpgradeService.java

@ -524,6 +524,8 @@ public class SqlDatabaseUpgradeService implements DatabaseEntitiesUpgradeService
log.info("Updating TTL cleanup procedure for the event table...");
schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "3.3.3", "schema_event_ttl_procedure.sql");
loadSql(schemaUpdateFile, conn);
schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "3.3.3", SCHEMA_UPDATE_SQL);
loadSql(schemaUpdateFile, conn);
log.info("Updating schema settings...");
conn.createStatement().execute("UPDATE tb_schema_settings SET schema_version = 3003004;");

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

@ -213,7 +213,6 @@ CREATE TABLE IF NOT EXISTS ota_package (
additional_info varchar,
search_text varchar(255),
CONSTRAINT ota_package_tenant_title_version_unq_key UNIQUE (tenant_id, title, version)
-- CONSTRAINT fk_device_profile_firmware FOREIGN KEY (device_profile_id) REFERENCES device_profile(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS device_profile (
@ -243,6 +242,11 @@ CREATE TABLE IF NOT EXISTS device_profile (
CONSTRAINT fk_software_device_profile FOREIGN KEY (software_id) REFERENCES ota_package(id)
);
ALTER TABLE ota_package
ADD CONSTRAINT fk_device_profile_ota_package
FOREIGN KEY (device_profile_id) REFERENCES device_profile (id)
ON DELETE CASCADE;
-- We will use one-to-many relation in the first release and extend this feature in case of user requests
-- CREATE TABLE IF NOT EXISTS device_profile_firmware (
-- device_profile_id uuid NOT NULL,

22
dao/src/test/java/org/thingsboard/server/dao/service/AbstractServiceTest.java

@ -33,14 +33,18 @@ import org.thingsboard.server.common.data.DeviceProfileType;
import org.thingsboard.server.common.data.DeviceTransportType;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.Event;
import org.thingsboard.server.common.data.OtaPackage;
import org.thingsboard.server.common.data.Tenant;
import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileConfiguration;
import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileTransportConfiguration;
import org.thingsboard.server.common.data.device.profile.DeviceProfileData;
import org.thingsboard.server.common.data.edge.Edge;
import org.thingsboard.server.common.data.id.DeviceProfileId;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.HasId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.ota.ChecksumAlgorithm;
import org.thingsboard.server.common.data.ota.OtaPackageType;
import org.thingsboard.server.dao.alarm.AlarmService;
import org.thingsboard.server.dao.asset.AssetService;
import org.thingsboard.server.dao.audit.AuditLogLevelFilter;
@ -70,6 +74,7 @@ import org.thingsboard.server.dao.widget.WidgetTypeService;
import org.thingsboard.server.dao.widget.WidgetsBundleService;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
@ -251,4 +256,21 @@ public abstract class AbstractServiceTest {
edge.setRoutingKey(RandomStringUtils.randomAlphanumeric(20));
return edge;
}
protected OtaPackage constructDefaultOtaPackage(TenantId tenantId, DeviceProfileId deviceProfileId) {
OtaPackage firmware = new OtaPackage();
firmware.setTenantId(tenantId);
firmware.setDeviceProfileId(deviceProfileId);
firmware.setType(OtaPackageType.FIRMWARE);
firmware.setTitle("My firmware");
firmware.setVersion("3.3.3");
firmware.setFileName("filename.txt");
firmware.setContentType("text/plain");
firmware.setChecksumAlgorithm(ChecksumAlgorithm.SHA256);
firmware.setChecksum("4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a");
firmware.setData(ByteBuffer.wrap(new byte[]{1}));
firmware.setDataSize(1L);
return firmware;
}
}

17
dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceProfileServiceTest.java

@ -45,6 +45,7 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.thingsboard.server.common.data.ota.OtaPackageType.FIRMWARE;
public abstract class BaseDeviceProfileServiceTest extends AbstractServiceTest {
@ -252,6 +253,22 @@ public abstract class BaseDeviceProfileServiceTest extends AbstractServiceTest {
deviceProfileService.deleteDeviceProfile(tenantId, savedDeviceProfile.getId());
}
@Test
public void testDeleteDeviceProfileWithExistingOta_cascadeDelete() {
DeviceProfile deviceProfile = this.createDeviceProfile(tenantId, "Device Profile");
deviceProfile = deviceProfileService.saveDeviceProfile(deviceProfile);
OtaPackage otaPackage = constructDefaultOtaPackage(tenantId, deviceProfile.getId());
otaPackage = otaPackageService.saveOtaPackage(otaPackage);
assertThat(deviceProfileService.findDeviceProfileById(tenantId, deviceProfile.getId())).isNotNull();
assertThat(otaPackageService.findOtaPackageById(tenantId, otaPackage.getId())).isNotNull();
deviceProfileService.deleteDeviceProfile(tenantId, deviceProfile.getId());
assertThat(deviceProfileService.findDeviceProfileById(tenantId, deviceProfile.getId())).isNull();
assertThat(otaPackageService.findOtaPackageById(tenantId, otaPackage.getId())).isNull();
}
@Test
public void testDeleteDeviceProfile() {
DeviceProfile deviceProfile = this.createDeviceProfile(tenantId, "Device Profile");

1
dao/src/test/java/org/thingsboard/server/dao/service/BaseOtaPackageServiceTest.java

@ -488,7 +488,6 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest {
otaPackageService.deleteOtaPackage(tenantId, savedFirmware.getId());
} finally {
deviceProfileService.deleteDeviceProfile(tenantId, savedDeviceProfile.getId());
otaPackageService.deleteOtaPackage(tenantId, savedFirmware.getId());
}
}

2
dao/src/test/resources/sql/system-test-psql.sql

@ -1,2 +1,2 @@
--PostgreSQL specific truncate to fit constraints
TRUNCATE TABLE device_credentials, device, device_profile, rule_node_state, rule_node, rule_chain;
TRUNCATE TABLE device_credentials, device, device_profile, ota_package, rule_node_state, rule_node, rule_chain;

2
dao/src/test/resources/sql/system-test.sql

@ -1,6 +1,6 @@
TRUNCATE TABLE device_credentials;
TRUNCATE TABLE device;
TRUNCATE TABLE device_profile;
TRUNCATE TABLE device_profile CASCADE;
TRUNCATE TABLE rule_node_state;
TRUNCATE TABLE rule_node;
TRUNCATE TABLE rule_chain;

4
ui-ngx/src/assets/locale/locale.constant-en_US.json

@ -1119,9 +1119,9 @@
"mobile-dashboard-hint": "Used by mobile application as a device details dashboard",
"select-queue-hint": "Select from a drop-down list.",
"delete-device-profile-title": "Are you sure you want to delete the device profile '{{deviceProfileName}}'?",
"delete-device-profile-text": "Be careful, after the confirmation the device profile and all related data will become unrecoverable.",
"delete-device-profile-text": "Be careful, after the confirmation the device profile and all related data including associated OTA updates will become unrecoverable.",
"delete-device-profiles-title": "Are you sure you want to delete { count, plural, 1 {1 device profile} other {# device profiles} }?",
"delete-device-profiles-text": "Be careful, after the confirmation all selected device profiles will be removed and all related data will become unrecoverable.",
"delete-device-profiles-text": "Be careful, after the confirmation all selected device profiles will be removed and all related data including associated OTA updates will become unrecoverable.",
"set-default-device-profile-title": "Are you sure you want to make the device profile '{{deviceProfileName}}' default?",
"set-default-device-profile-text": "After the confirmation the device profile will be marked as default and will be used for new devices with no profile specified.",
"no-device-profiles-found": "No device profiles found.",

Loading…
Cancel
Save