89 changed files with 4231 additions and 3650 deletions
@ -0,0 +1,48 @@ |
|||||
|
/** |
||||
|
* 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. |
||||
|
*/ |
||||
|
package org.thingsboard.server.service.install; |
||||
|
|
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.beans.factory.annotation.Qualifier; |
||||
|
import org.thingsboard.server.dao.cassandra.CassandraCluster; |
||||
|
import org.thingsboard.server.dao.cassandra.CassandraInstallCluster; |
||||
|
import org.thingsboard.server.service.install.cql.CQLStatementsParser; |
||||
|
|
||||
|
import java.nio.file.Path; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@Slf4j |
||||
|
public abstract class AbstractCassandraDatabaseUpgradeService { |
||||
|
@Autowired |
||||
|
protected CassandraCluster cluster; |
||||
|
|
||||
|
@Autowired |
||||
|
@Qualifier("CassandraInstallCluster") |
||||
|
private CassandraInstallCluster installCluster; |
||||
|
|
||||
|
protected void loadCql(Path cql) throws Exception { |
||||
|
List<String> statements = new CQLStatementsParser(cql).getStatements(); |
||||
|
statements.forEach(statement -> { |
||||
|
installCluster.getSession().execute(statement); |
||||
|
try { |
||||
|
Thread.sleep(2500); |
||||
|
} catch (InterruptedException e) { |
||||
|
} |
||||
|
}); |
||||
|
Thread.sleep(5000); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,56 @@ |
|||||
|
/** |
||||
|
* 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. |
||||
|
*/ |
||||
|
package org.thingsboard.server.service.install; |
||||
|
|
||||
|
import com.datastax.driver.core.exceptions.InvalidQueryException; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.context.annotation.Profile; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.thingsboard.server.dao.util.NoSqlTsDao; |
||||
|
|
||||
|
@Service |
||||
|
@NoSqlTsDao |
||||
|
@Profile("install") |
||||
|
@Slf4j |
||||
|
public class CassandraTsDatabaseUpgradeService extends AbstractCassandraDatabaseUpgradeService implements DatabaseTsUpgradeService { |
||||
|
|
||||
|
@Override |
||||
|
public void upgradeDatabase(String fromVersion) throws Exception { |
||||
|
switch (fromVersion) { |
||||
|
case "2.4.3": |
||||
|
log.info("Updating schema ..."); |
||||
|
String updateTsKvTableStmt = "alter table ts_kv_cf add json_v text"; |
||||
|
String updateTsKvLatestTableStmt = "alter table ts_kv_latest_cf add json_v text"; |
||||
|
|
||||
|
try { |
||||
|
log.info("Updating ts ..."); |
||||
|
cluster.getSession().execute(updateTsKvTableStmt); |
||||
|
Thread.sleep(2500); |
||||
|
log.info("Ts updated."); |
||||
|
log.info("Updating ts latest ..."); |
||||
|
cluster.getSession().execute(updateTsKvLatestTableStmt); |
||||
|
Thread.sleep(2500); |
||||
|
log.info("Ts latest updated."); |
||||
|
} catch (InvalidQueryException e) { |
||||
|
} |
||||
|
log.info("Schema updated."); |
||||
|
break; |
||||
|
default: |
||||
|
throw new RuntimeException("Unable to upgrade Cassandra database, unsupported fromVersion: " + fromVersion); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,55 @@ |
|||||
|
/** |
||||
|
* 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. |
||||
|
*/ |
||||
|
package org.thingsboard.server.utils; |
||||
|
|
||||
|
import com.google.gson.JsonElement; |
||||
|
import com.google.gson.JsonObject; |
||||
|
import com.google.gson.JsonParser; |
||||
|
import org.thingsboard.server.gen.transport.TransportProtos.KeyValueProto; |
||||
|
import java.util.List; |
||||
|
|
||||
|
public class JsonUtils { |
||||
|
|
||||
|
private static final JsonParser jsonParser = new JsonParser(); |
||||
|
|
||||
|
public static JsonObject getJsonObject(List<KeyValueProto> tsKv) { |
||||
|
JsonObject json = new JsonObject(); |
||||
|
for (KeyValueProto kv : tsKv) { |
||||
|
switch (kv.getType()) { |
||||
|
case BOOLEAN_V: |
||||
|
json.addProperty(kv.getKey(), kv.getBoolV()); |
||||
|
break; |
||||
|
case LONG_V: |
||||
|
json.addProperty(kv.getKey(), kv.getLongV()); |
||||
|
break; |
||||
|
case DOUBLE_V: |
||||
|
json.addProperty(kv.getKey(), kv.getDoubleV()); |
||||
|
break; |
||||
|
case STRING_V: |
||||
|
json.addProperty(kv.getKey(), kv.getStringV()); |
||||
|
break; |
||||
|
case JSON_V: |
||||
|
json.add(kv.getKey(), jsonParser.parse(kv.getJsonV())); |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
return json; |
||||
|
} |
||||
|
|
||||
|
public static JsonElement parse(String params) { |
||||
|
return jsonParser.parse(params); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,69 @@ |
|||||
|
/** |
||||
|
* 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. |
||||
|
*/ |
||||
|
package org.thingsboard.server.common.data.kv; |
||||
|
|
||||
|
import java.util.Objects; |
||||
|
import java.util.Optional; |
||||
|
|
||||
|
public class JsonDataEntry extends BasicKvEntry { |
||||
|
private final String value; |
||||
|
|
||||
|
public JsonDataEntry(String key, String value) { |
||||
|
super(key); |
||||
|
this.value = value; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public DataType getDataType() { |
||||
|
return DataType.JSON; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Optional<String> getJsonValue() { |
||||
|
return Optional.ofNullable(value); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public boolean equals(Object o) { |
||||
|
if (this == o) return true; |
||||
|
if (!(o instanceof JsonDataEntry)) return false; |
||||
|
if (!super.equals(o)) return false; |
||||
|
JsonDataEntry that = (JsonDataEntry) o; |
||||
|
return Objects.equals(value, that.value); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Object getValue() { |
||||
|
return value; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public int hashCode() { |
||||
|
return Objects.hash(super.hashCode(), value); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String toString() { |
||||
|
return "JsonDataEntry{" + |
||||
|
"value=" + value + |
||||
|
"} " + super.toString(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String getValueAsString() { |
||||
|
return value; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,251 @@ |
|||||
|
-- |
||||
|
-- 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 admin_settings ( |
||||
|
id varchar(31) NOT NULL CONSTRAINT admin_settings_pkey PRIMARY KEY, |
||||
|
json_value varchar, |
||||
|
key varchar(255) |
||||
|
); |
||||
|
|
||||
|
CREATE TABLE IF NOT EXISTS alarm ( |
||||
|
id varchar(31) NOT NULL CONSTRAINT alarm_pkey PRIMARY KEY, |
||||
|
ack_ts bigint, |
||||
|
clear_ts bigint, |
||||
|
additional_info varchar, |
||||
|
end_ts bigint, |
||||
|
originator_id varchar(31), |
||||
|
originator_type integer, |
||||
|
propagate boolean, |
||||
|
severity varchar(255), |
||||
|
start_ts bigint, |
||||
|
status varchar(255), |
||||
|
tenant_id varchar(31), |
||||
|
propagate_relation_types varchar, |
||||
|
type varchar(255) |
||||
|
); |
||||
|
|
||||
|
CREATE TABLE IF NOT EXISTS asset ( |
||||
|
id varchar(31) NOT NULL CONSTRAINT asset_pkey PRIMARY KEY, |
||||
|
additional_info varchar, |
||||
|
customer_id varchar(31), |
||||
|
name varchar(255), |
||||
|
label varchar(255), |
||||
|
search_text varchar(255), |
||||
|
tenant_id varchar(31), |
||||
|
type varchar(255), |
||||
|
CONSTRAINT asset_name_unq_key UNIQUE (tenant_id, name) |
||||
|
); |
||||
|
|
||||
|
CREATE TABLE IF NOT EXISTS audit_log ( |
||||
|
id varchar(31) NOT NULL CONSTRAINT audit_log_pkey PRIMARY KEY, |
||||
|
tenant_id varchar(31), |
||||
|
customer_id varchar(31), |
||||
|
entity_id varchar(31), |
||||
|
entity_type varchar(255), |
||||
|
entity_name varchar(255), |
||||
|
user_id varchar(31), |
||||
|
user_name varchar(255), |
||||
|
action_type varchar(255), |
||||
|
action_data varchar(1000000), |
||||
|
action_status varchar(255), |
||||
|
action_failure_details varchar(1000000) |
||||
|
); |
||||
|
|
||||
|
CREATE TABLE IF NOT EXISTS attribute_kv ( |
||||
|
entity_type varchar(255), |
||||
|
entity_id varchar(31), |
||||
|
attribute_type varchar(255), |
||||
|
attribute_key varchar(255), |
||||
|
bool_v boolean, |
||||
|
str_v varchar(10000000), |
||||
|
long_v bigint, |
||||
|
dbl_v double precision, |
||||
|
json_v varchar(10000000), |
||||
|
last_update_ts bigint, |
||||
|
CONSTRAINT attribute_kv_pkey PRIMARY KEY (entity_type, entity_id, attribute_type, attribute_key) |
||||
|
); |
||||
|
|
||||
|
CREATE TABLE IF NOT EXISTS component_descriptor ( |
||||
|
id varchar(31) NOT NULL CONSTRAINT component_descriptor_pkey PRIMARY KEY, |
||||
|
actions varchar(255), |
||||
|
clazz varchar UNIQUE, |
||||
|
configuration_descriptor varchar, |
||||
|
name varchar(255), |
||||
|
scope varchar(255), |
||||
|
search_text varchar(255), |
||||
|
type varchar(255) |
||||
|
); |
||||
|
|
||||
|
CREATE TABLE IF NOT EXISTS customer ( |
||||
|
id varchar(31) NOT NULL CONSTRAINT customer_pkey PRIMARY KEY, |
||||
|
additional_info varchar, |
||||
|
address varchar, |
||||
|
address2 varchar, |
||||
|
city varchar(255), |
||||
|
country varchar(255), |
||||
|
email varchar(255), |
||||
|
phone varchar(255), |
||||
|
search_text varchar(255), |
||||
|
state varchar(255), |
||||
|
tenant_id varchar(31), |
||||
|
title varchar(255), |
||||
|
zip varchar(255) |
||||
|
); |
||||
|
|
||||
|
CREATE TABLE IF NOT EXISTS dashboard ( |
||||
|
id varchar(31) NOT NULL CONSTRAINT dashboard_pkey PRIMARY KEY, |
||||
|
configuration varchar(10000000), |
||||
|
assigned_customers varchar(1000000), |
||||
|
search_text varchar(255), |
||||
|
tenant_id varchar(31), |
||||
|
title varchar(255) |
||||
|
); |
||||
|
|
||||
|
CREATE TABLE IF NOT EXISTS device ( |
||||
|
id varchar(31) NOT NULL CONSTRAINT device_pkey PRIMARY KEY, |
||||
|
additional_info varchar, |
||||
|
customer_id varchar(31), |
||||
|
type varchar(255), |
||||
|
name varchar(255), |
||||
|
label varchar(255), |
||||
|
search_text varchar(255), |
||||
|
tenant_id varchar(31), |
||||
|
CONSTRAINT device_name_unq_key UNIQUE (tenant_id, name) |
||||
|
); |
||||
|
|
||||
|
CREATE TABLE IF NOT EXISTS device_credentials ( |
||||
|
id varchar(31) NOT NULL CONSTRAINT device_credentials_pkey PRIMARY KEY, |
||||
|
credentials_id varchar, |
||||
|
credentials_type varchar(255), |
||||
|
credentials_value varchar, |
||||
|
device_id varchar(31), |
||||
|
CONSTRAINT device_credentials_id_unq_key UNIQUE (credentials_id) |
||||
|
); |
||||
|
|
||||
|
CREATE TABLE IF NOT EXISTS event ( |
||||
|
id varchar(31) NOT NULL CONSTRAINT event_pkey PRIMARY KEY, |
||||
|
body varchar(10000000), |
||||
|
entity_id varchar(31), |
||||
|
entity_type varchar(255), |
||||
|
event_type varchar(255), |
||||
|
event_uid varchar(255), |
||||
|
tenant_id varchar(31), |
||||
|
CONSTRAINT event_unq_key UNIQUE (tenant_id, entity_type, entity_id, event_type, event_uid) |
||||
|
); |
||||
|
|
||||
|
CREATE TABLE IF NOT EXISTS relation ( |
||||
|
from_id varchar(31), |
||||
|
from_type varchar(255), |
||||
|
to_id varchar(31), |
||||
|
to_type varchar(255), |
||||
|
relation_type_group varchar(255), |
||||
|
relation_type varchar(255), |
||||
|
additional_info varchar, |
||||
|
CONSTRAINT relation_pkey PRIMARY KEY (from_id, from_type, relation_type_group, relation_type, to_id, to_type) |
||||
|
); |
||||
|
|
||||
|
CREATE TABLE IF NOT EXISTS tb_user ( |
||||
|
id varchar(31) NOT NULL CONSTRAINT tb_user_pkey PRIMARY KEY, |
||||
|
additional_info varchar, |
||||
|
authority varchar(255), |
||||
|
customer_id varchar(31), |
||||
|
email varchar(255) UNIQUE, |
||||
|
first_name varchar(255), |
||||
|
last_name varchar(255), |
||||
|
search_text varchar(255), |
||||
|
tenant_id varchar(31) |
||||
|
); |
||||
|
|
||||
|
CREATE TABLE IF NOT EXISTS tenant ( |
||||
|
id varchar(31) NOT NULL CONSTRAINT tenant_pkey PRIMARY KEY, |
||||
|
additional_info varchar, |
||||
|
address varchar, |
||||
|
address2 varchar, |
||||
|
city varchar(255), |
||||
|
country varchar(255), |
||||
|
email varchar(255), |
||||
|
phone varchar(255), |
||||
|
region varchar(255), |
||||
|
search_text varchar(255), |
||||
|
state varchar(255), |
||||
|
title varchar(255), |
||||
|
zip varchar(255) |
||||
|
); |
||||
|
|
||||
|
CREATE TABLE IF NOT EXISTS user_credentials ( |
||||
|
id varchar(31) NOT NULL CONSTRAINT user_credentials_pkey PRIMARY KEY, |
||||
|
activate_token varchar(255) UNIQUE, |
||||
|
enabled boolean, |
||||
|
password varchar(255), |
||||
|
reset_token varchar(255) UNIQUE, |
||||
|
user_id varchar(31) UNIQUE |
||||
|
); |
||||
|
|
||||
|
CREATE TABLE IF NOT EXISTS widget_type ( |
||||
|
id varchar(31) NOT NULL CONSTRAINT widget_type_pkey PRIMARY KEY, |
||||
|
alias varchar(255), |
||||
|
bundle_alias varchar(255), |
||||
|
descriptor varchar(1000000), |
||||
|
name varchar(255), |
||||
|
tenant_id varchar(31) |
||||
|
); |
||||
|
|
||||
|
CREATE TABLE IF NOT EXISTS widgets_bundle ( |
||||
|
id varchar(31) NOT NULL CONSTRAINT widgets_bundle_pkey PRIMARY KEY, |
||||
|
alias varchar(255), |
||||
|
search_text varchar(255), |
||||
|
tenant_id varchar(31), |
||||
|
title varchar(255) |
||||
|
); |
||||
|
|
||||
|
CREATE TABLE IF NOT EXISTS rule_chain ( |
||||
|
id varchar(31) NOT NULL CONSTRAINT rule_chain_pkey PRIMARY KEY, |
||||
|
additional_info varchar, |
||||
|
configuration varchar(10000000), |
||||
|
name varchar(255), |
||||
|
first_rule_node_id varchar(31), |
||||
|
root boolean, |
||||
|
debug_mode boolean, |
||||
|
search_text varchar(255), |
||||
|
tenant_id varchar(31) |
||||
|
); |
||||
|
|
||||
|
CREATE TABLE IF NOT EXISTS rule_node ( |
||||
|
id varchar(31) NOT NULL CONSTRAINT rule_node_pkey PRIMARY KEY, |
||||
|
rule_chain_id varchar(31), |
||||
|
additional_info varchar, |
||||
|
configuration varchar(10000000), |
||||
|
type varchar(255), |
||||
|
name varchar(255), |
||||
|
debug_mode boolean, |
||||
|
search_text varchar(255) |
||||
|
); |
||||
|
|
||||
|
CREATE TABLE IF NOT EXISTS entity_view ( |
||||
|
id varchar(31) NOT NULL CONSTRAINT entity_view_pkey PRIMARY KEY, |
||||
|
entity_id varchar(31), |
||||
|
entity_type varchar(255), |
||||
|
tenant_id varchar(31), |
||||
|
customer_id varchar(31), |
||||
|
type varchar(255), |
||||
|
name varchar(255), |
||||
|
keys varchar(10000000), |
||||
|
start_ts bigint, |
||||
|
end_ts bigint, |
||||
|
search_text varchar(255), |
||||
|
additional_info varchar |
||||
|
); |
||||
File diff suppressed because it is too large
File diff suppressed because it is too large
Loading…
Reference in new issue