Browse Source
* refactored sqlUpgradeService implementation * fix typo * change string constant name * add ability to re-init chunks for upgrade timescalepull/2412/head
committed by
Andrew Shvayka
22 changed files with 449 additions and 338 deletions
@ -0,0 +1,124 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2020 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. |
||||
|
*/ |
||||
|
package org.thingsboard.server.service.install; |
||||
|
|
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.beans.factory.annotation.Value; |
||||
|
|
||||
|
import java.nio.charset.StandardCharsets; |
||||
|
import java.nio.file.Files; |
||||
|
import java.nio.file.Path; |
||||
|
import java.sql.CallableStatement; |
||||
|
import java.sql.Connection; |
||||
|
import java.sql.SQLException; |
||||
|
import java.sql.Types; |
||||
|
|
||||
|
@Slf4j |
||||
|
public abstract class AbstractSqlTsDatabaseUpgradeService { |
||||
|
|
||||
|
protected static final String CALL_REGEX = "call "; |
||||
|
protected static final String CHECK_VERSION = "check_version()"; |
||||
|
protected static final String DROP_TABLE = "DROP TABLE "; |
||||
|
protected static final String DROP_FUNCTION_IF_EXISTS = "DROP FUNCTION IF EXISTS "; |
||||
|
|
||||
|
private static final String CALL_CHECK_VERSION = CALL_REGEX + CHECK_VERSION; |
||||
|
|
||||
|
|
||||
|
private static final String FUNCTION = "function: {}"; |
||||
|
private static final String DROP_STATEMENT = "drop statement: {}"; |
||||
|
private static final String QUERY = "query: {}"; |
||||
|
private static final String SUCCESSFULLY_EXECUTED = "Successfully executed "; |
||||
|
private static final String FAILED_TO_EXECUTE = "Failed to execute "; |
||||
|
private static final String FAILED_DUE_TO = " due to: {}"; |
||||
|
|
||||
|
protected static final String SUCCESSFULLY_EXECUTED_FUNCTION = SUCCESSFULLY_EXECUTED + FUNCTION; |
||||
|
protected static final String FAILED_TO_EXECUTE_FUNCTION_DUE_TO = FAILED_TO_EXECUTE + FUNCTION + FAILED_DUE_TO; |
||||
|
|
||||
|
protected static final String SUCCESSFULLY_EXECUTED_DROP_STATEMENT = SUCCESSFULLY_EXECUTED + DROP_STATEMENT; |
||||
|
protected static final String FAILED_TO_EXECUTE_DROP_STATEMENT = FAILED_TO_EXECUTE + DROP_STATEMENT + FAILED_DUE_TO; |
||||
|
|
||||
|
protected static final String SUCCESSFULLY_EXECUTED_QUERY = SUCCESSFULLY_EXECUTED + QUERY; |
||||
|
protected static final String FAILED_TO_EXECUTE_QUERY = FAILED_TO_EXECUTE + QUERY + FAILED_DUE_TO; |
||||
|
|
||||
|
@Value("${spring.datasource.url}") |
||||
|
protected String dbUrl; |
||||
|
|
||||
|
@Value("${spring.datasource.username}") |
||||
|
protected String dbUserName; |
||||
|
|
||||
|
@Value("${spring.datasource.password}") |
||||
|
protected String dbPassword; |
||||
|
|
||||
|
@Autowired |
||||
|
protected InstallScripts installScripts; |
||||
|
|
||||
|
protected abstract void loadSql(Connection conn); |
||||
|
|
||||
|
protected void loadFunctions(Path sqlFile, Connection conn) throws Exception { |
||||
|
String sql = new String(Files.readAllBytes(sqlFile), StandardCharsets.UTF_8); |
||||
|
conn.createStatement().execute(sql); //NOSONAR, ignoring because method used to execute thingsboard database upgrade script
|
||||
|
} |
||||
|
|
||||
|
protected boolean checkVersion(Connection conn) { |
||||
|
log.info("Check the current PostgreSQL version..."); |
||||
|
boolean versionValid = false; |
||||
|
try { |
||||
|
CallableStatement callableStatement = conn.prepareCall("{? = " + CALL_CHECK_VERSION + " }"); |
||||
|
callableStatement.registerOutParameter(1, Types.BOOLEAN); |
||||
|
callableStatement.execute(); |
||||
|
versionValid = callableStatement.getBoolean(1); |
||||
|
callableStatement.close(); |
||||
|
} catch (Exception e) { |
||||
|
log.info("Failed to check current PostgreSQL version due to: {}", e.getMessage()); |
||||
|
} |
||||
|
return versionValid; |
||||
|
} |
||||
|
|
||||
|
protected void executeFunction(Connection conn, String query) { |
||||
|
log.info("{} ... ", query); |
||||
|
try { |
||||
|
CallableStatement callableStatement = conn.prepareCall("{" + query + "}"); |
||||
|
callableStatement.execute(); |
||||
|
callableStatement.close(); |
||||
|
log.info(SUCCESSFULLY_EXECUTED_FUNCTION, query.replace(CALL_REGEX, "")); |
||||
|
Thread.sleep(2000); |
||||
|
} catch (Exception e) { |
||||
|
log.info(FAILED_TO_EXECUTE_FUNCTION_DUE_TO, query, e.getMessage()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
protected void executeDropStatement(Connection conn, String query) { |
||||
|
try { |
||||
|
conn.createStatement().execute(query); //NOSONAR, ignoring because method used to execute thingsboard database upgrade script
|
||||
|
log.info(SUCCESSFULLY_EXECUTED_DROP_STATEMENT, query); |
||||
|
Thread.sleep(5000); |
||||
|
} catch (InterruptedException | SQLException e) { |
||||
|
log.info(FAILED_TO_EXECUTE_DROP_STATEMENT, query, e.getMessage()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
protected void executeQuery(Connection conn, String query) { |
||||
|
try { |
||||
|
conn.createStatement().execute(query); //NOSONAR, ignoring because method used to execute thingsboard database upgrade script
|
||||
|
log.info(SUCCESSFULLY_EXECUTED_QUERY, query); |
||||
|
Thread.sleep(5000); |
||||
|
} catch (InterruptedException | SQLException e) { |
||||
|
log.info(FAILED_TO_EXECUTE_QUERY, query, e.getMessage()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -1,31 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2020 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. |
|
||||
*/ |
|
||||
package org.thingsboard.server.service.install; |
|
||||
|
|
||||
import org.springframework.context.annotation.Profile; |
|
||||
import org.springframework.stereotype.Service; |
|
||||
import org.thingsboard.server.dao.util.SqlDao; |
|
||||
import org.thingsboard.server.dao.util.TimescaleDBTsDao; |
|
||||
|
|
||||
@Service |
|
||||
@TimescaleDBTsDao |
|
||||
@Profile("install") |
|
||||
public class SqlTimescaleDatabaseSchemaService extends SqlAbstractDatabaseSchemaService |
|
||||
implements TsDatabaseSchemaService { |
|
||||
public SqlTimescaleDatabaseSchemaService() { |
|
||||
super("schema-timescale.sql", "schema-timescale-idx.sql"); |
|
||||
} |
|
||||
} |
|
||||
@ -1,147 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2020 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. |
|
||||
*/ |
|
||||
package org.thingsboard.server.service.install; |
|
||||
|
|
||||
import lombok.extern.slf4j.Slf4j; |
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||
import org.springframework.beans.factory.annotation.Value; |
|
||||
import org.springframework.context.annotation.Profile; |
|
||||
import org.springframework.stereotype.Service; |
|
||||
import org.thingsboard.server.dao.util.PsqlDao; |
|
||||
import org.thingsboard.server.dao.util.TimescaleDBTsDao; |
|
||||
|
|
||||
import java.nio.charset.StandardCharsets; |
|
||||
import java.nio.file.Files; |
|
||||
import java.nio.file.Path; |
|
||||
import java.nio.file.Paths; |
|
||||
import java.sql.CallableStatement; |
|
||||
import java.sql.Connection; |
|
||||
import java.sql.DriverManager; |
|
||||
import java.sql.SQLException; |
|
||||
import java.sql.Types; |
|
||||
|
|
||||
@Service |
|
||||
@Profile("install") |
|
||||
@Slf4j |
|
||||
@TimescaleDBTsDao |
|
||||
@PsqlDao |
|
||||
public class SqlTimescaleDatabaseUpgradeService implements DatabaseTsUpgradeService { |
|
||||
|
|
||||
private static final String CALL_REGEX = "call "; |
|
||||
private static final String LOAD_FUNCTIONS_SQL = "schema_update_timescale_ts.sql"; |
|
||||
private static final String CHECK_VERSION = CALL_REGEX + "check_version()"; |
|
||||
private static final String CREATE_TS_KV_LATEST_TABLE = CALL_REGEX + "create_ts_kv_latest_table()"; |
|
||||
private static final String CREATE_TENANT_TS_KV_TABLE_COPY = CALL_REGEX + "create_tenant_ts_kv_table_copy()"; |
|
||||
private static final String CREATE_TS_KV_DICTIONARY_TABLE = CALL_REGEX + "create_ts_kv_dictionary_table()"; |
|
||||
private static final String INSERT_INTO_DICTIONARY = CALL_REGEX + "insert_into_dictionary()"; |
|
||||
private static final String INSERT_INTO_TS_KV = CALL_REGEX + "insert_into_tenant_ts_kv()"; |
|
||||
private static final String INSERT_INTO_TS_KV_LATEST = CALL_REGEX + "insert_into_ts_kv_latest()"; |
|
||||
private static final String DROP_OLD_TS_KV_TABLE = "DROP TABLE tenant_ts_kv_old;"; |
|
||||
|
|
||||
@Value("${spring.datasource.url}") |
|
||||
private String dbUrl; |
|
||||
|
|
||||
@Value("${spring.datasource.username}") |
|
||||
private String dbUserName; |
|
||||
|
|
||||
@Value("${spring.datasource.password}") |
|
||||
private String dbPassword; |
|
||||
|
|
||||
@Autowired |
|
||||
private InstallScripts installScripts; |
|
||||
|
|
||||
@Override |
|
||||
public void upgradeDatabase(String fromVersion) throws Exception { |
|
||||
switch (fromVersion) { |
|
||||
case "2.4.3": |
|
||||
try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) { |
|
||||
log.info("Updating timescale schema ..."); |
|
||||
log.info("Load upgrade functions ..."); |
|
||||
loadSql(conn); |
|
||||
boolean versionValid = checkVersion(conn); |
|
||||
if (!versionValid) { |
|
||||
log.info("PostgreSQL version should be at least more than 9.6!"); |
|
||||
log.info("Please upgrade your PostgreSQL and restart the script!"); |
|
||||
} else { |
|
||||
log.info("PostgreSQL version is valid!"); |
|
||||
log.info("Updating schema ..."); |
|
||||
executeFunction(conn, CREATE_TS_KV_LATEST_TABLE); |
|
||||
executeFunction(conn, CREATE_TENANT_TS_KV_TABLE_COPY); |
|
||||
executeFunction(conn, CREATE_TS_KV_DICTIONARY_TABLE); |
|
||||
executeFunction(conn, INSERT_INTO_DICTIONARY); |
|
||||
executeFunction(conn, INSERT_INTO_TS_KV); |
|
||||
executeFunction(conn, INSERT_INTO_TS_KV_LATEST); |
|
||||
executeQuery(conn, DROP_OLD_TS_KV_TABLE); |
|
||||
log.info("schema timeseries updated!"); |
|
||||
} |
|
||||
} |
|
||||
break; |
|
||||
default: |
|
||||
throw new RuntimeException("Unable to upgrade SQL database, unsupported fromVersion: " + fromVersion); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
private void loadSql(Connection conn) { |
|
||||
Path schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "2.4.3", LOAD_FUNCTIONS_SQL); |
|
||||
try { |
|
||||
loadFunctions(schemaUpdateFile, conn); |
|
||||
log.info("Upgrade functions successfully loaded!"); |
|
||||
} catch (Exception e) { |
|
||||
log.info("Failed to load Timescale upgrade functions due to: {}", e.getMessage()); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
private void loadFunctions(Path sqlFile, Connection conn) throws Exception { |
|
||||
String sql = new String(Files.readAllBytes(sqlFile), StandardCharsets.UTF_8); |
|
||||
conn.createStatement().execute(sql); //NOSONAR, ignoring because method used to execute thingsboard database upgrade script
|
|
||||
} |
|
||||
|
|
||||
private boolean checkVersion(Connection conn) { |
|
||||
log.info("Check the current PostgreSQL version..."); |
|
||||
boolean versionValid = false; |
|
||||
try { |
|
||||
CallableStatement callableStatement = conn.prepareCall("{? = " + CHECK_VERSION + " }"); |
|
||||
callableStatement.registerOutParameter(1, Types.BOOLEAN); |
|
||||
callableStatement.execute(); |
|
||||
versionValid = callableStatement.getBoolean(1); |
|
||||
callableStatement.close(); |
|
||||
} catch (Exception e) { |
|
||||
log.info("Failed to check current PostgreSQL version due to: {}", e.getMessage()); |
|
||||
} |
|
||||
return versionValid; |
|
||||
} |
|
||||
|
|
||||
private void executeFunction(Connection conn, String query) { |
|
||||
log.info("{} ... ", query); |
|
||||
try { |
|
||||
CallableStatement callableStatement = conn.prepareCall("{" + query + "}"); |
|
||||
callableStatement.execute(); |
|
||||
callableStatement.close(); |
|
||||
log.info("Successfully executed: {}", query.replace(CALL_REGEX, "")); |
|
||||
} catch (Exception e) { |
|
||||
log.info("Failed to execute {} due to: {}", query, e.getMessage()); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
private void executeQuery(Connection conn, String query) { |
|
||||
try { |
|
||||
conn.createStatement().execute(query); //NOSONAR, ignoring because method used to execute thingsboard database upgrade script
|
|
||||
Thread.sleep(5000); |
|
||||
} catch (InterruptedException | SQLException e) { |
|
||||
log.info("Failed to drop table {} due to: {}", query.replace("DROP TABLE ", ""), e.getMessage()); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,68 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2020 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. |
||||
|
*/ |
||||
|
package org.thingsboard.server.service.install; |
||||
|
|
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.beans.factory.annotation.Value; |
||||
|
import org.springframework.context.annotation.Profile; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.thingsboard.server.dao.util.PsqlDao; |
||||
|
import org.thingsboard.server.dao.util.TimescaleDBTsDao; |
||||
|
|
||||
|
import java.sql.Connection; |
||||
|
import java.sql.DriverManager; |
||||
|
import java.sql.SQLException; |
||||
|
|
||||
|
@Service |
||||
|
@TimescaleDBTsDao |
||||
|
@PsqlDao |
||||
|
@Profile("install") |
||||
|
@Slf4j |
||||
|
public class TimescaleTsDatabaseSchemaService extends SqlAbstractDatabaseSchemaService implements TsDatabaseSchemaService { |
||||
|
|
||||
|
private static final String QUERY = "query: {}"; |
||||
|
private static final String SUCCESSFULLY_EXECUTED = "Successfully executed "; |
||||
|
private static final String FAILED_TO_EXECUTE = "Failed to execute "; |
||||
|
private static final String FAILED_DUE_TO = " due to: {}"; |
||||
|
|
||||
|
private static final String SUCCESSFULLY_EXECUTED_QUERY = SUCCESSFULLY_EXECUTED + QUERY; |
||||
|
private static final String FAILED_TO_EXECUTE_QUERY = FAILED_TO_EXECUTE + QUERY + FAILED_DUE_TO; |
||||
|
|
||||
|
@Value("${sql.timescale.chunk_time_interval:86400000}") |
||||
|
private long chunkTimeInterval; |
||||
|
|
||||
|
public TimescaleTsDatabaseSchemaService() { |
||||
|
super("schema-timescale.sql", "schema-timescale-idx.sql"); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void createDatabaseSchema() throws Exception { |
||||
|
super.createDatabaseSchema(); |
||||
|
executeQuery("SELECT create_hypertable('tenant_ts_kv', 'ts', chunk_time_interval => " + chunkTimeInterval + ", if_not_exists => true);"); |
||||
|
} |
||||
|
|
||||
|
private void executeQuery(String query) { |
||||
|
try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) { |
||||
|
conn.createStatement().execute(query); //NOSONAR, ignoring because method used to execute thingsboard database upgrade script
|
||||
|
log.info(SUCCESSFULLY_EXECUTED_QUERY, query); |
||||
|
Thread.sleep(5000); |
||||
|
} catch (InterruptedException | SQLException e) { |
||||
|
log.info(FAILED_TO_EXECUTE_QUERY, query, e.getMessage()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
@ -0,0 +1,125 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2020 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. |
||||
|
*/ |
||||
|
package org.thingsboard.server.service.install; |
||||
|
|
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.beans.factory.annotation.Value; |
||||
|
import org.springframework.context.annotation.Profile; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.thingsboard.server.dao.util.PsqlDao; |
||||
|
import org.thingsboard.server.dao.util.TimescaleDBTsDao; |
||||
|
|
||||
|
import java.nio.file.Path; |
||||
|
import java.nio.file.Paths; |
||||
|
import java.sql.Connection; |
||||
|
import java.sql.DriverManager; |
||||
|
|
||||
|
@Service |
||||
|
@Profile("install") |
||||
|
@Slf4j |
||||
|
@TimescaleDBTsDao |
||||
|
@PsqlDao |
||||
|
public class TimescaleTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgradeService implements DatabaseTsUpgradeService { |
||||
|
|
||||
|
@Value("${sql.timescale.chunk_time_interval:86400000}") |
||||
|
private long chunkTimeInterval; |
||||
|
|
||||
|
private static final String LOAD_FUNCTIONS_SQL = "schema_update_timescale_ts.sql"; |
||||
|
|
||||
|
private static final String TENANT_TS_KV_OLD_TABLE = "tenant_ts_kv_old;"; |
||||
|
|
||||
|
private static final String CREATE_TS_KV_LATEST_TABLE = "create_ts_kv_latest_table()"; |
||||
|
private static final String CREATE_NEW_TENANT_TS_KV_TABLE = "create_new_tenant_ts_kv_table()"; |
||||
|
private static final String CREATE_TS_KV_DICTIONARY_TABLE = "create_ts_kv_dictionary_table()"; |
||||
|
private static final String INSERT_INTO_DICTIONARY = "insert_into_dictionary()"; |
||||
|
private static final String INSERT_INTO_TENANT_TS_KV = "insert_into_tenant_ts_kv()"; |
||||
|
private static final String INSERT_INTO_TS_KV_LATEST = "insert_into_ts_kv_latest()"; |
||||
|
|
||||
|
private static final String CALL_CREATE_TS_KV_LATEST_TABLE = CALL_REGEX + CREATE_TS_KV_LATEST_TABLE; |
||||
|
private static final String CALL_CREATE_NEW_TENANT_TS_KV_TABLE = CALL_REGEX + CREATE_NEW_TENANT_TS_KV_TABLE; |
||||
|
private static final String CALL_CREATE_TS_KV_DICTIONARY_TABLE = CALL_REGEX + CREATE_TS_KV_DICTIONARY_TABLE; |
||||
|
private static final String CALL_INSERT_INTO_DICTIONARY = CALL_REGEX + INSERT_INTO_DICTIONARY; |
||||
|
private static final String CALL_INSERT_INTO_TS_KV = CALL_REGEX + INSERT_INTO_TENANT_TS_KV; |
||||
|
private static final String CALL_INSERT_INTO_TS_KV_LATEST = CALL_REGEX + INSERT_INTO_TS_KV_LATEST; |
||||
|
|
||||
|
private static final String DROP_OLD_TENANT_TS_KV_TABLE = DROP_TABLE + TENANT_TS_KV_OLD_TABLE; |
||||
|
|
||||
|
private static final String DROP_FUNCTION_CREATE_TS_KV_LATEST_TABLE = DROP_FUNCTION_IF_EXISTS + CREATE_TS_KV_LATEST_TABLE; |
||||
|
private static final String DROP_FUNCTION_CREATE_TENANT_TS_KV_TABLE_COPY = DROP_FUNCTION_IF_EXISTS + CREATE_NEW_TENANT_TS_KV_TABLE; |
||||
|
private static final String DROP_FUNCTION_CREATE_TS_KV_DICTIONARY_TABLE = DROP_FUNCTION_IF_EXISTS + CREATE_TS_KV_DICTIONARY_TABLE; |
||||
|
private static final String DROP_FUNCTION_INSERT_INTO_DICTIONARY = DROP_FUNCTION_IF_EXISTS + INSERT_INTO_DICTIONARY; |
||||
|
private static final String DROP_FUNCTION_INSERT_INTO_TENANT_TS_KV = DROP_FUNCTION_IF_EXISTS + INSERT_INTO_TENANT_TS_KV; |
||||
|
private static final String DROP_FUNCTION_INSERT_INTO_TS_KV_LATEST = DROP_FUNCTION_IF_EXISTS + INSERT_INTO_TS_KV_LATEST; |
||||
|
|
||||
|
@Autowired |
||||
|
private InstallScripts installScripts; |
||||
|
|
||||
|
@Override |
||||
|
public void upgradeDatabase(String fromVersion) throws Exception { |
||||
|
switch (fromVersion) { |
||||
|
case "2.4.3": |
||||
|
try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) { |
||||
|
log.info("Updating timescale schema ..."); |
||||
|
log.info("Load upgrade functions ..."); |
||||
|
loadSql(conn); |
||||
|
boolean versionValid = checkVersion(conn); |
||||
|
if (!versionValid) { |
||||
|
log.info("PostgreSQL version should be at least more than 9.6!"); |
||||
|
log.info("Please upgrade your PostgreSQL and restart the script!"); |
||||
|
} else { |
||||
|
log.info("PostgreSQL version is valid!"); |
||||
|
log.info("Updating schema ..."); |
||||
|
executeFunction(conn, CALL_CREATE_TS_KV_LATEST_TABLE); |
||||
|
executeFunction(conn, CALL_CREATE_NEW_TENANT_TS_KV_TABLE); |
||||
|
|
||||
|
executeQuery(conn, "SELECT create_hypertable('tenant_ts_kv', 'ts', chunk_time_interval => " + chunkTimeInterval + ", if_not_exists => true);"); |
||||
|
|
||||
|
executeFunction(conn, CALL_CREATE_TS_KV_DICTIONARY_TABLE); |
||||
|
executeFunction(conn, CALL_INSERT_INTO_DICTIONARY); |
||||
|
executeFunction(conn, CALL_INSERT_INTO_TS_KV); |
||||
|
executeFunction(conn, CALL_INSERT_INTO_TS_KV_LATEST); |
||||
|
|
||||
|
//executeQuery(conn, "SELECT set_chunk_time_interval('tenant_ts_kv', " + chunkTimeInterval +");");
|
||||
|
|
||||
|
executeDropStatement(conn, DROP_OLD_TENANT_TS_KV_TABLE); |
||||
|
|
||||
|
executeDropStatement(conn, DROP_FUNCTION_CREATE_TS_KV_LATEST_TABLE); |
||||
|
executeDropStatement(conn, DROP_FUNCTION_CREATE_TENANT_TS_KV_TABLE_COPY); |
||||
|
executeDropStatement(conn, DROP_FUNCTION_CREATE_TS_KV_DICTIONARY_TABLE); |
||||
|
executeDropStatement(conn, DROP_FUNCTION_INSERT_INTO_DICTIONARY); |
||||
|
executeDropStatement(conn, DROP_FUNCTION_INSERT_INTO_TENANT_TS_KV); |
||||
|
executeDropStatement(conn, DROP_FUNCTION_INSERT_INTO_TS_KV_LATEST); |
||||
|
|
||||
|
log.info("schema timeseries updated!"); |
||||
|
} |
||||
|
} |
||||
|
break; |
||||
|
default: |
||||
|
throw new RuntimeException("Unable to upgrade SQL database, unsupported fromVersion: " + fromVersion); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
protected void loadSql(Connection conn) { |
||||
|
Path schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "2.4.3", LOAD_FUNCTIONS_SQL); |
||||
|
try { |
||||
|
loadFunctions(schemaUpdateFile, conn); |
||||
|
log.info("Upgrade functions successfully loaded!"); |
||||
|
} catch (Exception e) { |
||||
|
log.info("Failed to load Timescale upgrade functions due to: {}", e.getMessage()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue