Browse Source

Merge pull request #3292 from YevhenBondarenko/master

fix ts latest migration
pull/3314/head
Igor Kulikov 6 years ago
committed by GitHub
parent
commit
e364b865d6
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 35
      application/src/main/data/upgrade/3.0.1/schema_ts_latest.sql
  2. 21
      application/src/main/java/org/thingsboard/server/service/install/migrate/CassandraTsLatestToSqlMigrateService.java

35
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)
);

21
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;
@ -60,9 +64,6 @@ public class CassandraTsLatestToSqlMigrateService implements TsLatestMigrateServ
private static final int MAX_KEY_LENGTH = 255;
private static final int MAX_STR_V_LENGTH = 10000000;
@Autowired
private EntityDatabaseSchemaService entityDatabaseSchemaService;
@Autowired
private InsertLatestTsRepository insertLatestTsRepository;
@ -72,6 +73,9 @@ public class CassandraTsLatestToSqlMigrateService implements TsLatestMigrateServ
@Autowired
protected TsKvDictionaryRepository dictionaryRepository;
@Autowired
private InstallScripts installScripts;
@Value("${spring.datasource.url}")
protected String dbUrl;
@ -88,8 +92,9 @@ public class CassandraTsLatestToSqlMigrateService implements TsLatestMigrateServ
@Override
public void migrate() throws Exception {
log.info("Performing migration of latest timeseries data from cassandra to SQL database ...");
entityDatabaseSchemaService.createDatabaseSchema(false);
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);
@ -219,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);
}
}

Loading…
Cancel
Save