Browse Source

Refactor database upgrade services

pull/4261/head
Viacheslav Klimov 5 years ago
committed by Andrew Shvayka
parent
commit
d369e296da
  1. 2
      application/src/main/java/org/thingsboard/server/service/install/AbstractSqlTsDatabaseUpgradeService.java
  2. 14
      application/src/main/java/org/thingsboard/server/service/install/PsqlTsDatabaseUpgradeService.java
  3. 10
      application/src/main/java/org/thingsboard/server/service/install/TimescaleTsDatabaseUpgradeService.java

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

@ -50,7 +50,7 @@ public abstract class AbstractSqlTsDatabaseUpgradeService {
@Autowired @Autowired
protected InstallScripts installScripts; protected InstallScripts installScripts;
protected abstract void loadSql(Connection conn, String fileName); protected abstract void loadSql(Connection conn, String fileName, String version);
protected void loadFunctions(Path sqlFile, Connection conn) throws Exception { protected void loadFunctions(Path sqlFile, Connection conn) throws Exception {
String sql = new String(Files.readAllBytes(sqlFile), StandardCharsets.UTF_8); String sql = new String(Files.readAllBytes(sqlFile), StandardCharsets.UTF_8);

14
application/src/main/java/org/thingsboard/server/service/install/PsqlTsDatabaseUpgradeService.java

@ -94,7 +94,7 @@ public class PsqlTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgradeSe
log.info("PostgreSQL version is valid!"); log.info("PostgreSQL version is valid!");
if (isOldSchema(conn, 2004003)) { if (isOldSchema(conn, 2004003)) {
log.info("Load upgrade functions ..."); log.info("Load upgrade functions ...");
loadSql(conn, LOAD_FUNCTIONS_SQL); loadSql(conn, LOAD_FUNCTIONS_SQL, "2.4.3");
log.info("Updating timeseries schema ..."); log.info("Updating timeseries schema ...");
executeQuery(conn, CALL_CREATE_PARTITION_TS_KV_TABLE); executeQuery(conn, CALL_CREATE_PARTITION_TS_KV_TABLE);
if (!partitionType.equals("INDEFINITE")) { if (!partitionType.equals("INDEFINITE")) {
@ -179,9 +179,9 @@ public class PsqlTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgradeSe
} }
log.info("Load TTL functions ..."); log.info("Load TTL functions ...");
loadSql(conn, LOAD_TTL_FUNCTIONS_SQL); loadSql(conn, LOAD_TTL_FUNCTIONS_SQL, "2.4.3");
log.info("Load Drop Partitions functions ..."); log.info("Load Drop Partitions functions ...");
loadSql(conn, LOAD_DROP_PARTITIONS_FUNCTIONS_SQL); loadSql(conn, LOAD_DROP_PARTITIONS_FUNCTIONS_SQL, "2.4.3");
executeQuery(conn, "UPDATE tb_schema_settings SET schema_version = 2005000"); executeQuery(conn, "UPDATE tb_schema_settings SET schema_version = 2005000");
@ -199,9 +199,9 @@ public class PsqlTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgradeSe
case "3.2.1": case "3.2.1":
try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) { try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
log.info("Load TTL functions ..."); log.info("Load TTL functions ...");
loadSql(conn, LOAD_TTL_FUNCTIONS_SQL); loadSql(conn, LOAD_TTL_FUNCTIONS_SQL, "2.4.3");
log.info("Load Drop Partitions functions ..."); log.info("Load Drop Partitions functions ...");
loadSql(conn, LOAD_DROP_PARTITIONS_FUNCTIONS_SQL); loadSql(conn, LOAD_DROP_PARTITIONS_FUNCTIONS_SQL, "2.4.3");
executeQuery(conn, "DROP PROCEDURE IF EXISTS cleanup_timeseries_by_ttl(character varying, bigint, bigint);"); executeQuery(conn, "DROP PROCEDURE IF EXISTS cleanup_timeseries_by_ttl(character varying, bigint, bigint);");
executeQuery(conn, "DROP FUNCTION IF EXISTS delete_asset_records_from_ts_kv(character varying, character varying, bigint);"); executeQuery(conn, "DROP FUNCTION IF EXISTS delete_asset_records_from_ts_kv(character varying, character varying, bigint);");
@ -244,8 +244,8 @@ public class PsqlTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgradeSe
} }
@Override @Override
protected void loadSql(Connection conn, String fileName) { protected void loadSql(Connection conn, String fileName, String version) {
Path schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "2.4.3", fileName); Path schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", version, fileName);
try { try {
loadFunctions(schemaUpdateFile, conn); loadFunctions(schemaUpdateFile, conn);
log.info("Functions successfully loaded!"); log.info("Functions successfully loaded!");

10
application/src/main/java/org/thingsboard/server/service/install/TimescaleTsDatabaseUpgradeService.java

@ -89,7 +89,7 @@ public class TimescaleTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgr
log.info("PostgreSQL version is valid!"); log.info("PostgreSQL version is valid!");
if (isOldSchema(conn, 2004003)) { if (isOldSchema(conn, 2004003)) {
log.info("Load upgrade functions ..."); log.info("Load upgrade functions ...");
loadSql(conn, LOAD_FUNCTIONS_SQL); loadSql(conn, LOAD_FUNCTIONS_SQL, "2.4.3");
log.info("Updating timescale schema ..."); log.info("Updating timescale schema ...");
executeQuery(conn, CALL_CREATE_TS_KV_LATEST_TABLE); executeQuery(conn, CALL_CREATE_TS_KV_LATEST_TABLE);
executeQuery(conn, CALL_CREATE_NEW_TENANT_TS_KV_TABLE); executeQuery(conn, CALL_CREATE_NEW_TENANT_TS_KV_TABLE);
@ -165,7 +165,7 @@ public class TimescaleTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgr
} }
log.info("Load TTL functions ..."); log.info("Load TTL functions ...");
loadSql(conn, LOAD_TTL_FUNCTIONS_SQL); loadSql(conn, LOAD_TTL_FUNCTIONS_SQL, "2.4.3");
executeQuery(conn, "UPDATE tb_schema_settings SET schema_version = 2005000"); executeQuery(conn, "UPDATE tb_schema_settings SET schema_version = 2005000");
log.info("schema timescale updated!"); log.info("schema timescale updated!");
@ -181,7 +181,7 @@ public class TimescaleTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgr
break; break;
case "3.2.1": case "3.2.1":
try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) { try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
loadSql(conn, LOAD_TTL_FUNCTIONS_SQL); loadSql(conn, LOAD_TTL_FUNCTIONS_SQL, "3.2.1");
} }
break; break;
default: default:
@ -205,8 +205,8 @@ public class TimescaleTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgr
} }
@Override @Override
protected void loadSql(Connection conn, String fileName) { protected void loadSql(Connection conn, String fileName, String version) {
Path schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "2.4.3", fileName); Path schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", version, fileName);
try { try {
loadFunctions(schemaUpdateFile, conn); loadFunctions(schemaUpdateFile, conn);
log.info("Functions successfully loaded!"); log.info("Functions successfully loaded!");

Loading…
Cancel
Save