|
|
|
@ -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_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_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_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_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_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_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_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;"; |
|
|
|
|
|
|
|
@Override |
|
|
|
@ -118,39 +124,44 @@ public class PsqlTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgradeSe |
|
|
|
Path tsKvLatestFile = Files.createTempFile(pathToDir, "ts_kv_latest", ".sql"); |
|
|
|
pathToTempTsKvFile = tsKvFile.toAbsolutePath(); |
|
|
|
pathToTempTsKvLatestFile = tsKvLatestFile.toAbsolutePath(); |
|
|
|
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 + "');"); |
|
|
|
try { |
|
|
|
copyTimeseries(conn, pathToTempTsKvFile, pathToTempTsKvLatestFile); |
|
|
|
} catch (Exception e) { |
|
|
|
log.info("Upgrade script failed using the copy to/from files strategy!" + |
|
|
|
" Trying to perfrom the upgrade using Inserts strategy ..."); |
|
|
|
insertTimeseries(conn); |
|
|
|
} |
|
|
|
} catch (IOException | SecurityException e) { |
|
|
|
throw new RuntimeException("Failed to create time-series upgrade files due to: " + e); |
|
|
|
} |
|
|
|
} else { |
|
|
|
Path tempDirPath = Files.createTempDirectory("ts_kv"); |
|
|
|
File tempDirAsFile = tempDirPath.toFile(); |
|
|
|
boolean writable = tempDirAsFile.setWritable(true, false); |
|
|
|
boolean readable = tempDirAsFile.setReadable(true, false); |
|
|
|
boolean executable = tempDirAsFile.setExecutable(true, false); |
|
|
|
if (writable && readable && executable) { |
|
|
|
try { |
|
|
|
Path tempDirPath = Files.createTempDirectory("ts_kv"); |
|
|
|
File tempDirAsFile = tempDirPath.toFile(); |
|
|
|
boolean writable = tempDirAsFile.setWritable(true, false); |
|
|
|
boolean readable = tempDirAsFile.setReadable(true, false); |
|
|
|
boolean executable = tempDirAsFile.setExecutable(true, false); |
|
|
|
pathToTempTsKvFile = tempDirPath.resolve(TS_KV_SQL).toAbsolutePath(); |
|
|
|
pathToTempTsKvLatestFile = tempDirPath.resolve(TS_KV_LATEST_SQL).toAbsolutePath(); |
|
|
|
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 + "');"); |
|
|
|
} else { |
|
|
|
throw new RuntimeException("Failed to grant write permissions for the: " + tempDirPath + "folder!"); |
|
|
|
} |
|
|
|
} |
|
|
|
if (pathToTempTsKvFile.toFile().exists() && pathToTempTsKvLatestFile.toFile().exists()) { |
|
|
|
boolean deleteTsKvFile = pathToTempTsKvFile.toFile().delete(); |
|
|
|
if (deleteTsKvFile) { |
|
|
|
log.info("Successfully deleted the temp file for ts_kv table upgrade!"); |
|
|
|
} |
|
|
|
boolean deleteTsKvLatestFile = pathToTempTsKvLatestFile.toFile().delete(); |
|
|
|
if (deleteTsKvLatestFile) { |
|
|
|
log.info("Successfully deleted the temp file for ts_kv_latest table upgrade!"); |
|
|
|
try { |
|
|
|
if (writable && readable && executable) { |
|
|
|
copyTimeseries(conn, pathToTempTsKvFile, pathToTempTsKvLatestFile); |
|
|
|
} else { |
|
|
|
throw new RuntimeException("Failed to grant write permissions for the: " + tempDirPath + "folder!"); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
log.info(e.getMessage()); |
|
|
|
log.info("Upgrade script failed using the copy to/from files strategy!" + |
|
|
|
" Trying to perfrom the upgrade using Inserts strategy ..."); |
|
|
|
insertTimeseries(conn); |
|
|
|
} |
|
|
|
} catch (IOException | SecurityException e) { |
|
|
|
throw new RuntimeException("Failed to create time-series upgrade files due to: " + e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
removeUpgradeFiles(pathToTempTsKvFile, pathToTempTsKvLatestFile); |
|
|
|
|
|
|
|
executeQuery(conn, DROP_TABLE_TS_KV_OLD); |
|
|
|
executeQuery(conn, DROP_TABLE_TS_KV_LATEST_OLD); |
|
|
|
|
|
|
|
@ -161,6 +172,8 @@ public class PsqlTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgradeSe |
|
|
|
executeQuery(conn, DROP_PROCEDURE_INSERT_INTO_TS_KV); |
|
|
|
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_CURSOR); |
|
|
|
executeQuery(conn, DROP_PROCEDURE_INSERT_INTO_TS_KV_LATEST_CURSOR); |
|
|
|
executeQuery(conn, DROP_FUNCTION_GET_PARTITION_DATA); |
|
|
|
|
|
|
|
executeQuery(conn, "ALTER TABLE ts_kv ADD COLUMN IF NOT EXISTS json_v json;"); |
|
|
|
@ -186,6 +199,31 @@ public class PsqlTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgradeSe |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void removeUpgradeFiles(Path pathToTempTsKvFile, Path pathToTempTsKvLatestFile) { |
|
|
|
if (pathToTempTsKvFile.toFile().exists() && pathToTempTsKvLatestFile.toFile().exists()) { |
|
|
|
boolean deleteTsKvFile = pathToTempTsKvFile.toFile().delete(); |
|
|
|
if (deleteTsKvFile) { |
|
|
|
log.info("Successfully deleted the temp file for ts_kv table upgrade!"); |
|
|
|
} |
|
|
|
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) { |
|
|
|
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 |
|
|
|
protected void loadSql(Connection conn, String fileName) { |
|
|
|
Path schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "2.4.3", fileName); |
|
|
|
|