Commit 562bbbe849ac80d62f48bcc229d869b55b2d220c

Authored by Igor Kulikov
1 parent 84460a00

Fix upgrade scripts.

@@ -14,6 +14,14 @@ @@ -14,6 +14,14 @@
14 -- limitations under the License. 14 -- limitations under the License.
15 -- 15 --
16 16
  17 +CREATE OR REPLACE FUNCTION to_uuid(IN entity_id varchar, OUT uuid_id uuid) AS
  18 +$$
  19 +BEGIN
  20 + uuid_id := substring(entity_id, 8, 8) || '-' || substring(entity_id, 4, 4) || '-1' || substring(entity_id, 1, 3) ||
  21 + '-' || substring(entity_id, 16, 4) || '-' || substring(entity_id, 20, 12);
  22 +END;
  23 +$$ LANGUAGE plpgsql;
  24 +
17 CREATE OR REPLACE FUNCTION extract_ts(uuid UUID) RETURNS BIGINT AS 25 CREATE OR REPLACE FUNCTION extract_ts(uuid UUID) RETURNS BIGINT AS
18 $$ 26 $$
19 DECLARE 27 DECLARE
@@ -826,3 +834,23 @@ BEGIN @@ -826,3 +834,23 @@ BEGIN
826 END IF; 834 END IF;
827 END; 835 END;
828 $$; 836 $$;
  837 +
  838 +CREATE TABLE IF NOT EXISTS ts_kv_latest
  839 +(
  840 + entity_id uuid NOT NULL,
  841 + key int NOT NULL,
  842 + ts bigint NOT NULL,
  843 + bool_v boolean,
  844 + str_v varchar(10000000),
  845 + long_v bigint,
  846 + dbl_v double precision,
  847 + json_v json,
  848 + CONSTRAINT ts_kv_latest_pkey PRIMARY KEY (entity_id, key)
  849 +);
  850 +
  851 +CREATE TABLE IF NOT EXISTS ts_kv_dictionary
  852 +(
  853 + key varchar(255) NOT NULL,
  854 + key_id serial UNIQUE,
  855 + CONSTRAINT ts_key_id_pkey PRIMARY KEY (key)
  856 +);
@@ -156,12 +156,11 @@ public class ThingsboardInstallService { @@ -156,12 +156,11 @@ public class ThingsboardInstallService {
156 } 156 }
157 case "2.5.1": 157 case "2.5.1":
158 log.info("Upgrading ThingsBoard from version 2.5.1 to 3.0.0 ..."); 158 log.info("Upgrading ThingsBoard from version 2.5.1 to 3.0.0 ...");
159 - log.info("Updating system data...");  
160 - systemDataLoaderService.updateSystemWidgets();  
161 - break;  
162 case "3.0.1": 159 case "3.0.1":
163 log.info("Upgrading ThingsBoard from version 3.0.1 to 3.1.0 ..."); 160 log.info("Upgrading ThingsBoard from version 3.0.1 to 3.1.0 ...");
164 databaseEntitiesUpgradeService.upgradeDatabase("3.0.1"); 161 databaseEntitiesUpgradeService.upgradeDatabase("3.0.1");
  162 + log.info("Updating system data...");
  163 + systemDataLoaderService.updateSystemWidgets();
