Commit 4d4e4940d22ad2c83309068c848e2290bb1bef7f

Authored by YevhenBondarenko
1 parent 70c3494c

added schema ts latest for migration

  1 +--
  2 +-- Copyright © 2016-2020 The Thingsboard Authors
  3 +--
  4 +-- Licensed under the Apache License, Version 2.0 (the "License");
  5 +-- you may not use this file except in compliance with the License.
  6 +-- You may obtain a copy of the License at
  7 +--
  8 +-- http://www.apache.org/licenses/LICENSE-2.0
  9 +--
  10 +-- Unless required by applicable law or agreed to in writing, software
  11 +-- distributed under the License is distributed on an "AS IS" BASIS,
  12 +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 +-- See the License for the specific language governing permissions and
  14 +-- limitations under the License.
  15 +--
  16 +
  17 +CREATE TABLE IF NOT EXISTS ts_kv_latest
  18 +(
  19 + entity_id uuid NOT NULL,
  20 + key int NOT NULL,
  21 + ts bigint NOT NULL,
  22 + bool_v boolean,
  23 + str_v varchar(10000000),
  24 + long_v bigint,
  25 + dbl_v double precision,
  26 + json_v json,
  27 + CONSTRAINT ts_kv_latest_pkey PRIMARY KEY (entity_id, key)
  28 +);
  29 +
  30 +CREATE TABLE IF NOT EXISTS ts_kv_dictionary
  31 +(
  32 + key varchar(255) NOT NULL,
  33 + key_id serial UNIQUE,
  34 + CONSTRAINT ts_key_id_pkey PRIMARY KEY (key)
  35 +);
\ No newline at end of file
... ...
... ... @@ -31,8 +31,12 @@ import org.thingsboard.server.dao.sqlts.dictionary.TsKvDictionaryRepository;
31 31 import org.thingsboard.server.dao.sqlts.insert.latest.InsertLatestTsRepository;
32 32 import org.thingsboard.server.dao.util.NoSqlTsDao;
33 33 import org.thingsboard.server.dao.util.SqlTsLatestDao;
34   -import org.thingsboard.server.service.install.EntityDatabaseSchemaService;
  34 +import org.thingsboard.server.service.install.InstallScripts;
35 35
  36 +import java.nio.charset.Charset;
  37 +import java.nio.file.Files;
  38 +import java.nio.file.Path;
  39 +import java.nio.file.Paths;
36 40 import java.sql.Connection;
37 41 import java.sql.DriverManager;
38 42 import java.util.Arrays;
... ... @@ -69,6 +73,9 @@ public class CassandraTsLatestToSqlMigrateService implements TsLatestMigrateServ
69 73 @Autowired
70 74 protected TsKvDictionaryRepository dictionaryRepository;
71 75
  76 + @Autowired
  77 + private InstallScripts installScripts;
  78 +
72 79 @Value("${spring.datasource.url}")
73 80 protected String dbUrl;
74 81
... ... @@ -86,6 +93,8 @@ public class CassandraTsLatestToSqlMigrateService implements TsLatestMigrateServ
86 93 public void migrate() throws Exception {
87 94 log.info("Performing migration of latest timeseries data from cassandra to SQL database ...");
88 95 try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
  96 + Path schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "3.0.1", "schema_ts_latest.sql");
  97 + loadSql(schemaUpdateFile, conn);
89 98 conn.setAutoCommit(false);
90 99 for (CassandraToSqlTable table : tables) {
91 100 table.migrateToSql(cluster.getSession(), conn);
... ... @@ -215,4 +224,10 @@ public class CassandraTsLatestToSqlMigrateService implements TsLatestMigrateServ
215 224 }
216 225 return keyId;
217 226 }
  227 +
  228 + private void loadSql(Path sqlFile, Connection conn) throws Exception {
  229 + String sql = new String(Files.readAllBytes(sqlFile), Charset.forName("UTF-8"));
  230 + conn.createStatement().execute(sql); //NOSONAR, ignoring because method used to execute thingsboard database upgrade script
  231 + Thread.sleep(5000);
  232 + }
218 233 }
... ...