|
|
@ -57,11 +57,15 @@ public class PsqlTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgradeSe |
|
|
private static final String INSERT_INTO_DICTIONARY = "insert_into_dictionary()"; |
|
|
private static final String INSERT_INTO_DICTIONARY = "insert_into_dictionary()"; |
|
|
private static final String INSERT_INTO_TS_KV = "insert_into_ts_kv(IN path_to_file varchar)"; |
|
|
private static final String INSERT_INTO_TS_KV = "insert_into_ts_kv(IN path_to_file varchar)"; |
|
|
private static final String INSERT_INTO_TS_KV_LATEST = "insert_into_ts_kv_latest(IN path_to_file varchar)"; |
|
|
private static final String INSERT_INTO_TS_KV_LATEST = "insert_into_ts_kv_latest(IN path_to_file varchar)"; |
|
|
|
|
|
private static final String INSERT_INTO_TS_KV_CURSOR = "insert_into_ts_kv_cursor()"; |
|
|
|
|
|
private static final String INSERT_INTO_TS_KV_LATEST_CURSOR = "insert_into_ts_kv_latest_cursor()"; |
|
|
|
|
|
|
|
|
private static final String CALL_CREATE_PARTITION_TS_KV_TABLE = CALL_REGEX + CREATE_PARTITION_TS_KV_TABLE; |
|
|
private static final String CALL_CREATE_PARTITION_TS_KV_TABLE = CALL_REGEX + CREATE_PARTITION_TS_KV_TABLE; |
|
|
private static final String CALL_CREATE_NEW_TS_KV_LATEST_TABLE = CALL_REGEX + CREATE_NEW_TS_KV_LATEST_TABLE; |
|
|
private static final String CALL_CREATE_NEW_TS_KV_LATEST_TABLE = CALL_REGEX + CREATE_NEW_TS_KV_LATEST_TABLE; |
|
|
private static final String CALL_CREATE_TS_KV_DICTIONARY_TABLE = CALL_REGEX + CREATE_TS_KV_DICTIONARY_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_DICTIONARY = CALL_REGEX + INSERT_INTO_DICTIONARY; |
|
|
|
|
|
private static final String CALL_INSERT_INTO_TS_KV_CURSOR = CALL_REGEX + INSERT_INTO_TS_KV_CURSOR; |
|
|
|
|
|
private static final String CALL_INSERT_INTO_TS_KV_LATEST_CURSOR = CALL_REGEX + INSERT_INTO_TS_KV_LATEST_CURSOR; |
|
|
|
|
|
|
|
|
private static final String DROP_TABLE_TS_KV_OLD = DROP_TABLE + TS_KV_OLD; |
|
|
private static final String DROP_TABLE_TS_KV_OLD = DROP_TABLE + TS_KV_OLD; |
|
|
private static final String DROP_TABLE_TS_KV_LATEST_OLD = DROP_TABLE + TS_KV_LATEST_OLD; |
|
|
private static final String DROP_TABLE_TS_KV_LATEST_OLD = DROP_TABLE + TS_KV_LATEST_OLD; |
|
|
@ -73,6 +77,8 @@ public class PsqlTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgradeSe |
|
|
private static final String DROP_PROCEDURE_INSERT_INTO_DICTIONARY = DROP_PROCEDURE_IF_EXISTS + INSERT_INTO_DICTIONARY; |
|
|
private static final String DROP_PROCEDURE_INSERT_INTO_DICTIONARY = DROP_PROCEDURE_IF_EXISTS + INSERT_INTO_DICTIONARY; |
|
|
private static final String DROP_PROCEDURE_INSERT_INTO_TS_KV = DROP_PROCEDURE_IF_EXISTS + INSERT_INTO_TS_KV; |
|
|
private static final String DROP_PROCEDURE_INSERT_INTO_TS_KV = DROP_PROCEDURE_IF_EXISTS + INSERT_INTO_TS_KV; |
|
|
private static final String DROP_PROCEDURE_INSERT_INTO_TS_KV_LATEST = DROP_PROCEDURE_IF_EXISTS + INSERT_INTO_TS_KV_LATEST; |
|
|
private static final String DROP_PROCEDURE_INSERT_INTO_TS_KV_LATEST = DROP_PROCEDURE_IF_EXISTS + INSERT_INTO_TS_KV_LATEST; |
|
|
|
|
|
private static final String DROP_PROCEDURE_INSERT_INTO_TS_KV_CURSOR = DROP_PROCEDURE_IF_EXISTS + INSERT_INTO_TS_KV_CURSOR; |
|
|
|
|
|
private static final String DROP_PROCEDURE_INSERT_INTO_TS_KV_LATEST_CURSOR = DROP_PROCEDURE_IF_EXISTS + INSERT_INTO_TS_KV_LATEST_CURSOR; |
|
|
private static final String DROP_FUNCTION_GET_PARTITION_DATA = "DROP FUNCTION IF EXISTS get_partitions_data;"; |
|
|
private static final String DROP_FUNCTION_GET_PARTITION_DATA = "DROP FUNCTION IF EXISTS get_partitions_data;"; |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
@ -93,14 +99,12 @@ public class PsqlTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgradeSe |
|
|
executeQuery(conn, CALL_CREATE_PARTITION_TS_KV_TABLE); |
|
|
executeQuery(conn, CALL_CREATE_PARTITION_TS_KV_TABLE); |
|
|
if (!partitionType.equals("INDEFINITE")) { |
|
|
if (!partitionType.equals("INDEFINITE")) { |
|
|
executeQuery(conn, "call create_partitions('" + partitionType + "')"); |
|
|
executeQuery(conn, "call create_partitions('" + partitionType + "')"); |
|
|
} else { |
|
|
|
|
|
executeQuery(conn, "CREATE TABLE IF NOT EXISTS ts_kv_indefinite PARTITION OF ts_kv DEFAULT;"); |
|
|
|
|
|
} |
|
|
} |
|
|
executeQuery(conn, CALL_CREATE_TS_KV_DICTIONARY_TABLE); |
|
|
executeQuery(conn, CALL_CREATE_TS_KV_DICTIONARY_TABLE); |
|
|
executeQuery(conn, CALL_INSERT_INTO_DICTIONARY); |
|
|
executeQuery(conn, CALL_INSERT_INTO_DICTIONARY); |
|
|
|
|
|
|
|
|
Path pathToTempTsKvFile; |
|
|
Path pathToTempTsKvFile = null; |
|
|
Path pathToTempTsKvLatestFile; |
|
|
Path pathToTempTsKvLatestFile = null; |
|
|
if (SystemUtils.IS_OS_WINDOWS) { |
|
|
if (SystemUtils.IS_OS_WINDOWS) { |
|
|
log.info("Lookup for environment variable: {} ...", THINGSBOARD_WINDOWS_UPGRADE_DIR); |
|
|
log.info("Lookup for environment variable: {} ...", THINGSBOARD_WINDOWS_UPGRADE_DIR); |
|
|
Path pathToDir; |
|
|
Path pathToDir; |
|
|
@ -118,39 +122,41 @@ public class PsqlTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgradeSe |
|
|
Path tsKvLatestFile = Files.createTempFile(pathToDir, "ts_kv_latest", ".sql"); |
|
|
Path tsKvLatestFile = Files.createTempFile(pathToDir, "ts_kv_latest", ".sql"); |
|
|
pathToTempTsKvFile = tsKvFile.toAbsolutePath(); |
|
|
pathToTempTsKvFile = tsKvFile.toAbsolutePath(); |
|
|
pathToTempTsKvLatestFile = tsKvLatestFile.toAbsolutePath(); |
|
|
pathToTempTsKvLatestFile = tsKvLatestFile.toAbsolutePath(); |
|
|
executeQuery(conn, "call insert_into_ts_kv('" + pathToTempTsKvFile + "')"); |
|
|
try { |
|
|
executeQuery(conn, CALL_CREATE_NEW_TS_KV_LATEST_TABLE); |
|
|
copyTimeseries(conn, pathToTempTsKvFile, pathToTempTsKvLatestFile); |
|
|
executeQuery(conn, "call insert_into_ts_kv_latest('" + pathToTempTsKvLatestFile + "');"); |
|
|
} catch (Exception e) { |
|
|
|
|
|
insertTimeseries(conn); |
|
|
|
|
|
} |
|
|
} catch (IOException | SecurityException e) { |
|
|
} catch (IOException | SecurityException e) { |
|
|
throw new RuntimeException("Failed to create time-series upgrade files due to: " + e); |
|
|
log.warn("Failed to create time-series upgrade files due to: {}", e.getMessage()); |
|
|
|
|
|
insertTimeseries(conn); |
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
Path tempDirPath = Files.createTempDirectory("ts_kv"); |
|
|
try { |
|
|
File tempDirAsFile = tempDirPath.toFile(); |
|
|
Path tempDirPath = Files.createTempDirectory("ts_kv"); |
|
|
boolean writable = tempDirAsFile.setWritable(true, false); |
|
|
File tempDirAsFile = tempDirPath.toFile(); |
|
|
boolean readable = tempDirAsFile.setReadable(true, false); |
|
|
boolean writable = tempDirAsFile.setWritable(true, false); |
|
|
boolean executable = tempDirAsFile.setExecutable(true, false); |
|
|
boolean readable = tempDirAsFile.setReadable(true, false); |
|
|
if (writable && readable && executable) { |
|
|
boolean executable = tempDirAsFile.setExecutable(true, false); |
|
|
pathToTempTsKvFile = tempDirPath.resolve(TS_KV_SQL).toAbsolutePath(); |
|
|
pathToTempTsKvFile = tempDirPath.resolve(TS_KV_SQL).toAbsolutePath(); |
|
|
pathToTempTsKvLatestFile = tempDirPath.resolve(TS_KV_LATEST_SQL).toAbsolutePath(); |
|
|
pathToTempTsKvLatestFile = tempDirPath.resolve(TS_KV_LATEST_SQL).toAbsolutePath(); |
|
|
executeQuery(conn, "call insert_into_ts_kv('" + pathToTempTsKvFile + "')"); |
|
|
try { |
|
|
executeQuery(conn, CALL_CREATE_NEW_TS_KV_LATEST_TABLE); |
|
|
if (writable && readable && executable) { |
|
|
executeQuery(conn, "call insert_into_ts_kv_latest('" + pathToTempTsKvLatestFile + "');"); |
|
|
copyTimeseries(conn, pathToTempTsKvFile, pathToTempTsKvLatestFile); |
|
|
} else { |
|
|
} else { |
|
|
throw new RuntimeException("Failed to grant write permissions for the: " + tempDirPath + "folder!"); |
|
|
throw new RuntimeException("Failed to grant write permissions for the: " + tempDirPath + "folder!"); |
|
|
} |
|
|
} |
|
|
} |
|
|
} catch (Exception e) { |
|
|
if (pathToTempTsKvFile.toFile().exists() && pathToTempTsKvLatestFile.toFile().exists()) { |
|
|
insertTimeseries(conn); |
|
|
boolean deleteTsKvFile = pathToTempTsKvFile.toFile().delete(); |
|
|
} |
|
|
if (deleteTsKvFile) { |
|
|
} catch (IOException | SecurityException e) { |
|
|
log.info("Successfully deleted the temp file for ts_kv table upgrade!"); |
|
|
log.warn("Failed to create time-series upgrade files due to: {}", e.getMessage()); |
|
|
} |
|
|
insertTimeseries(conn); |
|
|
boolean deleteTsKvLatestFile = pathToTempTsKvLatestFile.toFile().delete(); |
|
|
|
|
|
if (deleteTsKvLatestFile) { |
|
|
|
|
|
log.info("Successfully deleted the temp file for ts_kv_latest table upgrade!"); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
removeUpgradeFiles(pathToTempTsKvFile, pathToTempTsKvLatestFile); |
|
|
|
|
|
|
|
|
executeQuery(conn, DROP_TABLE_TS_KV_OLD); |
|
|
executeQuery(conn, DROP_TABLE_TS_KV_OLD); |
|
|
executeQuery(conn, DROP_TABLE_TS_KV_LATEST_OLD); |
|
|
executeQuery(conn, DROP_TABLE_TS_KV_LATEST_OLD); |
|
|
|
|
|
|
|
|
@ -161,6 +167,8 @@ public class PsqlTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgradeSe |
|
|
executeQuery(conn, DROP_PROCEDURE_INSERT_INTO_TS_KV); |
|
|
executeQuery(conn, DROP_PROCEDURE_INSERT_INTO_TS_KV); |
|
|
executeQuery(conn, DROP_PROCEDURE_CREATE_NEW_TS_KV_LATEST_TABLE); |
|
|
executeQuery(conn, DROP_PROCEDURE_CREATE_NEW_TS_KV_LATEST_TABLE); |
|
|
executeQuery(conn, DROP_PROCEDURE_INSERT_INTO_TS_KV_LATEST); |
|
|
executeQuery(conn, DROP_PROCEDURE_INSERT_INTO_TS_KV_LATEST); |
|
|
|
|
|
executeQuery(conn, DROP_PROCEDURE_INSERT_INTO_TS_KV_CURSOR); |
|
|
|
|
|
executeQuery(conn, DROP_PROCEDURE_INSERT_INTO_TS_KV_LATEST_CURSOR); |
|
|
executeQuery(conn, DROP_FUNCTION_GET_PARTITION_DATA); |
|
|
executeQuery(conn, DROP_FUNCTION_GET_PARTITION_DATA); |
|
|
|
|
|
|
|
|
executeQuery(conn, "ALTER TABLE ts_kv ADD COLUMN IF NOT EXISTS json_v json;"); |
|
|
executeQuery(conn, "ALTER TABLE ts_kv ADD COLUMN IF NOT EXISTS json_v json;"); |
|
|
@ -181,11 +189,46 @@ public class PsqlTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgradeSe |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
break; |
|
|
break; |
|
|
|
|
|
case "2.5.0": |
|
|
|
|
|
try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) { |
|
|
|
|
|
executeQuery(conn, "CREATE TABLE IF NOT EXISTS ts_kv_indefinite PARTITION OF ts_kv DEFAULT;"); |
|
|
|
|
|
executeQuery(conn, "UPDATE tb_schema_settings SET schema_version = 2005001"); |
|
|
|
|
|
} |
|
|
|
|
|
break; |
|
|
default: |
|
|
default: |
|
|
throw new RuntimeException("Unable to upgrade SQL database, unsupported fromVersion: " + fromVersion); |
|
|
throw new RuntimeException("Unable to upgrade SQL database, unsupported fromVersion: " + fromVersion); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void removeUpgradeFiles(Path pathToTempTsKvFile, Path pathToTempTsKvLatestFile) { |
|
|
|
|
|
if (pathToTempTsKvFile != null && pathToTempTsKvFile.toFile().exists()) { |
|
|
|
|
|
boolean deleteTsKvFile = pathToTempTsKvFile.toFile().delete(); |
|
|
|
|
|
if (deleteTsKvFile) { |
|
|
|
|
|
log.info("Successfully deleted the temp file for ts_kv table upgrade!"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if (pathToTempTsKvLatestFile != null && pathToTempTsKvLatestFile.toFile().exists()) { |
|
|
|
|
|
boolean deleteTsKvLatestFile = pathToTempTsKvLatestFile.toFile().delete(); |
|
|
|
|
|
if (deleteTsKvLatestFile) { |
|
|
|
|
|
log.info("Successfully deleted the temp file for ts_kv_latest table upgrade!"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void copyTimeseries(Connection conn, Path pathToTempTsKvFile, Path pathToTempTsKvLatestFile) { |
|
|
|
|
|
executeQuery(conn, "call insert_into_ts_kv('" + pathToTempTsKvFile + "')"); |
|
|
|
|
|
executeQuery(conn, CALL_CREATE_NEW_TS_KV_LATEST_TABLE); |
|
|
|
|
|
executeQuery(conn, "call insert_into_ts_kv_latest('" + pathToTempTsKvLatestFile + "')"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void insertTimeseries(Connection conn) { |
|
|
|
|
|
log.warn("Upgrade script failed using the copy to/from files strategy!" + |
|
|
|
|
|
" Trying to perfrom the upgrade using Inserts strategy ..."); |
|
|
|
|
|
executeQuery(conn, CALL_INSERT_INTO_TS_KV_CURSOR); |
|
|
|
|
|
executeQuery(conn, CALL_CREATE_NEW_TS_KV_LATEST_TABLE); |
|
|
|
|
|
executeQuery(conn, CALL_INSERT_INTO_TS_KV_LATEST_CURSOR); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
protected void loadSql(Connection conn, String fileName) { |
|
|
protected void loadSql(Connection conn, String fileName) { |
|
|
Path schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "2.4.3", fileName); |
|
|
Path schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "2.4.3", fileName); |
|
|
|