165 break; 164 break;
166 default: 165 default:
167 throw new RuntimeException("Unable to upgrade ThingsBoard, unsupported fromVersion: " + upgradeFromVersion); 166 throw new RuntimeException("Unable to upgrade ThingsBoard, unsupported fromVersion: " + upgradeFromVersion);
@@ -254,13 +254,9 @@ public class SqlDatabaseUpgradeService implements DatabaseEntitiesUpgradeService @@ -254,13 +254,9 @@ public class SqlDatabaseUpgradeService implements DatabaseEntitiesUpgradeService
254 254
255 conn.createStatement().execute("call drop_all_idx()"); 255 conn.createStatement().execute("call drop_all_idx()");
256 256
257 - log.info("Updating alarm relations..."); 257 + log.info("Optimizing alarm relations...");
258 conn.createStatement().execute("DELETE from relation WHERE relation_type_group = 'ALARM' AND relation_type <> 'ALARM_ANY';"); 258 conn.createStatement().execute("DELETE from relation WHERE relation_type_group = 'ALARM' AND relation_type <> 'ALARM_ANY';");
259 -  
260 - conn.createStatement().execute("UPDATE relation SET relation_type = 'ANY' WHERE relation_type_group = 'ALARM' AND relation_type = 'ALARM_ANY';");  
261 - log.info("Alarm relations updated.");  
262 -  
263 - conn.createStatement().execute("VACUUM FULL relation"); 259 + log.info("Alarm relations optimized.");
264 260
265 for (String table : tables) { 261 for (String table : tables) {
266 log.info("Updating table {}.", table); 262 log.info("Updating table {}.", table);
@@ -286,6 +282,10 @@ public class SqlDatabaseUpgradeService implements DatabaseEntitiesUpgradeService @@ -286,6 +282,10 @@ public class SqlDatabaseUpgradeService implements DatabaseEntitiesUpgradeService
286 conn.createStatement().execute("DROP PROCEDURE create_all_idx"); 282 conn.createStatement().execute("DROP PROCEDURE create_all_idx");
287 conn.createStatement().execute("DROP FUNCTION column_type_to_uuid"); 283 conn.createStatement().execute("DROP FUNCTION column_type_to_uuid");
288 284
  285 + log.info("Updating alarm relations...");
  286 + conn.createStatement().execute("UPDATE relation SET relation_type = 'ANY' WHERE relation_type_group = 'ALARM' AND relation_type = 'ALARM_ANY';");
  287 + log.info("Alarm relations updated.");
  288 +
289 conn.createStatement().execute("UPDATE tb_schema_settings SET schema_version = 3001000;"); 289 conn.createStatement().execute("UPDATE tb_schema_settings SET schema_version = 3001000;");
290 290
291 conn.createStatement().execute("VACUUM FULL"); 291 conn.createStatement().execute("VACUUM FULL");
@@ -277,6 +277,26 @@ CREATE TABLE IF NOT EXISTS entity_view ( @@ -277,6 +277,26 @@ CREATE TABLE IF NOT EXISTS entity_view (
277 additional_info varchar 277 additional_info varchar
278 ); 278 );
279 279
  280 +CREATE TABLE IF NOT EXISTS ts_kv_latest
  281 +(
  282 + entity_id uuid NOT NULL,
  283 + key int NOT NULL,
  284 + ts bigint NOT NULL,
  285 + bool_v boolean,
  286 + str_v varchar(10000000),
  287 + long_v bigint,
  288 + dbl_v double precision,
  289 + json_v json,
  290 + CONSTRAINT ts_kv_latest_pkey PRIMARY KEY (entity_id, key)
  291 +);
  292 +
  293 +CREATE TABLE IF NOT EXISTS ts_kv_dictionary
  294 +(
  295 + key varchar(255) NOT NULL,
  296 + key_id serial UNIQUE,
  297 + CONSTRAINT ts_key_id_pkey PRIMARY KEY (key)
  298 +);
  299 +
280 CREATE OR REPLACE PROCEDURE cleanup_events_by_ttl(IN ttl bigint, IN debug_ttl bigint, INOUT deleted bigint) 300 CREATE OR REPLACE PROCEDURE cleanup_events_by_ttl(IN ttl bigint, IN debug_ttl bigint, INOUT deleted bigint)
281 LANGUAGE plpgsql AS 301 LANGUAGE plpgsql AS
282 $$ 302 $$
@@ -301,3 +321,11 @@ BEGIN @@ -301,3 +321,11 @@ BEGIN
301 deleted := ttl_deleted_count + debug_ttl_deleted_count; 321 deleted := ttl_deleted_count + debug_ttl_deleted_count;
302 END 322 END
303 $$; 323 $$;
  324 +
  325 +CREATE OR REPLACE FUNCTION to_uuid(IN entity_id varchar, OUT uuid_id uuid) AS
  326 +$$
  327 +BEGIN
  328 + uuid_id := substring(entity_id, 8, 8) || '-' || substring(entity_id, 4, 4) || '-1' || substring(entity_id, 1, 3) ||
  329 + '-' || substring(entity_id, 16, 4) || '-' || substring(entity_id, 20, 12);
  330 +END;
  331 +$$ LANGUAGE plpgsql;