Commit 562bbbe849ac80d62f48bcc229d869b55b2d220c

Authored by Igor Kulikov
1 parent 84460a00

Fix upgrade scripts.

... ... @@ -14,6 +14,14 @@
14 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 25 CREATE OR REPLACE FUNCTION extract_ts(uuid UUID) RETURNS BIGINT AS
18 26 $$
19 27 DECLARE
... ... @@ -826,3 +834,23 @@ BEGIN
826 834 END IF;
827 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 156 }
157 157 case "2.5.1":
158 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 159 case "3.0.1":
163 160 log.info("Upgrading ThingsBoard from version 3.0.1 to 3.1.0 ...");
164 161 databaseEntitiesUpgradeService.upgradeDatabase("3.0.1");
  162 + log.info("Updating system data...");
  163 + systemDataLoaderService.updateSystemWidgets();
165 164 break;
166 165 default:
167 166 throw new RuntimeException("Unable to upgrade ThingsBoard, unsupported fromVersion: " + upgradeFromVersion);
... ...
... ... @@ -254,13 +254,9 @@ public class SqlDatabaseUpgradeService implements DatabaseEntitiesUpgradeService
254 254
255 255 conn.createStatement().execute("call drop_all_idx()");
256 256
257   - log.info("Updating alarm relations...");
  257 + log.info("Optimizing alarm relations...");
258 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 261 for (String table : tables) {
266 262 log.info("Updating table {}.", table);
... ... @@ -286,6 +282,10 @@ public class SqlDatabaseUpgradeService implements DatabaseEntitiesUpgradeService
286 282 conn.createStatement().execute("DROP PROCEDURE create_all_idx");
287 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 289 conn.createStatement().execute("UPDATE tb_schema_settings SET schema_version = 3001000;");
290 290
291 291 conn.createStatement().execute("VACUUM FULL");
... ...
... ... @@ -277,6 +277,26 @@ CREATE TABLE IF NOT EXISTS entity_view (
277 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 300 CREATE OR REPLACE PROCEDURE cleanup_events_by_ttl(IN ttl bigint, IN debug_ttl bigint, INOUT deleted bigint)
281 301 LANGUAGE plpgsql AS
282 302 $$
... ... @@ -301,3 +321,11 @@ BEGIN
301 321 deleted := ttl_deleted_count + debug_ttl_deleted_count;
302 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;
... ...