From 4d4e4940d22ad2c83309068c848e2290bb1bef7f Mon Sep 17 00:00:00 2001 From: YevhenBondarenko Date: Wed, 12 Aug 2020 14:59:46 +0300 Subject: [PATCH] added schema ts latest for migration --- .../data/upgrade/3.0.1/schema_ts_latest.sql | 35 +++++++++++++++++++ .../CassandraTsLatestToSqlMigrateService.java | 17 ++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 application/src/main/data/upgrade/3.0.1/schema_ts_latest.sql diff --git a/application/src/main/data/upgrade/3.0.1/schema_ts_latest.sql b/application/src/main/data/upgrade/3.0.1/schema_ts_latest.sql new file mode 100644 index 0000000000..bcdd1a72d4 --- /dev/null +++ b/application/src/main/data/upgrade/3.0.1/schema_ts_latest.sql @@ -0,0 +1,35 @@ +-- +-- Copyright © 2016-2020 The Thingsboard Authors +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- + +CREATE TABLE IF NOT EXISTS ts_kv_latest +( + entity_id uuid NOT NULL, + key int NOT NULL, + ts bigint NOT NULL, + bool_v boolean, + str_v varchar(10000000), + long_v bigint, + dbl_v double precision, + json_v json, + CONSTRAINT ts_kv_latest_pkey PRIMARY KEY (entity_id, key) +); + +CREATE TABLE IF NOT EXISTS ts_kv_dictionary +( + key varchar(255) NOT NULL, + key_id serial UNIQUE, + CONSTRAINT ts_key_id_pkey PRIMARY KEY (key) +); \ No newline at end of file diff --git a/application/src/main/java/org/thingsboard/server/service/install/migrate/CassandraTsLatestToSqlMigrateService.java b/application/src/main/java/org/thingsboard/server/service/install/migrate/CassandraTsLatestToSqlMigrateService.java index c4f3e5f393..f1fa9a43f0 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/migrate/CassandraTsLatestToSqlMigrateService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/migrate/CassandraTsLatestToSqlMigrateService.java @@ -31,8 +31,12 @@ import org.thingsboard.server.dao.sqlts.dictionary.TsKvDictionaryRepository; import org.thingsboard.server.dao.sqlts.insert.latest.InsertLatestTsRepository; import org.thingsboard.server.dao.util.NoSqlTsDao; import org.thingsboard.server.dao.util.SqlTsLatestDao; -import org.thingsboard.server.service.install.EntityDatabaseSchemaService; +import org.thingsboard.server.service.install.InstallScripts; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.sql.Connection; import java.sql.DriverManager; import java.util.Arrays; @@ -69,6 +73,9 @@ public class CassandraTsLatestToSqlMigrateService implements TsLatestMigrateServ @Autowired protected TsKvDictionaryRepository dictionaryRepository; + @Autowired + private InstallScripts installScripts; + @Value("${spring.datasource.url}") protected String dbUrl; @@ -86,6 +93,8 @@ public class CassandraTsLatestToSqlMigrateService implements TsLatestMigrateServ public void migrate() throws Exception { log.info("Performing migration of latest timeseries data from cassandra to SQL database ..."); try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) { + Path schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "3.0.1", "schema_ts_latest.sql"); + loadSql(schemaUpdateFile, conn); conn.setAutoCommit(false); for (CassandraToSqlTable table : tables) { table.migrateToSql(cluster.getSession(), conn); @@ -215,4 +224,10 @@ public class CassandraTsLatestToSqlMigrateService implements TsLatestMigrateServ } return keyId; } + + private void loadSql(Path sqlFile, Connection conn) throws Exception { + String sql = new String(Files.readAllBytes(sqlFile), Charset.forName("UTF-8")); + conn.createStatement().execute(sql); //NOSONAR, ignoring because method used to execute thingsboard database upgrade script + Thread.sleep(5000); + } }