|
|
@ -15,6 +15,8 @@ |
|
|
*/ |
|
|
*/ |
|
|
package org.thingsboard.server.service.install; |
|
|
package org.thingsboard.server.service.install; |
|
|
|
|
|
|
|
|
|
|
|
import com.google.common.util.concurrent.Futures; |
|
|
|
|
|
import com.google.common.util.concurrent.ListenableFuture; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.apache.commons.collections.CollectionUtils; |
|
|
import org.apache.commons.collections.CollectionUtils; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
@ -32,14 +34,14 @@ import org.thingsboard.server.common.data.queue.ProcessingStrategyType; |
|
|
import org.thingsboard.server.common.data.queue.Queue; |
|
|
import org.thingsboard.server.common.data.queue.Queue; |
|
|
import org.thingsboard.server.common.data.queue.SubmitStrategy; |
|
|
import org.thingsboard.server.common.data.queue.SubmitStrategy; |
|
|
import org.thingsboard.server.common.data.queue.SubmitStrategyType; |
|
|
import org.thingsboard.server.common.data.queue.SubmitStrategyType; |
|
|
|
|
|
import org.thingsboard.server.dao.asset.AssetDao; |
|
|
import org.thingsboard.server.dao.asset.AssetProfileService; |
|
|
import org.thingsboard.server.dao.asset.AssetProfileService; |
|
|
import org.thingsboard.server.dao.asset.AssetService; |
|
|
|
|
|
import org.thingsboard.server.dao.dashboard.DashboardService; |
|
|
import org.thingsboard.server.dao.dashboard.DashboardService; |
|
|
import org.thingsboard.server.dao.device.DeviceProfileService; |
|
|
import org.thingsboard.server.dao.device.DeviceProfileService; |
|
|
import org.thingsboard.server.dao.device.DeviceService; |
|
|
import org.thingsboard.server.dao.device.DeviceService; |
|
|
|
|
|
import org.thingsboard.server.common.data.util.TbPair; |
|
|
import org.thingsboard.server.dao.queue.QueueService; |
|
|
import org.thingsboard.server.dao.queue.QueueService; |
|
|
import org.thingsboard.server.dao.rule.RuleChainService; |
|
|
import org.thingsboard.server.dao.sql.tenant.TenantRepository; |
|
|
import org.thingsboard.server.dao.tenant.TenantProfileService; |
|
|
|
|
|
import org.thingsboard.server.dao.tenant.TenantService; |
|
|
import org.thingsboard.server.dao.tenant.TenantService; |
|
|
import org.thingsboard.server.dao.usagerecord.ApiUsageStateService; |
|
|
import org.thingsboard.server.dao.usagerecord.ApiUsageStateService; |
|
|
import org.thingsboard.server.queue.settings.TbRuleEngineQueueConfiguration; |
|
|
import org.thingsboard.server.queue.settings.TbRuleEngineQueueConfiguration; |
|
|
@ -56,7 +58,9 @@ import java.sql.SQLException; |
|
|
import java.sql.SQLSyntaxErrorException; |
|
|
import java.sql.SQLSyntaxErrorException; |
|
|
import java.sql.SQLWarning; |
|
|
import java.sql.SQLWarning; |
|
|
import java.sql.Statement; |
|
|
import java.sql.Statement; |
|
|
|
|
|
import java.util.ArrayList; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
|
|
|
import java.util.UUID; |
|
|
import java.util.concurrent.TimeUnit; |
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
|
import static org.thingsboard.server.service.install.DatabaseHelper.ADDITIONAL_INFO; |
|
|
import static org.thingsboard.server.service.install.DatabaseHelper.ADDITIONAL_INFO; |
|
|
@ -106,11 +110,14 @@ public class SqlDatabaseUpgradeService implements DatabaseEntitiesUpgradeService |
|
|
@Autowired |
|
|
@Autowired |
|
|
private TenantService tenantService; |
|
|
private TenantService tenantService; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private TenantRepository tenantRepository; |
|
|
|
|
|
|
|
|
@Autowired |
|
|
@Autowired |
|
|
private DeviceService deviceService; |
|
|
private DeviceService deviceService; |
|
|
|
|
|
|
|
|
@Autowired |
|
|
@Autowired |
|
|
private AssetService assetService; |
|
|
private AssetDao assetDao; |
|
|
|
|
|
|
|
|
@Autowired |
|
|
@Autowired |
|
|
private DeviceProfileService deviceProfileService; |
|
|
private DeviceProfileService deviceProfileService; |
|
|
@ -129,10 +136,7 @@ public class SqlDatabaseUpgradeService implements DatabaseEntitiesUpgradeService |
|
|
private TbRuleEngineQueueConfigService queueConfig; |
|
|
private TbRuleEngineQueueConfigService queueConfig; |
|
|
|
|
|
|
|
|
@Autowired |
|
|
@Autowired |
|
|
private RuleChainService ruleChainService; |
|
|
private DbUpgradeExecutorService dbUpgradeExecutor; |
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private TenantProfileService tenantProfileService; |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void upgradeDatabase(String fromVersion) throws Exception { |
|
|
public void upgradeDatabase(String fromVersion) throws Exception { |
|
|
@ -620,26 +624,45 @@ public class SqlDatabaseUpgradeService implements DatabaseEntitiesUpgradeService |
|
|
schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "3.4.1", "schema_update_before.sql"); |
|
|
schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "3.4.1", "schema_update_before.sql"); |
|
|
loadSql(schemaUpdateFile, conn); |
|
|
loadSql(schemaUpdateFile, conn); |
|
|
|
|
|
|
|
|
|
|
|
conn.createStatement().execute("DELETE FROM asset a WHERE NOT exists(SELECT id FROM tenant WHERE id = a.tenant_id);"); |
|
|
|
|
|
|
|
|
log.info("Creating default asset profiles..."); |
|
|
log.info("Creating default asset profiles..."); |
|
|
PageLink pageLink = new PageLink(100); |
|
|
|
|
|
PageData<Tenant> pageData; |
|
|
PageLink pageLink = new PageLink(1000); |
|
|
|
|
|
PageData<TenantId> tenantIds; |
|
|
do { |
|
|
do { |
|
|
pageData = tenantService.findTenants(pageLink); |
|
|
List<ListenableFuture<?>> futures = new ArrayList<>(); |
|
|
for (Tenant tenant : pageData.getData()) { |
|
|
tenantIds = tenantService.findTenantsIds(pageLink); |
|
|
List<EntitySubtype> assetTypes = assetService.findAssetTypesByTenantId(tenant.getId()).get(); |
|
|
for (TenantId tenantId : tenantIds.getData()) { |
|
|
try { |
|
|
futures.add(dbUpgradeExecutor.submit(() -> { |
|
|
assetProfileService.createDefaultAssetProfile(tenant.getId()); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
} |
|
|
|
|
|
for (EntitySubtype assetType : assetTypes) { |
|
|
|
|
|
try { |
|
|
try { |
|
|
assetProfileService.findOrCreateAssetProfile(tenant.getId(), assetType.getType()); |
|
|
assetProfileService.createDefaultAssetProfile(tenantId); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) {} |
|
|
} |
|
|
})); |
|
|
|
|
|
} |
|
|
|
|
|
Futures.allAsList(futures).get(); |
|
|
|
|
|
pageLink = pageLink.nextPageLink(); |
|
|
|
|
|
} while (tenantIds.hasNext()); |
|
|
|
|
|
|
|
|
|
|
|
pageLink = new PageLink(1000); |
|
|
|
|
|
PageData<TbPair<UUID, String>> pairs; |
|
|
|
|
|
do { |
|
|
|
|
|
List<ListenableFuture<?>> futures = new ArrayList<>(); |
|
|
|
|
|
pairs = assetDao.getAllAssetTypes(pageLink); |
|
|
|
|
|
for (TbPair<UUID, String> pair : pairs.getData()) { |
|
|
|
|
|
TenantId tenantId = new TenantId(pair.getFirst()); |
|
|
|
|
|
String assetType = pair.getSecond(); |
|
|
|
|
|
if (!"default".equals(assetType)) { |
|
|
|
|
|
futures.add(dbUpgradeExecutor.submit(() -> { |
|
|
|
|
|
try { |
|
|
|
|
|
assetProfileService.findOrCreateAssetProfile(tenantId, assetType); |
|
|
|
|
|
} catch (Exception e) {} |
|
|
|
|
|
})); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
Futures.allAsList(futures).get(); |
|
|
pageLink = pageLink.nextPageLink(); |
|
|
pageLink = pageLink.nextPageLink(); |
|
|
} while (pageData.hasNext()); |
|
|
} while (pairs.hasNext()); |
|
|
|
|
|
|
|
|
log.info("Updating asset profiles..."); |
|
|
log.info("Updating asset profiles..."); |
|
|
conn.createStatement().execute("call update_asset_profiles()"); |
|
|
conn.createStatement().execute("call update_asset_profiles()"); |
|
|
@ -728,5 +751,4 @@ public class SqlDatabaseUpgradeService implements DatabaseEntitiesUpgradeService |
|
|
return queue; |
|
|
return queue; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|