Commit e364b865d6410707b9c2efa8428441e8f82ee8c0

Authored by Igor Kulikov
Committed by GitHub
2 parents bf920c0b 4d4e4940

Merge pull request #3292 from YevhenBondarenko/master

fix ts latest 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;
... ... @@ -61,9 +65,6 @@ public class CassandraTsLatestToSqlMigrateService implements TsLatestMigrateServ
61 65 private static final int MAX_STR_V_LENGTH = 10000000;
62 66
63 67 @Autowired
64   - private EntityDatabaseSchemaService entityDatabaseSchemaService;
65   -
66   - @Autowired
67 68 private InsertLatestTsRepository insertLatestTsRepository;
68 69
69 70 @Autowired
... ... @@ -72,6 +73,9 @@ public class CassandraTsLatestToSqlMigrateService implements TsLatestMigrateServ
72 73 @Autowired
73 74 protected TsKvDictionaryRepository dictionaryRepository;
74 75
  76 + @Autowired
  77 + private InstallScripts installScripts;
  78 +
75 79 @Value("${spring.datasource.url}")
76 80 protected String dbUrl;
77 81
... ... @@ -88,8 +92,9 @@ public class CassandraTsLatestToSqlMigrateService implements TsLatestMigrateServ
88 92 @Override
89 93 public void migrate() throws Exception {
90 94 log.info("Performing migration of latest timeseries data from cassandra to SQL database ...");
91   - entityDatabaseSchemaService.createDatabaseSchema(false);
92 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);
93 98 conn.setAutoCommit(false);
94 99 for (CassandraToSqlTable table : tables) {
95 100 table.migrateToSql(cluster.getSession(), conn);
... ... @@ -219,4 +224,10 @@ public class CassandraTsLatestToSqlMigrateService implements TsLatestMigrateServ
219 224 }
220 225 return keyId;
221 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 + }
222 233 }
... ...