Commit 1bde12eed0dfc8e5d6dc32462072417cbb44c59b
Committed by
Andrew Shvayka
1 parent
f57ed509
fix-the-timeseries-upgrade
Showing
5 changed files
with
16 additions
and
58 deletions
... | ... | @@ -14,24 +14,6 @@ |
14 | 14 | -- limitations under the License. |
15 | 15 | -- |
16 | 16 | |
17 | --- call check_version(); | |
18 | - | |
19 | -CREATE OR REPLACE PROCEDURE check_version(INOUT valid_version boolean) LANGUAGE plpgsql AS $BODY$ | |
20 | -DECLARE | |
21 | - current_version integer; | |
22 | -BEGIN | |
23 | - RAISE NOTICE 'Check the current installed PostgreSQL version...'; | |
24 | - SELECT current_setting('server_version_num') INTO current_version; | |
25 | - IF current_version > 110000 THEN | |
26 | - RAISE NOTICE 'PostgreSQL version is valid!'; | |
27 | - RAISE NOTICE 'Schema update started...'; | |
28 | - SELECT true INTO valid_version; | |
29 | - ELSE | |
30 | - RAISE NOTICE 'Postgres version should be at least more than 10!'; | |
31 | - END IF; | |
32 | -END; | |
33 | -$BODY$; | |
34 | - | |
35 | 17 | -- call create_partition_ts_kv_table(); |
36 | 18 | |
37 | 19 | CREATE OR REPLACE PROCEDURE create_partition_ts_kv_table() LANGUAGE plpgsql AS $$ | ... | ... |
... | ... | @@ -14,25 +14,6 @@ |
14 | 14 | -- limitations under the License. |
15 | 15 | -- |
16 | 16 | |
17 | --- call check_version(); | |
18 | - | |
19 | -CREATE OR REPLACE PROCEDURE check_version(INOUT valid_version boolean) LANGUAGE plpgsql AS $BODY$ | |
20 | - | |
21 | -DECLARE | |
22 | - current_version integer; | |
23 | -BEGIN | |
24 | - RAISE NOTICE 'Check the current installed PostgreSQL version...'; | |
25 | - SELECT current_setting('server_version_num') INTO current_version; | |
26 | - IF current_version > 110000 THEN | |
27 | - RAISE NOTICE 'PostgreSQL version is valid!'; | |
28 | - RAISE NOTICE 'Schema update started...'; | |
29 | - SELECT true INTO valid_version; | |
30 | - ELSE | |
31 | - RAISE NOTICE 'Postgres version should be at least more than 10!'; | |
32 | - END IF; | |
33 | -END; | |
34 | -$BODY$; | |
35 | - | |
36 | 17 | -- call create_new_ts_kv_table(); |
37 | 18 | |
38 | 19 | CREATE OR REPLACE PROCEDURE create_new_ts_kv_table() LANGUAGE plpgsql AS $$ | ... | ... |
... | ... | @@ -32,11 +32,8 @@ import java.sql.Statement; |
32 | 32 | public abstract class AbstractSqlTsDatabaseUpgradeService { |
33 | 33 | |
34 | 34 | protected static final String CALL_REGEX = "call "; |
35 | - protected static final String CHECK_VERSION = "check_version(false)"; | |
36 | - protected static final String CHECK_VERSION_TO_DELETE = "check_version(INOUT valid_version boolean)"; | |
37 | - protected static final String DROP_TABLE = "DROP TABLE "; | |
35 | + protected static final String DROP_TABLE = "DROP TABLE "; | |
38 | 36 | protected static final String DROP_PROCEDURE_IF_EXISTS = "DROP PROCEDURE IF EXISTS "; |
39 | - protected static final String DROP_PROCEDURE_CHECK_VERSION = DROP_PROCEDURE_IF_EXISTS + CHECK_VERSION_TO_DELETE; | |
40 | 37 | |
41 | 38 | @Value("${spring.datasource.url}") |
42 | 39 | protected String dbUrl; |
... | ... | @@ -58,13 +55,14 @@ public abstract class AbstractSqlTsDatabaseUpgradeService { |
58 | 55 | } |
59 | 56 | |
60 | 57 | protected boolean checkVersion(Connection conn) { |
61 | - log.info("Check the current PostgreSQL version..."); | |
62 | 58 | boolean versionValid = false; |
63 | 59 | try { |
64 | 60 | Statement statement = conn.createStatement(); |
65 | - ResultSet resultSet = statement.executeQuery(CALL_REGEX + CHECK_VERSION); | |
61 | + ResultSet resultSet = statement.executeQuery("SELECT current_setting('server_version_num')"); | |
66 | 62 | resultSet.next(); |
67 | - versionValid = resultSet.getBoolean(1); | |
63 | + if(resultSet.getLong(1) > 110000) { | |
64 | + versionValid = true; | |
65 | + } | |
68 | 66 | statement.close(); |
69 | 67 | } catch (Exception e) { |
70 | 68 | log.info("Failed to check current PostgreSQL version due to: {}", e.getMessage()); | ... | ... |
... | ... | @@ -70,16 +70,15 @@ public class PsqlTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgradeSe |
70 | 70 | switch (fromVersion) { |
71 | 71 | case "2.4.3": |
72 | 72 | try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) { |
73 | - log.info("Updating timeseries schema ..."); | |
74 | - log.info("Load upgrade functions ..."); | |
75 | - loadSql(conn); | |
73 | + log.info("Check the current PostgreSQL version..."); | |
76 | 74 | boolean versionValid = checkVersion(conn); |
77 | 75 | if (!versionValid) { |
78 | - log.info("PostgreSQL version should be at least more than 11!"); | |
79 | - log.info("Please upgrade your PostgreSQL and restart the script!"); | |
76 | + throw new RuntimeException("PostgreSQL version should be at least more than 11, please upgrade your PostgreSQL and restart the script!"); | |
80 | 77 | } else { |
81 | 78 | log.info("PostgreSQL version is valid!"); |
82 | - log.info("Updating schema ..."); | |
79 | + log.info("Load upgrade functions ..."); | |
80 | + loadSql(conn); | |
81 | + log.info("Updating timeseries schema ..."); | |
83 | 82 | executeQuery(conn, CALL_CREATE_PARTITION_TS_KV_TABLE); |
84 | 83 | executeQuery(conn, CALL_CREATE_PARTITIONS); |
85 | 84 | executeQuery(conn, CALL_CREATE_TS_KV_DICTIONARY_TABLE); |
... | ... | @@ -91,7 +90,6 @@ public class PsqlTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgradeSe |
91 | 90 | executeQuery(conn, DROP_TABLE_TS_KV_OLD); |
92 | 91 | executeQuery(conn, DROP_TABLE_TS_KV_LATEST_OLD); |
93 | 92 | |
94 | - executeQuery(conn, DROP_PROCEDURE_CHECK_VERSION); | |
95 | 93 | executeQuery(conn, DROP_PROCEDURE_CREATE_PARTITION_TS_KV_TABLE); |
96 | 94 | executeQuery(conn, DROP_PROCEDURE_CREATE_PARTITIONS); |
97 | 95 | executeQuery(conn, DROP_PROCEDURE_CREATE_TS_KV_DICTIONARY_TABLE); | ... | ... |
... | ... | @@ -73,16 +73,15 @@ public class TimescaleTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgr |
73 | 73 | switch (fromVersion) { |
74 | 74 | case "2.4.3": |
75 | 75 | try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) { |
76 | - log.info("Updating timescale schema ..."); | |
77 | - log.info("Load upgrade functions ..."); | |
78 | - loadSql(conn); | |
76 | + log.info("Check the current PostgreSQL version..."); | |
79 | 77 | boolean versionValid = checkVersion(conn); |
80 | 78 | if (!versionValid) { |
81 | - log.info("PostgreSQL version should be at least more than 11!"); | |
82 | - log.info("Please upgrade your PostgreSQL and restart the script!"); | |
79 | + throw new RuntimeException("PostgreSQL version should be at least more than 11, please upgrade your PostgreSQL and restart the script!"); | |
83 | 80 | } else { |
84 | 81 | log.info("PostgreSQL version is valid!"); |
85 | - log.info("Updating schema ..."); | |
82 | + log.info("Load upgrade functions ..."); | |
83 | + loadSql(conn); | |
84 | + log.info("Updating timescale schema ..."); | |
86 | 85 | executeQuery(conn, CALL_CREATE_TS_KV_LATEST_TABLE); |
87 | 86 | executeQuery(conn, CALL_CREATE_NEW_TENANT_TS_KV_TABLE); |
88 | 87 | |
... | ... | @@ -105,7 +104,7 @@ public class TimescaleTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgr |
105 | 104 | executeQuery(conn, "ALTER TABLE ts_kv ADD COLUMN json_v json;"); |
106 | 105 | executeQuery(conn, "ALTER TABLE ts_kv_latest ADD COLUMN json_v json;"); |
107 | 106 | |
108 | - log.info("schema timeseries updated!"); | |
107 | + log.info("schema timescale updated!"); | |
109 | 108 | } |
110 | 109 | } |
111 | 110 | break; | ... | ... |