Skip to content

Commit c7e678b

Browse files
refactor DefaultDatabaseSchemaSettingsService
1 parent 474d1f3 commit c7e678b

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

application/src/main/java/org/thingsboard/mqtt/broker/service/install/DefaultDatabaseSchemaSettingsService.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import lombok.RequiredArgsConstructor;
1919
import lombok.extern.slf4j.Slf4j;
20-
import org.springframework.boot.info.BuildProperties;
2120
import org.springframework.context.annotation.Profile;
2221
import org.springframework.jdbc.core.JdbcTemplate;
2322
import org.springframework.stereotype.Service;
@@ -31,12 +30,11 @@
3130
@RequiredArgsConstructor
3231
public class DefaultDatabaseSchemaSettingsService implements DatabaseSchemaSettingsService {
3332

34-
private static final String CURRENT_PRODUCT = "CE";
3533
// This list should include all versions that are compatible for the upgrade.
3634
// The compatibility cycle usually breaks when we have some scripts written in Java that may not work after new release.
3735
private static final List<String> SUPPORTED_VERSIONS_FOR_UPGRADE = List.of("2.2.0");
3836

39-
private final BuildProperties buildProperties;
37+
private final ProjectInfo projectInfo;
4038
private final JdbcTemplate jdbcTemplate;
4139

4240
private String packageSchemaVersion;
@@ -50,8 +48,8 @@ public void validateSchemaSettings() {
5048
}
5149

5250
String product = getProductFromDb();
53-
if (!CURRENT_PRODUCT.equals(product)) {
54-
onSchemaSettingsError(String.format("Upgrade failed: can't upgrade TBMQ %s database using TBMQ %s.", product, CURRENT_PRODUCT));
51+
if (!projectInfo.getProductType().equals(product)) {
52+
onSchemaSettingsError(String.format("Upgrade failed: can't upgrade TBMQ %s database using TBMQ %s.", product, projectInfo.getProductType()));
5553
}
5654

5755
String dbSchemaVersion = getDbSchemaVersion();
@@ -69,7 +67,7 @@ public void validateSchemaSettings() {
6967
public void createSchemaSettings() {
7068
Long schemaVersion = getSchemaVersionFromDb();
7169
if (schemaVersion == null) {
72-
jdbcTemplate.execute("INSERT INTO tb_schema_settings (schema_version, product) VALUES (" + getPackageSchemaVersionForDb() + ", '" + CURRENT_PRODUCT + "')");
70+
jdbcTemplate.execute("INSERT INTO tb_schema_settings (schema_version, product) VALUES (" + getPackageSchemaVersionForDb() + ", '" + projectInfo.getProductType() + "')");
7371
}
7472
}
7573

@@ -81,7 +79,7 @@ public void updateSchemaVersion() {
8179
@Override
8280
public String getPackageSchemaVersion() {
8381
if (packageSchemaVersion == null) {
84-
packageSchemaVersion = buildProperties.getVersion().replaceAll("[^\\d.]", "");
82+
packageSchemaVersion = projectInfo.getProjectVersion();
8583
}
8684
return packageSchemaVersion;
8785
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright © 2016-2025 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+
package org.thingsboard.mqtt.broker.service.install;
17+
18+
import lombok.RequiredArgsConstructor;
19+
import org.springframework.boot.info.BuildProperties;
20+
import org.springframework.stereotype.Component;
21+
22+
@Component
23+
@RequiredArgsConstructor
24+
public class ProjectInfo {
25+
26+
private final BuildProperties buildProperties;
27+
28+
public String getProjectVersion() {
29+
return buildProperties.getVersion().replaceAll("[^\\d.]", "");
30+
}
31+
32+
public String getProductType() {
33+
return "CE";
34+
}
35+
36+
}

0 commit comments

Comments
 (0)