From b7ed7ea039e96e65218eba375a14d36d2091e7f0 Mon Sep 17 00:00:00 2001 From: hagaic Date: Tue, 21 Aug 2018 00:58:45 +0300 Subject: [PATCH 1/8] hybrid db initial commit --- .../install/ThingsboardInstallService.java | 2 +- .../CassandraDatabaseSchemaService.java | 4 ++-- .../CassandraDatabaseUpgradeService.java | 4 ++-- .../src/main/resources/thingsboard.yml | 7 +++++- .../dao/cassandra/CassandraCluster.java | 4 ++-- .../cassandra/CassandraInstallCluster.java | 4 ++-- .../dao/cassandra/CassandraQueryOptions.java | 4 ++-- .../dao/cassandra/CassandraSocketOptions.java | 4 ++-- .../dao/sql/timeseries/JpaTimeseriesDao.java | 3 ++- .../CassandraBaseTimeseriesDao.java | 3 ++- .../server/dao/util/BufferedRateLimiter.java | 2 +- .../server/dao/util/NoSqlAnyDao.java | 22 +++++++++++++++++++ .../server/dao/util/NoSqlTsDao.java | 22 +++++++++++++++++++ .../thingsboard/server/dao/util/SqlTsDao.java | 22 +++++++++++++++++++ 14 files changed, 90 insertions(+), 17 deletions(-) create mode 100644 dao/src/main/java/org/thingsboard/server/dao/util/NoSqlAnyDao.java create mode 100644 dao/src/main/java/org/thingsboard/server/dao/util/NoSqlTsDao.java create mode 100644 dao/src/main/java/org/thingsboard/server/dao/util/SqlTsDao.java diff --git a/application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java b/application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java index f863d0bb2a..d0add1aa89 100644 --- a/application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java +++ b/application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java @@ -116,7 +116,7 @@ public class ThingsboardInstallService { log.info("Installing DataBase schema..."); - databaseSchemaService.createDatabaseSchema(); + databaseSchemaService.createDatabaseSchema();//TODO log.info("Loading system data..."); diff --git a/application/src/main/java/org/thingsboard/server/service/install/CassandraDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/CassandraDatabaseSchemaService.java index dd76b21844..6eeeb0a620 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/CassandraDatabaseSchemaService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/CassandraDatabaseSchemaService.java @@ -20,7 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Service; import org.thingsboard.server.dao.cassandra.CassandraInstallCluster; -import org.thingsboard.server.dao.util.NoSqlDao; +import org.thingsboard.server.dao.util.NoSqlAnyDao; import org.thingsboard.server.service.install.cql.CQLStatementsParser; import java.nio.file.Path; @@ -28,7 +28,7 @@ import java.nio.file.Paths; import java.util.List; @Service -@NoSqlDao +@NoSqlAnyDao @Profile("install") @Slf4j public class CassandraDatabaseSchemaService implements DatabaseSchemaService { diff --git a/application/src/main/java/org/thingsboard/server/service/install/CassandraDatabaseUpgradeService.java b/application/src/main/java/org/thingsboard/server/service/install/CassandraDatabaseUpgradeService.java index f4ff92cf23..2cbd167242 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/CassandraDatabaseUpgradeService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/CassandraDatabaseUpgradeService.java @@ -23,7 +23,7 @@ import org.springframework.stereotype.Service; import org.thingsboard.server.dao.cassandra.CassandraCluster; import org.thingsboard.server.dao.cassandra.CassandraInstallCluster; import org.thingsboard.server.dao.dashboard.DashboardService; -import org.thingsboard.server.dao.util.NoSqlDao; +import org.thingsboard.server.dao.util.NoSqlAnyDao; import org.thingsboard.server.service.install.cql.CQLStatementsParser; import org.thingsboard.server.service.install.cql.CassandraDbHelper; @@ -45,7 +45,7 @@ import static org.thingsboard.server.service.install.DatabaseHelper.TENANT_ID; import static org.thingsboard.server.service.install.DatabaseHelper.TITLE; @Service -@NoSqlDao +@NoSqlAnyDao @Profile("install") @Slf4j public class CassandraDatabaseUpgradeService implements DatabaseUpgradeService { diff --git a/application/src/main/resources/thingsboard.yml b/application/src/main/resources/thingsboard.yml index 743a8607b5..5573c7538b 100644 --- a/application/src/main/resources/thingsboard.yml +++ b/application/src/main/resources/thingsboard.yml @@ -151,6 +151,7 @@ quota: # Enable Host API Limits enabled: "${QUOTA_TENANT_ENABLED:false}" # Array of whitelist tenants + # Array of whitelist tenants whitelist: "${QUOTA_TENANT_WHITELIST:}" # Array of blacklist tenants blacklist: "${QUOTA_HOST_BLACKLIST:}" @@ -160,6 +161,10 @@ quota: database: type: "${DATABASE_TYPE:sql}" # cassandra OR sql + entities: + type: "${DATABASE_TYPE:sql}" # cassandra OR sql + ts: + type: "${DATABASE_TYPE:cassandra}" # cassandra OR sql # Cassandra driver configuration parameters cassandra: @@ -206,7 +211,7 @@ cassandra: write_consistency_level: "${CASSANDRA_WRITE_CONSISTENCY_LEVEL:ONE}" default_fetch_size: "${CASSANDRA_DEFAULT_FETCH_SIZE:2000}" # Specify partitioning size for timestamp key-value storage. Example MINUTES, HOURS, DAYS, MONTHS,INDEFINITE - ts_key_value_partitioning: "${TS_KV_PARTITIONING:MONTHS}" + ts_key_value_partitioning: "${TS_KV_PARTITIONING:INDEFINITE}" ts_key_value_ttl: "${TS_KV_TTL:0}" buffer_size: "${CASSANDRA_QUERY_BUFFER_SIZE:200000}" concurrent_limit: "${CASSANDRA_QUERY_CONCURRENT_LIMIT:1000}" diff --git a/dao/src/main/java/org/thingsboard/server/dao/cassandra/CassandraCluster.java b/dao/src/main/java/org/thingsboard/server/dao/cassandra/CassandraCluster.java index 567523228a..19409ad146 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/cassandra/CassandraCluster.java +++ b/dao/src/main/java/org/thingsboard/server/dao/cassandra/CassandraCluster.java @@ -17,12 +17,12 @@ package org.thingsboard.server.dao.cassandra; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; -import org.thingsboard.server.dao.util.NoSqlDao; +import org.thingsboard.server.dao.util.NoSqlAnyDao; import javax.annotation.PostConstruct; @Component -@NoSqlDao +@NoSqlAnyDao public class CassandraCluster extends AbstractCassandraCluster { @Value("${cassandra.keyspace_name}") diff --git a/dao/src/main/java/org/thingsboard/server/dao/cassandra/CassandraInstallCluster.java b/dao/src/main/java/org/thingsboard/server/dao/cassandra/CassandraInstallCluster.java index 02968078f1..247a204ee5 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/cassandra/CassandraInstallCluster.java +++ b/dao/src/main/java/org/thingsboard/server/dao/cassandra/CassandraInstallCluster.java @@ -17,12 +17,12 @@ package org.thingsboard.server.dao.cassandra; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Component; -import org.thingsboard.server.dao.util.NoSqlDao; +import org.thingsboard.server.dao.util.NoSqlAnyDao; import javax.annotation.PostConstruct; @Component -@NoSqlDao +@NoSqlAnyDao @Profile("install") public class CassandraInstallCluster extends AbstractCassandraCluster { diff --git a/dao/src/main/java/org/thingsboard/server/dao/cassandra/CassandraQueryOptions.java b/dao/src/main/java/org/thingsboard/server/dao/cassandra/CassandraQueryOptions.java index 474cad7c6a..1f09342ca1 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/cassandra/CassandraQueryOptions.java +++ b/dao/src/main/java/org/thingsboard/server/dao/cassandra/CassandraQueryOptions.java @@ -21,14 +21,14 @@ import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; -import org.thingsboard.server.dao.util.NoSqlDao; +import org.thingsboard.server.dao.util.NoSqlAnyDao; import javax.annotation.PostConstruct; @Component @Configuration @Data -@NoSqlDao +@NoSqlAnyDao public class CassandraQueryOptions { @Value("${cassandra.query.default_fetch_size}") diff --git a/dao/src/main/java/org/thingsboard/server/dao/cassandra/CassandraSocketOptions.java b/dao/src/main/java/org/thingsboard/server/dao/cassandra/CassandraSocketOptions.java index 8171ccc07f..15263c8b34 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/cassandra/CassandraSocketOptions.java +++ b/dao/src/main/java/org/thingsboard/server/dao/cassandra/CassandraSocketOptions.java @@ -20,14 +20,14 @@ import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; -import org.thingsboard.server.dao.util.NoSqlDao; +import org.thingsboard.server.dao.util.NoSqlAnyDao; import javax.annotation.PostConstruct; @Component @Configuration @Data -@NoSqlDao +@NoSqlAnyDao public class CassandraSocketOptions { @Value("${cassandra.socket.connect_timeout}") diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/timeseries/JpaTimeseriesDao.java b/dao/src/main/java/org/thingsboard/server/dao/sql/timeseries/JpaTimeseriesDao.java index e5f145f0fd..df72943249 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/timeseries/JpaTimeseriesDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/timeseries/JpaTimeseriesDao.java @@ -42,6 +42,7 @@ import org.thingsboard.server.dao.sql.JpaAbstractDaoListeningExecutorService; import org.thingsboard.server.dao.timeseries.TimeseriesDao; import org.thingsboard.server.dao.timeseries.TsInsertExecutorType; import org.thingsboard.server.dao.util.SqlDao; +import org.thingsboard.server.dao.util.SqlTsDao; import javax.annotation.Nullable; import javax.annotation.PostConstruct; @@ -58,7 +59,7 @@ import static org.thingsboard.server.common.data.UUIDConverter.fromTimeUUID; @Component @Slf4j -@SqlDao +@SqlTsDao public class JpaTimeseriesDao extends JpaAbstractDaoListeningExecutorService implements TimeseriesDao { @Value("${sql.ts_inserts_executor_type}") diff --git a/dao/src/main/java/org/thingsboard/server/dao/timeseries/CassandraBaseTimeseriesDao.java b/dao/src/main/java/org/thingsboard/server/dao/timeseries/CassandraBaseTimeseriesDao.java index c025e64a31..be36d3b156 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/timeseries/CassandraBaseTimeseriesDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/timeseries/CassandraBaseTimeseriesDao.java @@ -47,6 +47,7 @@ import org.thingsboard.server.common.data.kv.TsKvQuery; import org.thingsboard.server.dao.model.ModelConstants; import org.thingsboard.server.dao.nosql.CassandraAbstractAsyncDao; import org.thingsboard.server.dao.util.NoSqlDao; +import org.thingsboard.server.dao.util.NoSqlTsDao; import javax.annotation.Nullable; import javax.annotation.PostConstruct; @@ -68,7 +69,7 @@ import static com.datastax.driver.core.querybuilder.QueryBuilder.eq; */ @Component @Slf4j -@NoSqlDao +@NoSqlTsDao public class CassandraBaseTimeseriesDao extends CassandraAbstractAsyncDao implements TimeseriesDao { private static final int MIN_AGGREGATION_STEP_MS = 1000; diff --git a/dao/src/main/java/org/thingsboard/server/dao/util/BufferedRateLimiter.java b/dao/src/main/java/org/thingsboard/server/dao/util/BufferedRateLimiter.java index 817845b54a..eab05b2fdb 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/util/BufferedRateLimiter.java +++ b/dao/src/main/java/org/thingsboard/server/dao/util/BufferedRateLimiter.java @@ -34,7 +34,7 @@ import java.util.concurrent.atomic.AtomicInteger; @Component @Slf4j -@NoSqlDao +@NoSqlAnyDao public class BufferedRateLimiter implements AsyncRateLimiter { private final ListeningExecutorService pool = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10)); diff --git a/dao/src/main/java/org/thingsboard/server/dao/util/NoSqlAnyDao.java b/dao/src/main/java/org/thingsboard/server/dao/util/NoSqlAnyDao.java new file mode 100644 index 0000000000..7e7aefa3c3 --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/util/NoSqlAnyDao.java @@ -0,0 +1,22 @@ +/** + * Copyright © 2016-2018 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.dao.util; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; + +@ConditionalOnExpression("'${database.type}'=='cassandra' || '${database.ts.type}'=='cassandra'") +public @interface NoSqlAnyDao { +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/util/NoSqlTsDao.java b/dao/src/main/java/org/thingsboard/server/dao/util/NoSqlTsDao.java new file mode 100644 index 0000000000..748365fe41 --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/util/NoSqlTsDao.java @@ -0,0 +1,22 @@ +/** + * Copyright © 2016-2018 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.dao.util; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; + +@ConditionalOnProperty(prefix = "database.ts", value = "type", havingValue = "cassandra") +public @interface NoSqlTsDao { +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/util/SqlTsDao.java b/dao/src/main/java/org/thingsboard/server/dao/util/SqlTsDao.java new file mode 100644 index 0000000000..0470486408 --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/util/SqlTsDao.java @@ -0,0 +1,22 @@ +/** + * Copyright © 2016-2018 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.dao.util; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; + +@ConditionalOnProperty(prefix = "database.ts", value = "type", havingValue = "sql") +public @interface SqlTsDao { +} From c9fc57b3fd88137ac48c2fa9d47cf12a7703993a Mon Sep 17 00:00:00 2001 From: hagaic Date: Tue, 21 Aug 2018 02:05:45 +0300 Subject: [PATCH 2/8] make hybrid db optional --- .../thingsboard/server/install/ThingsboardInstallService.java | 2 +- application/src/main/resources/thingsboard.yml | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java b/application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java index d0add1aa89..884afc06c9 100644 --- a/application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java +++ b/application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java @@ -116,7 +116,7 @@ public class ThingsboardInstallService { log.info("Installing DataBase schema..."); - databaseSchemaService.createDatabaseSchema();//TODO + databaseSchemaService.createDatabaseSchema();//TODO issue 1005 - create both SQL and C* schemas in hybrid mode log.info("Loading system data..."); diff --git a/application/src/main/resources/thingsboard.yml b/application/src/main/resources/thingsboard.yml index 5573c7538b..a379e472aa 100644 --- a/application/src/main/resources/thingsboard.yml +++ b/application/src/main/resources/thingsboard.yml @@ -164,7 +164,8 @@ database: entities: type: "${DATABASE_TYPE:sql}" # cassandra OR sql ts: - type: "${DATABASE_TYPE:cassandra}" # cassandra OR sql + type: "${DATABASE_TYPE:sql}" # cassandra OR sql (for hybrid mode, only this value should be cassandra) + # Cassandra driver configuration parameters cassandra: From 4976a5326f5953a11a8edb80261aa21ffc9add10 Mon Sep 17 00:00:00 2001 From: hagaic Date: Wed, 22 Aug 2018 04:14:19 +0300 Subject: [PATCH 3/8] install hybrid db schema --- .../install/ThingsboardInstallService.java | 2 +- ...assandraAbstractDatabaseSchemaService.java | 56 ++ .../CassandraDatabaseSchemaService.java | 28 +- .../CassandraEntityDatabaseSchemaService.java | 25 + .../CassandraTsDatabaseSchemaService.java | 25 + .../install/HybridDatabaseSchemaService.java | 44 ++ .../SqlAbstractDatabaseSchemaService.java | 65 ++ .../install/SqlDatabaseSchemaService.java | 34 +- .../SqlEntityDatabaseSchemaService.java | 25 + .../install/SqlTsDatabaseSchemaService.java | 25 + .../server/dao/util/HybridDao.java | 22 + .../resources/cassandra/schema-entities.cql | 605 ++++++++++++++++++ .../main/resources/cassandra/schema-ts.cql | 55 ++ .../main/resources/sql/schema-entities.sql | 229 +++++++ dao/src/main/resources/sql/schema-ts.sql | 39 ++ 15 files changed, 1229 insertions(+), 50 deletions(-) create mode 100644 application/src/main/java/org/thingsboard/server/service/install/CassandraAbstractDatabaseSchemaService.java create mode 100644 application/src/main/java/org/thingsboard/server/service/install/CassandraEntityDatabaseSchemaService.java create mode 100644 application/src/main/java/org/thingsboard/server/service/install/CassandraTsDatabaseSchemaService.java create mode 100644 application/src/main/java/org/thingsboard/server/service/install/HybridDatabaseSchemaService.java create mode 100644 application/src/main/java/org/thingsboard/server/service/install/SqlAbstractDatabaseSchemaService.java create mode 100644 application/src/main/java/org/thingsboard/server/service/install/SqlEntityDatabaseSchemaService.java create mode 100644 application/src/main/java/org/thingsboard/server/service/install/SqlTsDatabaseSchemaService.java create mode 100644 dao/src/main/java/org/thingsboard/server/dao/util/HybridDao.java create mode 100644 dao/src/main/resources/cassandra/schema-entities.cql create mode 100644 dao/src/main/resources/cassandra/schema-ts.cql create mode 100644 dao/src/main/resources/sql/schema-entities.sql create mode 100644 dao/src/main/resources/sql/schema-ts.sql diff --git a/application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java b/application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java index 884afc06c9..f863d0bb2a 100644 --- a/application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java +++ b/application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java @@ -116,7 +116,7 @@ public class ThingsboardInstallService { log.info("Installing DataBase schema..."); - databaseSchemaService.createDatabaseSchema();//TODO issue 1005 - create both SQL and C* schemas in hybrid mode + databaseSchemaService.createDatabaseSchema(); log.info("Loading system data..."); diff --git a/application/src/main/java/org/thingsboard/server/service/install/CassandraAbstractDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/CassandraAbstractDatabaseSchemaService.java new file mode 100644 index 0000000000..d8d47444e5 --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/service/install/CassandraAbstractDatabaseSchemaService.java @@ -0,0 +1,56 @@ +/** + * Copyright © 2016-2018 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.thingsboard.server.dao.cassandra.CassandraInstallCluster; +import org.thingsboard.server.service.install.cql.CQLStatementsParser; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; + +@Slf4j +public abstract class CassandraAbstractDatabaseSchemaService /*implements DatabaseSchemaService*/ { + + private static final String CASSANDRA_DIR = "cassandra"; + + @Autowired + private CassandraInstallCluster cluster; + + @Autowired + private InstallScripts installScripts; + + private final String schemaCql; + + protected CassandraAbstractDatabaseSchemaService(String schemaCql) { + this.schemaCql = schemaCql; + } + + //@Override + public void createDatabaseSchema() throws Exception { + log.info("Installing Cassandra DataBase schema part: " + schemaCql); + Path schemaFile = Paths.get(installScripts.getDataDir(), CASSANDRA_DIR, schemaCql); + loadCql(schemaFile); + + } + + private void loadCql(Path cql) throws Exception { + List statements = new CQLStatementsParser(cql).getStatements(); + statements.forEach(statement -> cluster.getSession().execute(statement)); + } +} diff --git a/application/src/main/java/org/thingsboard/server/service/install/CassandraDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/CassandraDatabaseSchemaService.java index 6eeeb0a620..0735a59fca 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/CassandraDatabaseSchemaService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/CassandraDatabaseSchemaService.java @@ -19,39 +19,25 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Service; -import org.thingsboard.server.dao.cassandra.CassandraInstallCluster; -import org.thingsboard.server.dao.util.NoSqlAnyDao; -import org.thingsboard.server.service.install.cql.CQLStatementsParser; - -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.List; +import org.thingsboard.server.dao.util.NoSqlDao; @Service -@NoSqlAnyDao +@NoSqlDao @Profile("install") @Slf4j public class CassandraDatabaseSchemaService implements DatabaseSchemaService { - private static final String CASSANDRA_DIR = "cassandra"; - private static final String SCHEMA_CQL = "schema.cql"; + @Autowired + private CassandraEntityDatabaseSchemaService cassandraEntityDatabaseSchemaService; @Autowired - private CassandraInstallCluster cluster; + private CassandraTsDatabaseSchemaService cassandraTsDatabaseSchemaService; - @Autowired - private InstallScripts installScripts; @Override public void createDatabaseSchema() throws Exception { log.info("Installing Cassandra DataBase schema..."); - Path schemaFile = Paths.get(installScripts.getDataDir(), CASSANDRA_DIR, SCHEMA_CQL); - loadCql(schemaFile); - - } - - private void loadCql(Path cql) throws Exception { - List statements = new CQLStatementsParser(cql).getStatements(); - statements.forEach(statement -> cluster.getSession().execute(statement)); + cassandraEntityDatabaseSchemaService.createDatabaseSchema(); + cassandraTsDatabaseSchemaService.createDatabaseSchema(); } } diff --git a/application/src/main/java/org/thingsboard/server/service/install/CassandraEntityDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/CassandraEntityDatabaseSchemaService.java new file mode 100644 index 0000000000..9e4afb1a2a --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/service/install/CassandraEntityDatabaseSchemaService.java @@ -0,0 +1,25 @@ +/** + * Copyright © 2016-2018 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 org.springframework.stereotype.Service; + +@Service +public class CassandraEntityDatabaseSchemaService extends CassandraAbstractDatabaseSchemaService { + public CassandraEntityDatabaseSchemaService() { + super("schema-entities.cql"); + } +} diff --git a/application/src/main/java/org/thingsboard/server/service/install/CassandraTsDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/CassandraTsDatabaseSchemaService.java new file mode 100644 index 0000000000..addc180a8a --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/service/install/CassandraTsDatabaseSchemaService.java @@ -0,0 +1,25 @@ +/** + * Copyright © 2016-2018 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 org.springframework.stereotype.Service; + +@Service +public class CassandraTsDatabaseSchemaService extends CassandraAbstractDatabaseSchemaService { + public CassandraTsDatabaseSchemaService() { + super("schema-ts.cql"); + } +} diff --git a/application/src/main/java/org/thingsboard/server/service/install/HybridDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/HybridDatabaseSchemaService.java new file mode 100644 index 0000000000..e689c70109 --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/service/install/HybridDatabaseSchemaService.java @@ -0,0 +1,44 @@ +/** + * Copyright © 2016-2018 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.context.annotation.Profile; +import org.springframework.stereotype.Service; +import org.thingsboard.server.dao.util.HybridDao; + +@Service +@Profile("install") +@Slf4j +@HybridDao +public class HybridDatabaseSchemaService implements DatabaseSchemaService { + + @Autowired + private SqlEntityDatabaseSchemaService sqlEntityDatabaseSchemaService; + + @Autowired + private CassandraTsDatabaseSchemaService cassandraTsDatabaseSchemaService; + + + @Override + public void createDatabaseSchema() throws Exception { + log.info("Installing Hybrid SQL/Cassandra DataBase schema..."); + sqlEntityDatabaseSchemaService.createDatabaseSchema(); + cassandraTsDatabaseSchemaService.createDatabaseSchema(); + } + +} diff --git a/application/src/main/java/org/thingsboard/server/service/install/SqlAbstractDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/SqlAbstractDatabaseSchemaService.java new file mode 100644 index 0000000000..f468a6e475 --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/service/install/SqlAbstractDatabaseSchemaService.java @@ -0,0 +1,65 @@ +/** + * Copyright © 2016-2018 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.Value; + +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; + +@Slf4j +public abstract class SqlAbstractDatabaseSchemaService /*implements DatabaseSchemaService*/ { + + private static final String SQL_DIR = "sql"; + + @Value("${spring.datasource.url}") + private String dbUrl; + + @Value("${spring.datasource.username}") + private String dbUserName; + + @Value("${spring.datasource.password}") + private String dbPassword; + + @Autowired + private InstallScripts installScripts; + + private final String schemaSql; + + protected SqlAbstractDatabaseSchemaService(String schemaSql) { + this.schemaSql = schemaSql; + } + + //@Override + public void createDatabaseSchema() throws Exception { + + log.info("Installing SQL DataBase schema part: " + schemaSql); + + Path schemaFile = Paths.get(installScripts.getDataDir(), SQL_DIR, schemaSql); + try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) { + String sql = new String(Files.readAllBytes(schemaFile), Charset.forName("UTF-8")); + conn.createStatement().execute(sql); //NOSONAR, ignoring because method used to load initial thingsboard database schema + } + + } + +} diff --git a/application/src/main/java/org/thingsboard/server/service/install/SqlDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/SqlDatabaseSchemaService.java index 1daf66086a..8544b8bbf2 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/SqlDatabaseSchemaService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/SqlDatabaseSchemaService.java @@ -17,50 +17,28 @@ package org.thingsboard.server.service.install; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Service; import org.thingsboard.server.dao.util.SqlDao; -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; - @Service @Profile("install") @Slf4j @SqlDao public class SqlDatabaseSchemaService implements DatabaseSchemaService { - private static final String SQL_DIR = "sql"; - private static final String SCHEMA_SQL = "schema.sql"; - - @Value("${spring.datasource.url}") - private String dbUrl; - - @Value("${spring.datasource.username}") - private String dbUserName; - - @Value("${spring.datasource.password}") - private String dbPassword; + @Autowired + private SqlEntityDatabaseSchemaService sqlEntityDatabaseSchemaService; @Autowired - private InstallScripts installScripts; + private SqlTsDatabaseSchemaService sqlTsDatabaseSchemaService; + @Override public void createDatabaseSchema() throws Exception { - log.info("Installing SQL DataBase schema..."); - - Path schemaFile = Paths.get(installScripts.getDataDir(), SQL_DIR, SCHEMA_SQL); - try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) { - String sql = new String(Files.readAllBytes(schemaFile), Charset.forName("UTF-8")); - conn.createStatement().execute(sql); //NOSONAR, ignoring because method used to load initial thingsboard database schema - } - + sqlEntityDatabaseSchemaService.createDatabaseSchema(); + sqlTsDatabaseSchemaService.createDatabaseSchema(); } } diff --git a/application/src/main/java/org/thingsboard/server/service/install/SqlEntityDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/SqlEntityDatabaseSchemaService.java new file mode 100644 index 0000000000..0826099133 --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/service/install/SqlEntityDatabaseSchemaService.java @@ -0,0 +1,25 @@ +/** + * Copyright © 2016-2018 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 org.springframework.stereotype.Service; + +@Service +public class SqlEntityDatabaseSchemaService extends SqlAbstractDatabaseSchemaService { + public SqlEntityDatabaseSchemaService() { + super("schema-entities.sql"); + } +} diff --git a/application/src/main/java/org/thingsboard/server/service/install/SqlTsDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/SqlTsDatabaseSchemaService.java new file mode 100644 index 0000000000..6c8f8b5054 --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/service/install/SqlTsDatabaseSchemaService.java @@ -0,0 +1,25 @@ +/** + * Copyright © 2016-2018 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 org.springframework.stereotype.Service; + +@Service +public class SqlTsDatabaseSchemaService extends SqlAbstractDatabaseSchemaService { + public SqlTsDatabaseSchemaService() { + super("schema-ts.sql"); + } +} \ No newline at end of file diff --git a/dao/src/main/java/org/thingsboard/server/dao/util/HybridDao.java b/dao/src/main/java/org/thingsboard/server/dao/util/HybridDao.java new file mode 100644 index 0000000000..2caf8ccace --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/util/HybridDao.java @@ -0,0 +1,22 @@ +/** + * Copyright © 2016-2018 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.dao.util; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; + +@ConditionalOnExpression("'${database.entity.type}'=='sql' && '${database.ts.type}'=='cassandra'") +public @interface HybridDao { +} diff --git a/dao/src/main/resources/cassandra/schema-entities.cql b/dao/src/main/resources/cassandra/schema-entities.cql new file mode 100644 index 0000000000..e1a21ebc1a --- /dev/null +++ b/dao/src/main/resources/cassandra/schema-entities.cql @@ -0,0 +1,605 @@ +-- +-- Copyright © 2016-2018 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 KEYSPACE IF NOT EXISTS thingsboard +WITH replication = { + 'class' : 'SimpleStrategy', + 'replication_factor' : 1 +}; + +CREATE TABLE IF NOT EXISTS thingsboard.user ( + id timeuuid, + tenant_id timeuuid, + customer_id timeuuid, + email text, + search_text text, + authority text, + first_name text, + last_name text, + additional_info text, + PRIMARY KEY (id, tenant_id, customer_id, authority) +); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.user_by_email AS + SELECT * + from thingsboard.user + WHERE email IS NOT NULL AND tenant_id IS NOT NULL AND customer_id IS NOT NULL AND id IS NOT NULL AND authority IS NOT + NULL + PRIMARY KEY ( email, tenant_id, customer_id, id, authority ); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.user_by_tenant_and_search_text AS + SELECT * + from thingsboard.user + WHERE tenant_id IS NOT NULL AND customer_id IS NOT NULL AND authority IS NOT NULL AND search_text IS NOT NULL AND id + IS NOT NULL + PRIMARY KEY ( tenant_id, customer_id, authority, search_text, id ) + WITH CLUSTERING ORDER BY ( customer_id DESC, authority DESC, search_text ASC, id DESC ); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.user_by_customer_and_search_text AS + SELECT * + from thingsboard.user + WHERE tenant_id IS NOT NULL AND customer_id IS NOT NULL AND authority IS NOT NULL AND search_text IS NOT NULL AND id + IS NOT NULL + PRIMARY KEY ( customer_id, tenant_id, authority, search_text, id ) + WITH CLUSTERING ORDER BY ( tenant_id DESC, authority DESC, search_text ASC, id DESC ); + +CREATE TABLE IF NOT EXISTS thingsboard.user_credentials ( + id timeuuid PRIMARY KEY, + user_id timeuuid, + enabled boolean, + password text, + activate_token text, + reset_token text +); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.user_credentials_by_user AS + SELECT * + from thingsboard.user_credentials + WHERE user_id IS NOT NULL AND id IS NOT NULL + PRIMARY KEY ( user_id, id ); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.user_credentials_by_activate_token AS + SELECT * + from thingsboard.user_credentials + WHERE activate_token IS NOT NULL AND id IS NOT NULL + PRIMARY KEY ( activate_token, id ); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.user_credentials_by_reset_token AS + SELECT * + from thingsboard.user_credentials + WHERE reset_token IS NOT NULL AND id IS NOT NULL + PRIMARY KEY ( reset_token, id ); + +CREATE TABLE IF NOT EXISTS thingsboard.admin_settings ( + id timeuuid PRIMARY KEY, + key text, + json_value text +); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.admin_settings_by_key AS + SELECT * + from thingsboard.admin_settings + WHERE key IS NOT NULL AND id IS NOT NULL + PRIMARY KEY ( key, id ) + WITH CLUSTERING ORDER BY ( id DESC ); + +CREATE TABLE IF NOT EXISTS thingsboard.tenant ( + id timeuuid, + title text, + search_text text, + region text, + country text, + state text, + city text, + address text, + address2 text, + zip text, + phone text, + email text, + additional_info text, + PRIMARY KEY (id, region) +); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.tenant_by_region_and_search_text AS + SELECT * + from thingsboard.tenant + WHERE region IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL + PRIMARY KEY ( region, search_text, id ) + WITH CLUSTERING ORDER BY ( search_text ASC, id DESC ); + +CREATE TABLE IF NOT EXISTS thingsboard.customer ( + id timeuuid, + tenant_id timeuuid, + title text, + search_text text, + country text, + state text, + city text, + address text, + address2 text, + zip text, + phone text, + email text, + additional_info text, + PRIMARY KEY (id, tenant_id) +); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.customer_by_tenant_and_title AS + SELECT * + from thingsboard.customer + WHERE tenant_id IS NOT NULL AND title IS NOT NULL AND id IS NOT NULL + PRIMARY KEY ( tenant_id, title, id ) + WITH CLUSTERING ORDER BY ( title ASC, id DESC ); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.customer_by_tenant_and_search_text AS + SELECT * + from thingsboard.customer + WHERE tenant_id IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL + PRIMARY KEY ( tenant_id, search_text, id ) + WITH CLUSTERING ORDER BY ( search_text ASC, id DESC ); + +CREATE TABLE IF NOT EXISTS thingsboard.device ( + id timeuuid, + tenant_id timeuuid, + customer_id timeuuid, + name text, + type text, + search_text text, + additional_info text, + PRIMARY KEY (id, tenant_id, customer_id, type) +); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.device_by_tenant_and_name AS + SELECT * + from thingsboard.device + WHERE tenant_id IS NOT NULL AND customer_id IS NOT NULL AND type IS NOT NULL AND name IS NOT NULL AND id IS NOT NULL + PRIMARY KEY ( tenant_id, name, id, customer_id, type) + WITH CLUSTERING ORDER BY ( name ASC, id DESC, customer_id DESC); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.device_by_tenant_and_search_text AS + SELECT * + from thingsboard.device + WHERE tenant_id IS NOT NULL AND customer_id IS NOT NULL AND type IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL + PRIMARY KEY ( tenant_id, search_text, id, customer_id, type) + WITH CLUSTERING ORDER BY ( search_text ASC, id DESC, customer_id DESC); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.device_by_tenant_by_type_and_search_text AS + SELECT * + from thingsboard.device + WHERE tenant_id IS NOT NULL AND customer_id IS NOT NULL AND type IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL + PRIMARY KEY ( tenant_id, type, search_text, id, customer_id) + WITH CLUSTERING ORDER BY ( type ASC, search_text ASC, id DESC, customer_id DESC); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.device_by_customer_and_search_text AS + SELECT * + from thingsboard.device + WHERE tenant_id IS NOT NULL AND customer_id IS NOT NULL AND type IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL + PRIMARY KEY ( customer_id, tenant_id, search_text, id, type ) + WITH CLUSTERING ORDER BY ( tenant_id DESC, search_text ASC, id DESC ); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.device_by_customer_by_type_and_search_text AS + SELECT * + from thingsboard.device + WHERE tenant_id IS NOT NULL AND customer_id IS NOT NULL AND type IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL + PRIMARY KEY ( customer_id, tenant_id, type, search_text, id ) + WITH CLUSTERING ORDER BY ( tenant_id DESC, type ASC, search_text ASC, id DESC ); + +CREATE TABLE IF NOT EXISTS thingsboard.device_credentials ( + id timeuuid PRIMARY KEY, + device_id timeuuid, + credentials_type text, + credentials_id text, + credentials_value text +); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.device_credentials_by_device AS + SELECT * + from thingsboard.device_credentials + WHERE device_id IS NOT NULL AND id IS NOT NULL + PRIMARY KEY ( device_id, id ); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.device_credentials_by_credentials_id AS + SELECT * + from thingsboard.device_credentials + WHERE credentials_id IS NOT NULL AND id IS NOT NULL + PRIMARY KEY ( credentials_id, id ); + +CREATE TABLE IF NOT EXISTS thingsboard.asset ( + id timeuuid, + tenant_id timeuuid, + customer_id timeuuid, + name text, + type text, + search_text text, + additional_info text, + PRIMARY KEY (id, tenant_id, customer_id, type) +); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.asset_by_tenant_and_name AS + SELECT * + from thingsboard.asset + WHERE tenant_id IS NOT NULL AND customer_id IS NOT NULL AND type IS NOT NULL AND name IS NOT NULL AND id IS NOT NULL + PRIMARY KEY ( tenant_id, name, id, customer_id, type) + WITH CLUSTERING ORDER BY ( name ASC, id DESC, customer_id DESC); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.asset_by_tenant_and_search_text AS + SELECT * + from thingsboard.asset + WHERE tenant_id IS NOT NULL AND customer_id IS NOT NULL AND type IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL + PRIMARY KEY ( tenant_id, search_text, id, customer_id, type) + WITH CLUSTERING ORDER BY ( search_text ASC, id DESC, customer_id DESC); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.asset_by_tenant_by_type_and_search_text AS + SELECT * + from thingsboard.asset + WHERE tenant_id IS NOT NULL AND customer_id IS NOT NULL AND type IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL + PRIMARY KEY ( tenant_id, type, search_text, id, customer_id) + WITH CLUSTERING ORDER BY ( type ASC, search_text ASC, id DESC, customer_id DESC); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.asset_by_customer_and_search_text AS + SELECT * + from thingsboard.asset + WHERE tenant_id IS NOT NULL AND customer_id IS NOT NULL AND type IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL + PRIMARY KEY ( customer_id, tenant_id, search_text, id, type ) + WITH CLUSTERING ORDER BY ( tenant_id DESC, search_text ASC, id DESC ); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.asset_by_customer_by_type_and_search_text AS + SELECT * + from thingsboard.asset + WHERE tenant_id IS NOT NULL AND customer_id IS NOT NULL AND type IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL + PRIMARY KEY ( customer_id, tenant_id, type, search_text, id ) + WITH CLUSTERING ORDER BY ( tenant_id DESC, type ASC, search_text ASC, id DESC ); + +CREATE TABLE IF NOT EXISTS thingsboard.entity_subtype ( + tenant_id timeuuid, + entity_type text, // (DEVICE, ASSET) + type text, + PRIMARY KEY (tenant_id, entity_type, type) +); + +CREATE TABLE IF NOT EXISTS thingsboard.alarm ( + id timeuuid, + tenant_id timeuuid, + type text, + originator_id timeuuid, + originator_type text, + severity text, + status text, + start_ts bigint, + end_ts bigint, + ack_ts bigint, + clear_ts bigint, + details text, + propagate boolean, + PRIMARY KEY ((tenant_id, originator_id, originator_type), type, id) +) WITH CLUSTERING ORDER BY ( type ASC, id DESC); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.alarm_by_id AS + SELECT * + from thingsboard.alarm + WHERE tenant_id IS NOT NULL AND originator_id IS NOT NULL AND originator_type IS NOT NULL AND type IS NOT NULL + AND type IS NOT NULL AND id IS NOT NULL + PRIMARY KEY (id, tenant_id, originator_id, originator_type, type) + WITH CLUSTERING ORDER BY ( tenant_id ASC, originator_id ASC, originator_type ASC, type ASC); + +CREATE TABLE IF NOT EXISTS thingsboard.relation ( + from_id timeuuid, + from_type text, + to_id timeuuid, + to_type text, + relation_type_group text, + relation_type text, + additional_info text, + PRIMARY KEY ((from_id, from_type), relation_type_group, relation_type, to_id, to_type) +) WITH CLUSTERING ORDER BY ( relation_type_group ASC, relation_type ASC, to_id ASC, to_type ASC); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.relation_by_type_and_child_type AS + SELECT * + from thingsboard.relation + WHERE from_id IS NOT NULL AND from_type IS NOT NULL AND relation_type_group IS NOT NULL AND relation_type IS NOT NULL AND to_id IS NOT NULL AND to_type IS NOT NULL + PRIMARY KEY ((from_id, from_type), relation_type_group, relation_type, to_type, to_id) + WITH CLUSTERING ORDER BY ( relation_type_group ASC, relation_type ASC, to_type ASC, to_id DESC); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.reverse_relation AS + SELECT * + from thingsboard.relation + WHERE from_id IS NOT NULL AND from_type IS NOT NULL AND relation_type_group IS NOT NULL AND relation_type IS NOT NULL AND to_id IS NOT NULL AND to_type IS NOT NULL + PRIMARY KEY ((to_id, to_type), relation_type_group, relation_type, from_id, from_type) + WITH CLUSTERING ORDER BY ( relation_type_group ASC, relation_type ASC, from_id ASC, from_type ASC); + +CREATE TABLE IF NOT EXISTS thingsboard.widgets_bundle ( + id timeuuid, + tenant_id timeuuid, + alias text, + title text, + search_text text, + image blob, + PRIMARY KEY (id, tenant_id) +); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.widgets_bundle_by_tenant_and_search_text AS + SELECT * + from thingsboard.widgets_bundle + WHERE tenant_id IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL + PRIMARY KEY ( tenant_id, search_text, id ) + WITH CLUSTERING ORDER BY ( search_text ASC, id DESC ); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.widgets_bundle_by_tenant_and_alias AS + SELECT * + from thingsboard.widgets_bundle + WHERE tenant_id IS NOT NULL AND alias IS NOT NULL AND id IS NOT NULL + PRIMARY KEY ( tenant_id, alias, id ) + WITH CLUSTERING ORDER BY ( alias ASC, id DESC ); + +CREATE TABLE IF NOT EXISTS thingsboard.widget_type ( + id timeuuid, + tenant_id timeuuid, + bundle_alias text, + alias text, + name text, + descriptor text, + PRIMARY KEY (id, tenant_id, bundle_alias) +); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.widget_type_by_tenant_and_aliases AS + SELECT * + from thingsboard.widget_type + WHERE tenant_id IS NOT NULL AND bundle_alias IS NOT NULL AND alias IS NOT NULL AND id IS NOT NULL + PRIMARY KEY ( tenant_id, bundle_alias, alias, id ) + WITH CLUSTERING ORDER BY ( bundle_alias ASC, alias ASC, id DESC ); + +CREATE TABLE IF NOT EXISTS thingsboard.dashboard ( + id timeuuid, + tenant_id timeuuid, + title text, + search_text text, + assigned_customers text, + configuration text, + PRIMARY KEY (id, tenant_id) +); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.dashboard_by_tenant_and_search_text AS + SELECT * + from thingsboard.dashboard + WHERE tenant_id IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL + PRIMARY KEY ( tenant_id, search_text, id ) + WITH CLUSTERING ORDER BY ( search_text ASC, id DESC ); + +CREATE TABLE IF NOT EXISTS thingsboard.attributes_kv_cf ( + entity_type text, // (DEVICE, CUSTOMER, TENANT) + entity_id timeuuid, + attribute_type text, // (CLIENT_SIDE, SHARED, SERVER_SIDE) + attribute_key text, + bool_v boolean, + str_v text, + long_v bigint, + dbl_v double, + last_update_ts bigint, + PRIMARY KEY ((entity_type, entity_id, attribute_type), attribute_key) +) WITH compaction = { 'class' : 'LeveledCompactionStrategy' }; + +CREATE TABLE IF NOT EXISTS thingsboard.component_descriptor ( + id timeuuid, + type text, + scope text, + name text, + search_text text, + clazz text, + configuration_descriptor text, + actions text, + PRIMARY KEY (clazz, id, type, scope) +); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.component_desc_by_type_search_text AS + SELECT * + from thingsboard.component_descriptor + WHERE type IS NOT NULL AND scope IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL AND clazz IS NOT NULL + PRIMARY KEY ( type, search_text, id, clazz, scope) + WITH CLUSTERING ORDER BY ( search_text DESC); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.component_desc_by_scope_type_search_text AS + SELECT * + from thingsboard.component_descriptor + WHERE type IS NOT NULL AND scope IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL AND clazz IS NOT NULL + PRIMARY KEY ( (scope, type), search_text, id, clazz) + WITH CLUSTERING ORDER BY ( search_text DESC); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.component_desc_by_id AS + SELECT * + from thingsboard.component_descriptor + WHERE type IS NOT NULL AND scope IS NOT NULL AND id IS NOT NULL AND clazz IS NOT NULL + PRIMARY KEY ( id, clazz, scope, type ) + WITH CLUSTERING ORDER BY ( clazz ASC, scope ASC, type DESC); + +CREATE TABLE IF NOT EXISTS thingsboard.event ( + tenant_id timeuuid, // tenant or system + id timeuuid, + event_type text, + event_uid text, + entity_type text, + entity_id timeuuid, + body text, + PRIMARY KEY ((tenant_id, entity_type, entity_id), event_type, event_uid) +); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.event_by_type_and_id AS + SELECT * + FROM thingsboard.event + WHERE tenant_id IS NOT NULL AND entity_type IS NOT NULL AND entity_id IS NOT NULL AND id IS NOT NULL + AND event_type IS NOT NULL AND event_uid IS NOT NULL + PRIMARY KEY ((tenant_id, entity_type, entity_id), event_type, id, event_uid) + WITH CLUSTERING ORDER BY (event_type ASC, id ASC, event_uid ASC); + + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.event_by_id AS + SELECT * + FROM thingsboard.event + WHERE tenant_id IS NOT NULL AND entity_type IS NOT NULL AND entity_id IS NOT NULL AND id IS NOT NULL + AND event_type IS NOT NULL AND event_uid IS NOT NULL + PRIMARY KEY ((tenant_id, entity_type, entity_id), id, event_type, event_uid) + WITH CLUSTERING ORDER BY (id ASC, event_type ASC, event_uid ASC); + +CREATE TABLE IF NOT EXISTS thingsboard.audit_log_by_entity_id ( + tenant_id timeuuid, + id timeuuid, + customer_id timeuuid, + entity_id timeuuid, + entity_type text, + entity_name text, + user_id timeuuid, + user_name text, + action_type text, + action_data text, + action_status text, + action_failure_details text, + PRIMARY KEY ((tenant_id, entity_id, entity_type), id) +); + +CREATE TABLE IF NOT EXISTS thingsboard.audit_log_by_customer_id ( + tenant_id timeuuid, + id timeuuid, + customer_id timeuuid, + entity_id timeuuid, + entity_type text, + entity_name text, + user_id timeuuid, + user_name text, + action_type text, + action_data text, + action_status text, + action_failure_details text, + PRIMARY KEY ((tenant_id, customer_id), id) +); + +CREATE TABLE IF NOT EXISTS thingsboard.audit_log_by_user_id ( + tenant_id timeuuid, + id timeuuid, + customer_id timeuuid, + entity_id timeuuid, + entity_type text, + entity_name text, + user_id timeuuid, + user_name text, + action_type text, + action_data text, + action_status text, + action_failure_details text, + PRIMARY KEY ((tenant_id, user_id), id) +); + +CREATE TABLE IF NOT EXISTS thingsboard.audit_log_by_tenant_id ( + tenant_id timeuuid, + id timeuuid, + partition bigint, + customer_id timeuuid, + entity_id timeuuid, + entity_type text, + entity_name text, + user_id timeuuid, + user_name text, + action_type text, + action_data text, + action_status text, + action_failure_details text, + PRIMARY KEY ((tenant_id, partition), id) +); + +CREATE TABLE IF NOT EXISTS thingsboard.audit_log_by_tenant_id_partitions ( + tenant_id timeuuid, + partition bigint, + PRIMARY KEY (( tenant_id ), partition) +) WITH CLUSTERING ORDER BY ( partition ASC ) +AND compaction = { 'class' : 'LeveledCompactionStrategy' }; + +CREATE TABLE IF NOT EXISTS thingsboard.msg_queue ( + node_id timeuuid, + cluster_partition bigint, + ts_partition bigint, + ts bigint, + msg blob, + PRIMARY KEY ((node_id, cluster_partition, ts_partition), ts)) +WITH CLUSTERING ORDER BY (ts DESC) +AND compaction = { + 'class': 'org.apache.cassandra.db.compaction.DateTieredCompactionStrategy', + 'min_threshold': '5', + 'base_time_seconds': '43200', + 'max_window_size_seconds': '43200', + 'tombstone_threshold': '0.9', + 'unchecked_tombstone_compaction': 'true' +}; + +CREATE TABLE IF NOT EXISTS thingsboard.msg_ack_queue ( + node_id timeuuid, + cluster_partition bigint, + ts_partition bigint, + msg_id timeuuid, + PRIMARY KEY ((node_id, cluster_partition, ts_partition), msg_id)) +WITH CLUSTERING ORDER BY (msg_id DESC) +AND compaction = { + 'class': 'org.apache.cassandra.db.compaction.DateTieredCompactionStrategy', + 'min_threshold': '5', + 'base_time_seconds': '43200', + 'max_window_size_seconds': '43200', + 'tombstone_threshold': '0.9', + 'unchecked_tombstone_compaction': 'true' +}; + +CREATE TABLE IF NOT EXISTS thingsboard.processed_msg_partitions ( + node_id timeuuid, + cluster_partition bigint, + ts_partition bigint, + PRIMARY KEY ((node_id, cluster_partition), ts_partition)) +WITH CLUSTERING ORDER BY (ts_partition DESC) +AND compaction = { + 'class': 'org.apache.cassandra.db.compaction.DateTieredCompactionStrategy', + 'min_threshold': '5', + 'base_time_seconds': '43200', + 'max_window_size_seconds': '43200', + 'tombstone_threshold': '0.9', + 'unchecked_tombstone_compaction': 'true' +}; + +CREATE TABLE IF NOT EXISTS thingsboard.rule_chain ( + id uuid, + tenant_id uuid, + name text, + search_text text, + first_rule_node_id uuid, + root boolean, + debug_mode boolean, + configuration text, + additional_info text, + PRIMARY KEY (id, tenant_id) +); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.rule_chain_by_tenant_and_search_text AS + SELECT * + from thingsboard.rule_chain + WHERE tenant_id IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL + PRIMARY KEY ( tenant_id, search_text, id ) + WITH CLUSTERING ORDER BY ( search_text ASC, id DESC ); + +CREATE TABLE IF NOT EXISTS thingsboard.rule_node ( + id uuid, + rule_chain_id uuid, + type text, + name text, + debug_mode boolean, + search_text text, + configuration text, + additional_info text, + PRIMARY KEY (id) +); diff --git a/dao/src/main/resources/cassandra/schema-ts.cql b/dao/src/main/resources/cassandra/schema-ts.cql new file mode 100644 index 0000000000..a5c5ec2ff1 --- /dev/null +++ b/dao/src/main/resources/cassandra/schema-ts.cql @@ -0,0 +1,55 @@ +-- +-- Copyright © 2016-2018 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 KEYSPACE IF NOT EXISTS thingsboard +WITH replication = { + 'class' : 'SimpleStrategy', + 'replication_factor' : 1 +}; + +CREATE TABLE IF NOT EXISTS thingsboard.ts_kv_cf ( + entity_type text, // (DEVICE, CUSTOMER, TENANT) + entity_id timeuuid, + key text, + partition bigint, + ts bigint, + bool_v boolean, + str_v text, + long_v bigint, + dbl_v double, + PRIMARY KEY (( entity_type, entity_id, key, partition ), ts) +); + +CREATE TABLE IF NOT EXISTS thingsboard.ts_kv_partitions_cf ( + entity_type text, // (DEVICE, CUSTOMER, TENANT) + entity_id timeuuid, + key text, + partition bigint, + PRIMARY KEY (( entity_type, entity_id, key ), partition) +) WITH CLUSTERING ORDER BY ( partition ASC ) + AND compaction = { 'class' : 'LeveledCompactionStrategy' }; + +CREATE TABLE IF NOT EXISTS thingsboard.ts_kv_latest_cf ( + entity_type text, // (DEVICE, CUSTOMER, TENANT) + entity_id timeuuid, + key text, + ts bigint, + bool_v boolean, + str_v text, + long_v bigint, + dbl_v double, + PRIMARY KEY (( entity_type, entity_id ), key) +) WITH compaction = { 'class' : 'LeveledCompactionStrategy' }; diff --git a/dao/src/main/resources/sql/schema-entities.sql b/dao/src/main/resources/sql/schema-entities.sql new file mode 100644 index 0000000000..20efec8cd6 --- /dev/null +++ b/dao/src/main/resources/sql/schema-entities.sql @@ -0,0 +1,229 @@ +-- +-- Copyright © 2016-2018 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), + 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), + search_text varchar(255), + tenant_id varchar(31), + type varchar(255) +); + +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, + last_update_ts bigint, + CONSTRAINT attribute_kv_unq_key UNIQUE (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, + 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), + search_text varchar(255), + tenant_id varchar(31) +); + +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) +); + +CREATE TABLE IF NOT EXISTS event ( + id varchar(31) NOT NULL CONSTRAINT event_pkey PRIMARY KEY, + body varchar, + 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_unq_key UNIQUE (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) +); diff --git a/dao/src/main/resources/sql/schema-ts.sql b/dao/src/main/resources/sql/schema-ts.sql new file mode 100644 index 0000000000..53bc15acc3 --- /dev/null +++ b/dao/src/main/resources/sql/schema-ts.sql @@ -0,0 +1,39 @@ +-- +-- Copyright © 2016-2018 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 ( + entity_type varchar(255) NOT NULL, + entity_id varchar(31) NOT NULL, + key varchar(255) NOT NULL, + ts bigint NOT NULL, + bool_v boolean, + str_v varchar(10000000), + long_v bigint, + dbl_v double precision, + CONSTRAINT ts_kv_unq_key UNIQUE (entity_type, entity_id, key, ts) +); + +CREATE TABLE IF NOT EXISTS ts_kv_latest ( + entity_type varchar(255) NOT NULL, + entity_id varchar(31) NOT NULL, + key varchar(255) NOT NULL, + ts bigint NOT NULL, + bool_v boolean, + str_v varchar(10000000), + long_v bigint, + dbl_v double precision, + CONSTRAINT ts_kv_latest_unq_key UNIQUE (entity_type, entity_id, key) +); From 90e0d8965bc9b2d8886a6a9dbc87269dae4b02a0 Mon Sep 17 00:00:00 2001 From: hagaic Date: Wed, 22 Aug 2018 04:27:12 +0300 Subject: [PATCH 4/8] install hybrid db schema (simplified) --- .../install/ThingsboardInstallService.java | 16 +++++-- ...assandraAbstractDatabaseSchemaService.java | 4 +- .../CassandraDatabaseSchemaService.java | 43 ------------------ .../CassandraEntityDatabaseSchemaService.java | 5 ++- .../CassandraTsDatabaseSchemaService.java | 5 ++- .../install/EntityDatabaseSchemaService.java | 4 ++ .../install/HybridDatabaseSchemaService.java | 44 ------------------- .../SqlAbstractDatabaseSchemaService.java | 4 +- .../install/SqlDatabaseSchemaService.java | 44 ------------------- .../SqlEntityDatabaseSchemaService.java | 5 ++- .../install/SqlTsDatabaseSchemaService.java | 5 ++- .../install/TsDatabaseSchemaService.java | 4 ++ 12 files changed, 40 insertions(+), 143 deletions(-) delete mode 100644 application/src/main/java/org/thingsboard/server/service/install/CassandraDatabaseSchemaService.java create mode 100644 application/src/main/java/org/thingsboard/server/service/install/EntityDatabaseSchemaService.java delete mode 100644 application/src/main/java/org/thingsboard/server/service/install/HybridDatabaseSchemaService.java delete mode 100644 application/src/main/java/org/thingsboard/server/service/install/SqlDatabaseSchemaService.java create mode 100644 application/src/main/java/org/thingsboard/server/service/install/TsDatabaseSchemaService.java diff --git a/application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java b/application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java index f863d0bb2a..2a55abff65 100644 --- a/application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java +++ b/application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java @@ -24,9 +24,10 @@ import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Service; import org.thingsboard.server.service.component.ComponentDiscoveryService; import org.thingsboard.server.service.install.DataUpdateService; -import org.thingsboard.server.service.install.DatabaseSchemaService; import org.thingsboard.server.service.install.DatabaseUpgradeService; +import org.thingsboard.server.service.install.EntityDatabaseSchemaService; import org.thingsboard.server.service.install.SystemDataLoaderService; +import org.thingsboard.server.service.install.TsDatabaseSchemaService; @Service @Profile("install") @@ -43,7 +44,10 @@ public class ThingsboardInstallService { private Boolean loadDemo; @Autowired - private DatabaseSchemaService databaseSchemaService; + private EntityDatabaseSchemaService entityDatabaseSchemaService; + + @Autowired + private TsDatabaseSchemaService tsDatabaseSchemaService; @Autowired private DatabaseUpgradeService databaseUpgradeService; @@ -114,9 +118,13 @@ public class ThingsboardInstallService { log.info("Starting ThingsBoard Installation..."); - log.info("Installing DataBase schema..."); + log.info("Installing DataBase schema for entities..."); - databaseSchemaService.createDatabaseSchema(); + entityDatabaseSchemaService.createDatabaseSchema(); + + log.info("Installing DataBase schema for timeseries..."); + + tsDatabaseSchemaService.createDatabaseSchema(); log.info("Loading system data..."); diff --git a/application/src/main/java/org/thingsboard/server/service/install/CassandraAbstractDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/CassandraAbstractDatabaseSchemaService.java index d8d47444e5..10559ba2d2 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/CassandraAbstractDatabaseSchemaService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/CassandraAbstractDatabaseSchemaService.java @@ -25,7 +25,7 @@ import java.nio.file.Paths; import java.util.List; @Slf4j -public abstract class CassandraAbstractDatabaseSchemaService /*implements DatabaseSchemaService*/ { +public abstract class CassandraAbstractDatabaseSchemaService implements DatabaseSchemaService { private static final String CASSANDRA_DIR = "cassandra"; @@ -41,7 +41,7 @@ public abstract class CassandraAbstractDatabaseSchemaService /*implements Databa this.schemaCql = schemaCql; } - //@Override + @Override public void createDatabaseSchema() throws Exception { log.info("Installing Cassandra DataBase schema part: " + schemaCql); Path schemaFile = Paths.get(installScripts.getDataDir(), CASSANDRA_DIR, schemaCql); diff --git a/application/src/main/java/org/thingsboard/server/service/install/CassandraDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/CassandraDatabaseSchemaService.java deleted file mode 100644 index 0735a59fca..0000000000 --- a/application/src/main/java/org/thingsboard/server/service/install/CassandraDatabaseSchemaService.java +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright © 2016-2018 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.context.annotation.Profile; -import org.springframework.stereotype.Service; -import org.thingsboard.server.dao.util.NoSqlDao; - -@Service -@NoSqlDao -@Profile("install") -@Slf4j -public class CassandraDatabaseSchemaService implements DatabaseSchemaService { - - @Autowired - private CassandraEntityDatabaseSchemaService cassandraEntityDatabaseSchemaService; - - @Autowired - private CassandraTsDatabaseSchemaService cassandraTsDatabaseSchemaService; - - - @Override - public void createDatabaseSchema() throws Exception { - log.info("Installing Cassandra DataBase schema..."); - cassandraEntityDatabaseSchemaService.createDatabaseSchema(); - cassandraTsDatabaseSchemaService.createDatabaseSchema(); - } -} diff --git a/application/src/main/java/org/thingsboard/server/service/install/CassandraEntityDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/CassandraEntityDatabaseSchemaService.java index 9e4afb1a2a..7937ef23be 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/CassandraEntityDatabaseSchemaService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/CassandraEntityDatabaseSchemaService.java @@ -16,9 +16,12 @@ package org.thingsboard.server.service.install; import org.springframework.stereotype.Service; +import org.thingsboard.server.dao.util.NoSqlDao; @Service -public class CassandraEntityDatabaseSchemaService extends CassandraAbstractDatabaseSchemaService { +@NoSqlDao +public class CassandraEntityDatabaseSchemaService extends CassandraAbstractDatabaseSchemaService + implements EntityDatabaseSchemaService { public CassandraEntityDatabaseSchemaService() { super("schema-entities.cql"); } diff --git a/application/src/main/java/org/thingsboard/server/service/install/CassandraTsDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/CassandraTsDatabaseSchemaService.java index addc180a8a..ba18b57ff1 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/CassandraTsDatabaseSchemaService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/CassandraTsDatabaseSchemaService.java @@ -16,9 +16,12 @@ package org.thingsboard.server.service.install; import org.springframework.stereotype.Service; +import org.thingsboard.server.dao.util.NoSqlTsDao; @Service -public class CassandraTsDatabaseSchemaService extends CassandraAbstractDatabaseSchemaService { +@NoSqlTsDao +public class CassandraTsDatabaseSchemaService extends CassandraAbstractDatabaseSchemaService + implements TsDatabaseSchemaService { public CassandraTsDatabaseSchemaService() { super("schema-ts.cql"); } diff --git a/application/src/main/java/org/thingsboard/server/service/install/EntityDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/EntityDatabaseSchemaService.java new file mode 100644 index 0000000000..1d0ddc34a4 --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/service/install/EntityDatabaseSchemaService.java @@ -0,0 +1,4 @@ +package org.thingsboard.server.service.install; + +public interface EntityDatabaseSchemaService extends DatabaseSchemaService { +} diff --git a/application/src/main/java/org/thingsboard/server/service/install/HybridDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/HybridDatabaseSchemaService.java deleted file mode 100644 index e689c70109..0000000000 --- a/application/src/main/java/org/thingsboard/server/service/install/HybridDatabaseSchemaService.java +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Copyright © 2016-2018 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.context.annotation.Profile; -import org.springframework.stereotype.Service; -import org.thingsboard.server.dao.util.HybridDao; - -@Service -@Profile("install") -@Slf4j -@HybridDao -public class HybridDatabaseSchemaService implements DatabaseSchemaService { - - @Autowired - private SqlEntityDatabaseSchemaService sqlEntityDatabaseSchemaService; - - @Autowired - private CassandraTsDatabaseSchemaService cassandraTsDatabaseSchemaService; - - - @Override - public void createDatabaseSchema() throws Exception { - log.info("Installing Hybrid SQL/Cassandra DataBase schema..."); - sqlEntityDatabaseSchemaService.createDatabaseSchema(); - cassandraTsDatabaseSchemaService.createDatabaseSchema(); - } - -} diff --git a/application/src/main/java/org/thingsboard/server/service/install/SqlAbstractDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/SqlAbstractDatabaseSchemaService.java index f468a6e475..4a39b8f60b 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/SqlAbstractDatabaseSchemaService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/SqlAbstractDatabaseSchemaService.java @@ -27,7 +27,7 @@ import java.sql.Connection; import java.sql.DriverManager; @Slf4j -public abstract class SqlAbstractDatabaseSchemaService /*implements DatabaseSchemaService*/ { +public abstract class SqlAbstractDatabaseSchemaService implements DatabaseSchemaService { private static final String SQL_DIR = "sql"; @@ -49,7 +49,7 @@ public abstract class SqlAbstractDatabaseSchemaService /*implements DatabaseSche this.schemaSql = schemaSql; } - //@Override + @Override public void createDatabaseSchema() throws Exception { log.info("Installing SQL DataBase schema part: " + schemaSql); diff --git a/application/src/main/java/org/thingsboard/server/service/install/SqlDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/SqlDatabaseSchemaService.java deleted file mode 100644 index 8544b8bbf2..0000000000 --- a/application/src/main/java/org/thingsboard/server/service/install/SqlDatabaseSchemaService.java +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Copyright © 2016-2018 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.context.annotation.Profile; -import org.springframework.stereotype.Service; -import org.thingsboard.server.dao.util.SqlDao; - -@Service -@Profile("install") -@Slf4j -@SqlDao -public class SqlDatabaseSchemaService implements DatabaseSchemaService { - - @Autowired - private SqlEntityDatabaseSchemaService sqlEntityDatabaseSchemaService; - - @Autowired - private SqlTsDatabaseSchemaService sqlTsDatabaseSchemaService; - - - @Override - public void createDatabaseSchema() throws Exception { - log.info("Installing SQL DataBase schema..."); - sqlEntityDatabaseSchemaService.createDatabaseSchema(); - sqlTsDatabaseSchemaService.createDatabaseSchema(); - } - -} diff --git a/application/src/main/java/org/thingsboard/server/service/install/SqlEntityDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/SqlEntityDatabaseSchemaService.java index 0826099133..16453c6ebb 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/SqlEntityDatabaseSchemaService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/SqlEntityDatabaseSchemaService.java @@ -16,9 +16,12 @@ package org.thingsboard.server.service.install; import org.springframework.stereotype.Service; +import org.thingsboard.server.dao.util.SqlDao; @Service -public class SqlEntityDatabaseSchemaService extends SqlAbstractDatabaseSchemaService { +@SqlDao +public class SqlEntityDatabaseSchemaService extends SqlAbstractDatabaseSchemaService + implements EntityDatabaseSchemaService { public SqlEntityDatabaseSchemaService() { super("schema-entities.sql"); } diff --git a/application/src/main/java/org/thingsboard/server/service/install/SqlTsDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/SqlTsDatabaseSchemaService.java index 6c8f8b5054..82daf904df 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/SqlTsDatabaseSchemaService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/SqlTsDatabaseSchemaService.java @@ -16,9 +16,12 @@ package org.thingsboard.server.service.install; import org.springframework.stereotype.Service; +import org.thingsboard.server.dao.util.SqlTsDao; @Service -public class SqlTsDatabaseSchemaService extends SqlAbstractDatabaseSchemaService { +@SqlTsDao +public class SqlTsDatabaseSchemaService extends SqlAbstractDatabaseSchemaService + implements TsDatabaseSchemaService { public SqlTsDatabaseSchemaService() { super("schema-ts.sql"); } diff --git a/application/src/main/java/org/thingsboard/server/service/install/TsDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/TsDatabaseSchemaService.java new file mode 100644 index 0000000000..d95166feb8 --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/service/install/TsDatabaseSchemaService.java @@ -0,0 +1,4 @@ +package org.thingsboard.server.service.install; + +public interface TsDatabaseSchemaService extends DatabaseSchemaService { +} From 5165160b3b618001c4eb3d7a9a1900f84e761c6f Mon Sep 17 00:00:00 2001 From: hagaic Date: Wed, 22 Aug 2018 06:55:29 +0300 Subject: [PATCH 5/8] revert upgrade C* in hybrid mode, since no upgrade of TS tables --- .../service/install/CassandraDatabaseUpgradeService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/service/install/CassandraDatabaseUpgradeService.java b/application/src/main/java/org/thingsboard/server/service/install/CassandraDatabaseUpgradeService.java index 2cbd167242..f4ff92cf23 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/CassandraDatabaseUpgradeService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/CassandraDatabaseUpgradeService.java @@ -23,7 +23,7 @@ import org.springframework.stereotype.Service; import org.thingsboard.server.dao.cassandra.CassandraCluster; import org.thingsboard.server.dao.cassandra.CassandraInstallCluster; import org.thingsboard.server.dao.dashboard.DashboardService; -import org.thingsboard.server.dao.util.NoSqlAnyDao; +import org.thingsboard.server.dao.util.NoSqlDao; import org.thingsboard.server.service.install.cql.CQLStatementsParser; import org.thingsboard.server.service.install.cql.CassandraDbHelper; @@ -45,7 +45,7 @@ import static org.thingsboard.server.service.install.DatabaseHelper.TENANT_ID; import static org.thingsboard.server.service.install.DatabaseHelper.TITLE; @Service -@NoSqlAnyDao +@NoSqlDao @Profile("install") @Slf4j public class CassandraDatabaseUpgradeService implements DatabaseUpgradeService { From 8f614846eb0f456efdd0f915c4388db0acb8a35c Mon Sep 17 00:00:00 2001 From: hagaic Date: Wed, 22 Aug 2018 09:16:59 +0300 Subject: [PATCH 6/8] add license headers --- .../install/EntityDatabaseSchemaService.java | 15 +++++++++++++++ .../service/install/TsDatabaseSchemaService.java | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/application/src/main/java/org/thingsboard/server/service/install/EntityDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/EntityDatabaseSchemaService.java index 1d0ddc34a4..c215ea0d8f 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/EntityDatabaseSchemaService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/EntityDatabaseSchemaService.java @@ -1,3 +1,18 @@ +/** + * Copyright © 2016-2018 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; public interface EntityDatabaseSchemaService extends DatabaseSchemaService { diff --git a/application/src/main/java/org/thingsboard/server/service/install/TsDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/TsDatabaseSchemaService.java index d95166feb8..0a2ba7586c 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/TsDatabaseSchemaService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/TsDatabaseSchemaService.java @@ -1,3 +1,18 @@ +/** + * Copyright © 2016-2018 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; public interface TsDatabaseSchemaService extends DatabaseSchemaService { From 2adb39ba6c11733ffd763e59e04ed28e6fd434f3 Mon Sep 17 00:00:00 2001 From: Volodymyr Babak Date: Thu, 20 Sep 2018 16:03:44 +0300 Subject: [PATCH 7/8] code review fixes --- .../src/main/resources/thingsboard.yml | 6 +- .../controller/ControllerNoSqlTestSuite.java | 3 +- .../controller/ControllerSqlTestSuite.java | 2 +- .../server/mqtt/MqttNoSqlTestSuite.java | 3 +- .../server/mqtt/MqttSqlTestSuite.java | 4 +- .../rules/RuleEngineNoSqlTestSuite.java | 3 +- .../server/rules/RuleEngineSqlTestSuite.java | 2 +- .../server/system/SystemNoSqlTestSuite.java | 3 +- .../server/system/SystemSqlTestSuite.java | 2 +- .../server/dao/util/HybridDao.java | 22 - .../server/dao/util/NoSqlAnyDao.java | 2 +- .../thingsboard/server/dao/util/NoSqlDao.java | 2 +- .../thingsboard/server/dao/util/SqlDao.java | 2 +- dao/src/main/resources/cassandra/schema.cql | 640 ------------------ dao/src/main/resources/sql/schema.sql | 253 ------- .../server/dao/JpaDaoTestSuite.java | 2 +- .../server/dao/NoSqlDaoServiceTestSuite.java | 4 +- .../server/dao/SqlDaoServiceTestSuite.java | 2 +- dao/src/test/resources/nosql-test.properties | 3 +- dao/src/test/resources/sql-test.properties | 3 +- docker/k8s/cassandra-setup.yaml | 4 +- docker/k8s/cassandra-upgrade.yaml | 4 +- docker/k8s/tb.yaml | 7 +- docker/tb.env | 3 +- docker/tb/run-application.sh | 4 +- 25 files changed, 42 insertions(+), 943 deletions(-) delete mode 100644 dao/src/main/java/org/thingsboard/server/dao/util/HybridDao.java delete mode 100644 dao/src/main/resources/cassandra/schema.cql delete mode 100644 dao/src/main/resources/sql/schema.sql diff --git a/application/src/main/resources/thingsboard.yml b/application/src/main/resources/thingsboard.yml index a379e472aa..0cc075c1fc 100644 --- a/application/src/main/resources/thingsboard.yml +++ b/application/src/main/resources/thingsboard.yml @@ -151,7 +151,6 @@ quota: # Enable Host API Limits enabled: "${QUOTA_TENANT_ENABLED:false}" # Array of whitelist tenants - # Array of whitelist tenants whitelist: "${QUOTA_TENANT_WHITELIST:}" # Array of blacklist tenants blacklist: "${QUOTA_HOST_BLACKLIST:}" @@ -160,11 +159,10 @@ quota: intervalMin: 2 database: - type: "${DATABASE_TYPE:sql}" # cassandra OR sql entities: - type: "${DATABASE_TYPE:sql}" # cassandra OR sql + type: "${DATABASE_TS_TYPE:sql}" # cassandra OR sql ts: - type: "${DATABASE_TYPE:sql}" # cassandra OR sql (for hybrid mode, only this value should be cassandra) + type: "${DATABASE_CASSANDRA_TYPE:sql}" # cassandra OR sql (for hybrid mode, only this value should be cassandra) # Cassandra driver configuration parameters diff --git a/application/src/test/java/org/thingsboard/server/controller/ControllerNoSqlTestSuite.java b/application/src/test/java/org/thingsboard/server/controller/ControllerNoSqlTestSuite.java index 2e8848305f..f378437312 100644 --- a/application/src/test/java/org/thingsboard/server/controller/ControllerNoSqlTestSuite.java +++ b/application/src/test/java/org/thingsboard/server/controller/ControllerNoSqlTestSuite.java @@ -32,7 +32,8 @@ public class ControllerNoSqlTestSuite { public static CustomCassandraCQLUnit cassandraUnit = new CustomCassandraCQLUnit( Arrays.asList( - new ClassPathCQLDataSet("cassandra/schema.cql", false, false), + new ClassPathCQLDataSet("cassandra/schema-ts.cql", false, false), + new ClassPathCQLDataSet("cassandra/schema-entities.cql", false, false), new ClassPathCQLDataSet("cassandra/system-data.cql", false, false), new ClassPathCQLDataSet("cassandra/system-test.cql", false, false)), "cassandra-test.yaml", 30000l); diff --git a/application/src/test/java/org/thingsboard/server/controller/ControllerSqlTestSuite.java b/application/src/test/java/org/thingsboard/server/controller/ControllerSqlTestSuite.java index f316051212..3b3d9b04a6 100644 --- a/application/src/test/java/org/thingsboard/server/controller/ControllerSqlTestSuite.java +++ b/application/src/test/java/org/thingsboard/server/controller/ControllerSqlTestSuite.java @@ -30,7 +30,7 @@ public class ControllerSqlTestSuite { @ClassRule public static CustomSqlUnit sqlUnit = new CustomSqlUnit( - Arrays.asList("sql/schema.sql", "sql/system-data.sql"), + Arrays.asList("sql/schema-ts.sql", "sql/schema-entities.sql", "sql/system-data.sql"), "sql/drop-all-tables.sql", "sql-test.properties"); } diff --git a/application/src/test/java/org/thingsboard/server/mqtt/MqttNoSqlTestSuite.java b/application/src/test/java/org/thingsboard/server/mqtt/MqttNoSqlTestSuite.java index c4a969b1a9..2bb8a81ef2 100644 --- a/application/src/test/java/org/thingsboard/server/mqtt/MqttNoSqlTestSuite.java +++ b/application/src/test/java/org/thingsboard/server/mqtt/MqttNoSqlTestSuite.java @@ -32,7 +32,8 @@ public class MqttNoSqlTestSuite { public static CustomCassandraCQLUnit cassandraUnit = new CustomCassandraCQLUnit( Arrays.asList( - new ClassPathCQLDataSet("cassandra/schema.cql", false, false), + new ClassPathCQLDataSet("cassandra/schema-ts.cql", false, false), + new ClassPathCQLDataSet("cassandra/schema-entities.cql", false, false), new ClassPathCQLDataSet("cassandra/system-data.cql", false, false)), "cassandra-test.yaml", 30000l); } diff --git a/application/src/test/java/org/thingsboard/server/mqtt/MqttSqlTestSuite.java b/application/src/test/java/org/thingsboard/server/mqtt/MqttSqlTestSuite.java index 5ddbb67c97..1389c7edde 100644 --- a/application/src/test/java/org/thingsboard/server/mqtt/MqttSqlTestSuite.java +++ b/application/src/test/java/org/thingsboard/server/mqtt/MqttSqlTestSuite.java @@ -15,11 +15,9 @@ */ package org.thingsboard.server.mqtt; -import org.cassandraunit.dataset.cql.ClassPathCQLDataSet; import org.junit.ClassRule; import org.junit.extensions.cpsuite.ClasspathSuite; import org.junit.runner.RunWith; -import org.thingsboard.server.dao.CustomCassandraCQLUnit; import org.thingsboard.server.dao.CustomSqlUnit; import java.util.Arrays; @@ -31,7 +29,7 @@ public class MqttSqlTestSuite { @ClassRule public static CustomSqlUnit sqlUnit = new CustomSqlUnit( - Arrays.asList("sql/schema.sql", "sql/system-data.sql"), + Arrays.asList("sql/schema-ts.sql", "sql/schema-entities.sql", "sql/system-data.sql"), "sql/drop-all-tables.sql", "sql-test.properties"); } diff --git a/application/src/test/java/org/thingsboard/server/rules/RuleEngineNoSqlTestSuite.java b/application/src/test/java/org/thingsboard/server/rules/RuleEngineNoSqlTestSuite.java index bffe4913ef..c1d1e6cbaf 100644 --- a/application/src/test/java/org/thingsboard/server/rules/RuleEngineNoSqlTestSuite.java +++ b/application/src/test/java/org/thingsboard/server/rules/RuleEngineNoSqlTestSuite.java @@ -35,7 +35,8 @@ public class RuleEngineNoSqlTestSuite { public static CustomCassandraCQLUnit cassandraUnit = new CustomCassandraCQLUnit( Arrays.asList( - new ClassPathCQLDataSet("cassandra/schema.cql", false, false), + new ClassPathCQLDataSet("cassandra/schema-ts.cql", false, false), + new ClassPathCQLDataSet("cassandra/schema-entities.cql", false, false), new ClassPathCQLDataSet("cassandra/system-data.cql", false, false)), "cassandra-test.yaml", 30000l); diff --git a/application/src/test/java/org/thingsboard/server/rules/RuleEngineSqlTestSuite.java b/application/src/test/java/org/thingsboard/server/rules/RuleEngineSqlTestSuite.java index 7b13e2fd54..e09d820777 100644 --- a/application/src/test/java/org/thingsboard/server/rules/RuleEngineSqlTestSuite.java +++ b/application/src/test/java/org/thingsboard/server/rules/RuleEngineSqlTestSuite.java @@ -30,7 +30,7 @@ public class RuleEngineSqlTestSuite { @ClassRule public static CustomSqlUnit sqlUnit = new CustomSqlUnit( - Arrays.asList("sql/schema.sql", "sql/system-data.sql"), + Arrays.asList("sql/schema-ts.sql", "sql/schema-entities.sql", "sql/system-data.sql"), "sql/drop-all-tables.sql", "sql-test.properties"); } diff --git a/application/src/test/java/org/thingsboard/server/system/SystemNoSqlTestSuite.java b/application/src/test/java/org/thingsboard/server/system/SystemNoSqlTestSuite.java index 70e3fe2147..ebde304fd6 100644 --- a/application/src/test/java/org/thingsboard/server/system/SystemNoSqlTestSuite.java +++ b/application/src/test/java/org/thingsboard/server/system/SystemNoSqlTestSuite.java @@ -34,7 +34,8 @@ public class SystemNoSqlTestSuite { public static CustomCassandraCQLUnit cassandraUnit = new CustomCassandraCQLUnit( Arrays.asList( - new ClassPathCQLDataSet("cassandra/schema.cql", false, false), + new ClassPathCQLDataSet("cassandra/schema-ts.cql", false, false), + new ClassPathCQLDataSet("cassandra/schema-entities.cql", false, false), new ClassPathCQLDataSet("cassandra/system-data.cql", false, false)), "cassandra-test.yaml", 30000l); } diff --git a/application/src/test/java/org/thingsboard/server/system/SystemSqlTestSuite.java b/application/src/test/java/org/thingsboard/server/system/SystemSqlTestSuite.java index 97c67491f1..8ca6dcc877 100644 --- a/application/src/test/java/org/thingsboard/server/system/SystemSqlTestSuite.java +++ b/application/src/test/java/org/thingsboard/server/system/SystemSqlTestSuite.java @@ -31,7 +31,7 @@ public class SystemSqlTestSuite { @ClassRule public static CustomSqlUnit sqlUnit = new CustomSqlUnit( - Arrays.asList("sql/schema.sql", "sql/system-data.sql"), + Arrays.asList("sql/schema-ts.sql", "sql/schema-entities.sql", "sql/system-data.sql"), "sql/drop-all-tables.sql", "sql-test.properties"); diff --git a/dao/src/main/java/org/thingsboard/server/dao/util/HybridDao.java b/dao/src/main/java/org/thingsboard/server/dao/util/HybridDao.java deleted file mode 100644 index 2caf8ccace..0000000000 --- a/dao/src/main/java/org/thingsboard/server/dao/util/HybridDao.java +++ /dev/null @@ -1,22 +0,0 @@ -/** - * Copyright © 2016-2018 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.dao.util; - -import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; - -@ConditionalOnExpression("'${database.entity.type}'=='sql' && '${database.ts.type}'=='cassandra'") -public @interface HybridDao { -} diff --git a/dao/src/main/java/org/thingsboard/server/dao/util/NoSqlAnyDao.java b/dao/src/main/java/org/thingsboard/server/dao/util/NoSqlAnyDao.java index 7e7aefa3c3..a8049eec5f 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/util/NoSqlAnyDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/util/NoSqlAnyDao.java @@ -17,6 +17,6 @@ package org.thingsboard.server.dao.util; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; -@ConditionalOnExpression("'${database.type}'=='cassandra' || '${database.ts.type}'=='cassandra'") +@ConditionalOnExpression("'${database.ts.type}'=='cassandra' || '${database.entities.type}'=='cassandra'") public @interface NoSqlAnyDao { } diff --git a/dao/src/main/java/org/thingsboard/server/dao/util/NoSqlDao.java b/dao/src/main/java/org/thingsboard/server/dao/util/NoSqlDao.java index 96dbdab758..c3a719d893 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/util/NoSqlDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/util/NoSqlDao.java @@ -17,6 +17,6 @@ package org.thingsboard.server.dao.util; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -@ConditionalOnProperty(prefix = "database", value = "type", havingValue = "cassandra") +@ConditionalOnProperty(prefix = "database.entities", value = "type", havingValue = "cassandra") public @interface NoSqlDao { } diff --git a/dao/src/main/java/org/thingsboard/server/dao/util/SqlDao.java b/dao/src/main/java/org/thingsboard/server/dao/util/SqlDao.java index 3986f022d9..ab39c847b6 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/util/SqlDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/util/SqlDao.java @@ -17,6 +17,6 @@ package org.thingsboard.server.dao.util; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -@ConditionalOnProperty(prefix = "database", value = "type", havingValue = "sql") +@ConditionalOnProperty(prefix = "database.entities", value = "type", havingValue = "sql") public @interface SqlDao { } diff --git a/dao/src/main/resources/cassandra/schema.cql b/dao/src/main/resources/cassandra/schema.cql deleted file mode 100644 index f03122ab6b..0000000000 --- a/dao/src/main/resources/cassandra/schema.cql +++ /dev/null @@ -1,640 +0,0 @@ --- --- Copyright © 2016-2018 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 KEYSPACE IF NOT EXISTS thingsboard -WITH replication = { - 'class' : 'SimpleStrategy', - 'replication_factor' : 1 -}; - -CREATE TABLE IF NOT EXISTS thingsboard.user ( - id timeuuid, - tenant_id timeuuid, - customer_id timeuuid, - email text, - search_text text, - authority text, - first_name text, - last_name text, - additional_info text, - PRIMARY KEY (id, tenant_id, customer_id, authority) -); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.user_by_email AS - SELECT * - from thingsboard.user - WHERE email IS NOT NULL AND tenant_id IS NOT NULL AND customer_id IS NOT NULL AND id IS NOT NULL AND authority IS NOT - NULL - PRIMARY KEY ( email, tenant_id, customer_id, id, authority ); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.user_by_tenant_and_search_text AS - SELECT * - from thingsboard.user - WHERE tenant_id IS NOT NULL AND customer_id IS NOT NULL AND authority IS NOT NULL AND search_text IS NOT NULL AND id - IS NOT NULL - PRIMARY KEY ( tenant_id, customer_id, authority, search_text, id ) - WITH CLUSTERING ORDER BY ( customer_id DESC, authority DESC, search_text ASC, id DESC ); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.user_by_customer_and_search_text AS - SELECT * - from thingsboard.user - WHERE tenant_id IS NOT NULL AND customer_id IS NOT NULL AND authority IS NOT NULL AND search_text IS NOT NULL AND id - IS NOT NULL - PRIMARY KEY ( customer_id, tenant_id, authority, search_text, id ) - WITH CLUSTERING ORDER BY ( tenant_id DESC, authority DESC, search_text ASC, id DESC ); - -CREATE TABLE IF NOT EXISTS thingsboard.user_credentials ( - id timeuuid PRIMARY KEY, - user_id timeuuid, - enabled boolean, - password text, - activate_token text, - reset_token text -); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.user_credentials_by_user AS - SELECT * - from thingsboard.user_credentials - WHERE user_id IS NOT NULL AND id IS NOT NULL - PRIMARY KEY ( user_id, id ); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.user_credentials_by_activate_token AS - SELECT * - from thingsboard.user_credentials - WHERE activate_token IS NOT NULL AND id IS NOT NULL - PRIMARY KEY ( activate_token, id ); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.user_credentials_by_reset_token AS - SELECT * - from thingsboard.user_credentials - WHERE reset_token IS NOT NULL AND id IS NOT NULL - PRIMARY KEY ( reset_token, id ); - -CREATE TABLE IF NOT EXISTS thingsboard.admin_settings ( - id timeuuid PRIMARY KEY, - key text, - json_value text -); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.admin_settings_by_key AS - SELECT * - from thingsboard.admin_settings - WHERE key IS NOT NULL AND id IS NOT NULL - PRIMARY KEY ( key, id ) - WITH CLUSTERING ORDER BY ( id DESC ); - -CREATE TABLE IF NOT EXISTS thingsboard.tenant ( - id timeuuid, - title text, - search_text text, - region text, - country text, - state text, - city text, - address text, - address2 text, - zip text, - phone text, - email text, - additional_info text, - PRIMARY KEY (id, region) -); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.tenant_by_region_and_search_text AS - SELECT * - from thingsboard.tenant - WHERE region IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL - PRIMARY KEY ( region, search_text, id ) - WITH CLUSTERING ORDER BY ( search_text ASC, id DESC ); - -CREATE TABLE IF NOT EXISTS thingsboard.customer ( - id timeuuid, - tenant_id timeuuid, - title text, - search_text text, - country text, - state text, - city text, - address text, - address2 text, - zip text, - phone text, - email text, - additional_info text, - PRIMARY KEY (id, tenant_id) -); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.customer_by_tenant_and_title AS - SELECT * - from thingsboard.customer - WHERE tenant_id IS NOT NULL AND title IS NOT NULL AND id IS NOT NULL - PRIMARY KEY ( tenant_id, title, id ) - WITH CLUSTERING ORDER BY ( title ASC, id DESC ); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.customer_by_tenant_and_search_text AS - SELECT * - from thingsboard.customer - WHERE tenant_id IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL - PRIMARY KEY ( tenant_id, search_text, id ) - WITH CLUSTERING ORDER BY ( search_text ASC, id DESC ); - -CREATE TABLE IF NOT EXISTS thingsboard.device ( - id timeuuid, - tenant_id timeuuid, - customer_id timeuuid, - name text, - type text, - search_text text, - additional_info text, - PRIMARY KEY (id, tenant_id, customer_id, type) -); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.device_by_tenant_and_name AS - SELECT * - from thingsboard.device - WHERE tenant_id IS NOT NULL AND customer_id IS NOT NULL AND type IS NOT NULL AND name IS NOT NULL AND id IS NOT NULL - PRIMARY KEY ( tenant_id, name, id, customer_id, type) - WITH CLUSTERING ORDER BY ( name ASC, id DESC, customer_id DESC); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.device_by_tenant_and_search_text AS - SELECT * - from thingsboard.device - WHERE tenant_id IS NOT NULL AND customer_id IS NOT NULL AND type IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL - PRIMARY KEY ( tenant_id, search_text, id, customer_id, type) - WITH CLUSTERING ORDER BY ( search_text ASC, id DESC, customer_id DESC); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.device_by_tenant_by_type_and_search_text AS - SELECT * - from thingsboard.device - WHERE tenant_id IS NOT NULL AND customer_id IS NOT NULL AND type IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL - PRIMARY KEY ( tenant_id, type, search_text, id, customer_id) - WITH CLUSTERING ORDER BY ( type ASC, search_text ASC, id DESC, customer_id DESC); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.device_by_customer_and_search_text AS - SELECT * - from thingsboard.device - WHERE tenant_id IS NOT NULL AND customer_id IS NOT NULL AND type IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL - PRIMARY KEY ( customer_id, tenant_id, search_text, id, type ) - WITH CLUSTERING ORDER BY ( tenant_id DESC, search_text ASC, id DESC ); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.device_by_customer_by_type_and_search_text AS - SELECT * - from thingsboard.device - WHERE tenant_id IS NOT NULL AND customer_id IS NOT NULL AND type IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL - PRIMARY KEY ( customer_id, tenant_id, type, search_text, id ) - WITH CLUSTERING ORDER BY ( tenant_id DESC, type ASC, search_text ASC, id DESC ); - -CREATE TABLE IF NOT EXISTS thingsboard.device_credentials ( - id timeuuid PRIMARY KEY, - device_id timeuuid, - credentials_type text, - credentials_id text, - credentials_value text -); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.device_credentials_by_device AS - SELECT * - from thingsboard.device_credentials - WHERE device_id IS NOT NULL AND id IS NOT NULL - PRIMARY KEY ( device_id, id ); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.device_credentials_by_credentials_id AS - SELECT * - from thingsboard.device_credentials - WHERE credentials_id IS NOT NULL AND id IS NOT NULL - PRIMARY KEY ( credentials_id, id ); - -CREATE TABLE IF NOT EXISTS thingsboard.asset ( - id timeuuid, - tenant_id timeuuid, - customer_id timeuuid, - name text, - type text, - search_text text, - additional_info text, - PRIMARY KEY (id, tenant_id, customer_id, type) -); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.asset_by_tenant_and_name AS - SELECT * - from thingsboard.asset - WHERE tenant_id IS NOT NULL AND customer_id IS NOT NULL AND type IS NOT NULL AND name IS NOT NULL AND id IS NOT NULL - PRIMARY KEY ( tenant_id, name, id, customer_id, type) - WITH CLUSTERING ORDER BY ( name ASC, id DESC, customer_id DESC); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.asset_by_tenant_and_search_text AS - SELECT * - from thingsboard.asset - WHERE tenant_id IS NOT NULL AND customer_id IS NOT NULL AND type IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL - PRIMARY KEY ( tenant_id, search_text, id, customer_id, type) - WITH CLUSTERING ORDER BY ( search_text ASC, id DESC, customer_id DESC); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.asset_by_tenant_by_type_and_search_text AS - SELECT * - from thingsboard.asset - WHERE tenant_id IS NOT NULL AND customer_id IS NOT NULL AND type IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL - PRIMARY KEY ( tenant_id, type, search_text, id, customer_id) - WITH CLUSTERING ORDER BY ( type ASC, search_text ASC, id DESC, customer_id DESC); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.asset_by_customer_and_search_text AS - SELECT * - from thingsboard.asset - WHERE tenant_id IS NOT NULL AND customer_id IS NOT NULL AND type IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL - PRIMARY KEY ( customer_id, tenant_id, search_text, id, type ) - WITH CLUSTERING ORDER BY ( tenant_id DESC, search_text ASC, id DESC ); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.asset_by_customer_by_type_and_search_text AS - SELECT * - from thingsboard.asset - WHERE tenant_id IS NOT NULL AND customer_id IS NOT NULL AND type IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL - PRIMARY KEY ( customer_id, tenant_id, type, search_text, id ) - WITH CLUSTERING ORDER BY ( tenant_id DESC, type ASC, search_text ASC, id DESC ); - -CREATE TABLE IF NOT EXISTS thingsboard.entity_subtype ( - tenant_id timeuuid, - entity_type text, // (DEVICE, ASSET) - type text, - PRIMARY KEY (tenant_id, entity_type, type) -); - -CREATE TABLE IF NOT EXISTS thingsboard.alarm ( - id timeuuid, - tenant_id timeuuid, - type text, - originator_id timeuuid, - originator_type text, - severity text, - status text, - start_ts bigint, - end_ts bigint, - ack_ts bigint, - clear_ts bigint, - details text, - propagate boolean, - PRIMARY KEY ((tenant_id, originator_id, originator_type), type, id) -) WITH CLUSTERING ORDER BY ( type ASC, id DESC); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.alarm_by_id AS - SELECT * - from thingsboard.alarm - WHERE tenant_id IS NOT NULL AND originator_id IS NOT NULL AND originator_type IS NOT NULL AND type IS NOT NULL - AND type IS NOT NULL AND id IS NOT NULL - PRIMARY KEY (id, tenant_id, originator_id, originator_type, type) - WITH CLUSTERING ORDER BY ( tenant_id ASC, originator_id ASC, originator_type ASC, type ASC); - -CREATE TABLE IF NOT EXISTS thingsboard.relation ( - from_id timeuuid, - from_type text, - to_id timeuuid, - to_type text, - relation_type_group text, - relation_type text, - additional_info text, - PRIMARY KEY ((from_id, from_type), relation_type_group, relation_type, to_id, to_type) -) WITH CLUSTERING ORDER BY ( relation_type_group ASC, relation_type ASC, to_id ASC, to_type ASC); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.relation_by_type_and_child_type AS - SELECT * - from thingsboard.relation - WHERE from_id IS NOT NULL AND from_type IS NOT NULL AND relation_type_group IS NOT NULL AND relation_type IS NOT NULL AND to_id IS NOT NULL AND to_type IS NOT NULL - PRIMARY KEY ((from_id, from_type), relation_type_group, relation_type, to_type, to_id) - WITH CLUSTERING ORDER BY ( relation_type_group ASC, relation_type ASC, to_type ASC, to_id DESC); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.reverse_relation AS - SELECT * - from thingsboard.relation - WHERE from_id IS NOT NULL AND from_type IS NOT NULL AND relation_type_group IS NOT NULL AND relation_type IS NOT NULL AND to_id IS NOT NULL AND to_type IS NOT NULL - PRIMARY KEY ((to_id, to_type), relation_type_group, relation_type, from_id, from_type) - WITH CLUSTERING ORDER BY ( relation_type_group ASC, relation_type ASC, from_id ASC, from_type ASC); - -CREATE TABLE IF NOT EXISTS thingsboard.widgets_bundle ( - id timeuuid, - tenant_id timeuuid, - alias text, - title text, - search_text text, - image blob, - PRIMARY KEY (id, tenant_id) -); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.widgets_bundle_by_tenant_and_search_text AS - SELECT * - from thingsboard.widgets_bundle - WHERE tenant_id IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL - PRIMARY KEY ( tenant_id, search_text, id ) - WITH CLUSTERING ORDER BY ( search_text ASC, id DESC ); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.widgets_bundle_by_tenant_and_alias AS - SELECT * - from thingsboard.widgets_bundle - WHERE tenant_id IS NOT NULL AND alias IS NOT NULL AND id IS NOT NULL - PRIMARY KEY ( tenant_id, alias, id ) - WITH CLUSTERING ORDER BY ( alias ASC, id DESC ); - -CREATE TABLE IF NOT EXISTS thingsboard.widget_type ( - id timeuuid, - tenant_id timeuuid, - bundle_alias text, - alias text, - name text, - descriptor text, - PRIMARY KEY (id, tenant_id, bundle_alias) -); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.widget_type_by_tenant_and_aliases AS - SELECT * - from thingsboard.widget_type - WHERE tenant_id IS NOT NULL AND bundle_alias IS NOT NULL AND alias IS NOT NULL AND id IS NOT NULL - PRIMARY KEY ( tenant_id, bundle_alias, alias, id ) - WITH CLUSTERING ORDER BY ( bundle_alias ASC, alias ASC, id DESC ); - -CREATE TABLE IF NOT EXISTS thingsboard.dashboard ( - id timeuuid, - tenant_id timeuuid, - title text, - search_text text, - assigned_customers text, - configuration text, - PRIMARY KEY (id, tenant_id) -); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.dashboard_by_tenant_and_search_text AS - SELECT * - from thingsboard.dashboard - WHERE tenant_id IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL - PRIMARY KEY ( tenant_id, search_text, id ) - WITH CLUSTERING ORDER BY ( search_text ASC, id DESC ); - -CREATE TABLE IF NOT EXISTS thingsboard.ts_kv_cf ( - entity_type text, // (DEVICE, CUSTOMER, TENANT) - entity_id timeuuid, - key text, - partition bigint, - ts bigint, - bool_v boolean, - str_v text, - long_v bigint, - dbl_v double, - PRIMARY KEY (( entity_type, entity_id, key, partition ), ts) -); - -CREATE TABLE IF NOT EXISTS thingsboard.ts_kv_partitions_cf ( - entity_type text, // (DEVICE, CUSTOMER, TENANT) - entity_id timeuuid, - key text, - partition bigint, - PRIMARY KEY (( entity_type, entity_id, key ), partition) -) WITH CLUSTERING ORDER BY ( partition ASC ) - AND compaction = { 'class' : 'LeveledCompactionStrategy' }; - -CREATE TABLE IF NOT EXISTS thingsboard.ts_kv_latest_cf ( - entity_type text, // (DEVICE, CUSTOMER, TENANT) - entity_id timeuuid, - key text, - ts bigint, - bool_v boolean, - str_v text, - long_v bigint, - dbl_v double, - PRIMARY KEY (( entity_type, entity_id ), key) -) WITH compaction = { 'class' : 'LeveledCompactionStrategy' }; - - -CREATE TABLE IF NOT EXISTS thingsboard.attributes_kv_cf ( - entity_type text, // (DEVICE, CUSTOMER, TENANT) - entity_id timeuuid, - attribute_type text, // (CLIENT_SIDE, SHARED, SERVER_SIDE) - attribute_key text, - bool_v boolean, - str_v text, - long_v bigint, - dbl_v double, - last_update_ts bigint, - PRIMARY KEY ((entity_type, entity_id, attribute_type), attribute_key) -) WITH compaction = { 'class' : 'LeveledCompactionStrategy' }; - -CREATE TABLE IF NOT EXISTS thingsboard.component_descriptor ( - id timeuuid, - type text, - scope text, - name text, - search_text text, - clazz text, - configuration_descriptor text, - actions text, - PRIMARY KEY (clazz, id, type, scope) -); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.component_desc_by_type_search_text AS - SELECT * - from thingsboard.component_descriptor - WHERE type IS NOT NULL AND scope IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL AND clazz IS NOT NULL - PRIMARY KEY ( type, search_text, id, clazz, scope) - WITH CLUSTERING ORDER BY ( search_text DESC); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.component_desc_by_scope_type_search_text AS - SELECT * - from thingsboard.component_descriptor - WHERE type IS NOT NULL AND scope IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL AND clazz IS NOT NULL - PRIMARY KEY ( (scope, type), search_text, id, clazz) - WITH CLUSTERING ORDER BY ( search_text DESC); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.component_desc_by_id AS - SELECT * - from thingsboard.component_descriptor - WHERE type IS NOT NULL AND scope IS NOT NULL AND id IS NOT NULL AND clazz IS NOT NULL - PRIMARY KEY ( id, clazz, scope, type ) - WITH CLUSTERING ORDER BY ( clazz ASC, scope ASC, type DESC); - -CREATE TABLE IF NOT EXISTS thingsboard.event ( - tenant_id timeuuid, // tenant or system - id timeuuid, - event_type text, - event_uid text, - entity_type text, - entity_id timeuuid, - body text, - PRIMARY KEY ((tenant_id, entity_type, entity_id), event_type, event_uid) -); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.event_by_type_and_id AS - SELECT * - FROM thingsboard.event - WHERE tenant_id IS NOT NULL AND entity_type IS NOT NULL AND entity_id IS NOT NULL AND id IS NOT NULL - AND event_type IS NOT NULL AND event_uid IS NOT NULL - PRIMARY KEY ((tenant_id, entity_type, entity_id), event_type, id, event_uid) - WITH CLUSTERING ORDER BY (event_type ASC, id ASC, event_uid ASC); - - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.event_by_id AS - SELECT * - FROM thingsboard.event - WHERE tenant_id IS NOT NULL AND entity_type IS NOT NULL AND entity_id IS NOT NULL AND id IS NOT NULL - AND event_type IS NOT NULL AND event_uid IS NOT NULL - PRIMARY KEY ((tenant_id, entity_type, entity_id), id, event_type, event_uid) - WITH CLUSTERING ORDER BY (id ASC, event_type ASC, event_uid ASC); - -CREATE TABLE IF NOT EXISTS thingsboard.audit_log_by_entity_id ( - tenant_id timeuuid, - id timeuuid, - customer_id timeuuid, - entity_id timeuuid, - entity_type text, - entity_name text, - user_id timeuuid, - user_name text, - action_type text, - action_data text, - action_status text, - action_failure_details text, - PRIMARY KEY ((tenant_id, entity_id, entity_type), id) -); - -CREATE TABLE IF NOT EXISTS thingsboard.audit_log_by_customer_id ( - tenant_id timeuuid, - id timeuuid, - customer_id timeuuid, - entity_id timeuuid, - entity_type text, - entity_name text, - user_id timeuuid, - user_name text, - action_type text, - action_data text, - action_status text, - action_failure_details text, - PRIMARY KEY ((tenant_id, customer_id), id) -); - -CREATE TABLE IF NOT EXISTS thingsboard.audit_log_by_user_id ( - tenant_id timeuuid, - id timeuuid, - customer_id timeuuid, - entity_id timeuuid, - entity_type text, - entity_name text, - user_id timeuuid, - user_name text, - action_type text, - action_data text, - action_status text, - action_failure_details text, - PRIMARY KEY ((tenant_id, user_id), id) -); - -CREATE TABLE IF NOT EXISTS thingsboard.audit_log_by_tenant_id ( - tenant_id timeuuid, - id timeuuid, - partition bigint, - customer_id timeuuid, - entity_id timeuuid, - entity_type text, - entity_name text, - user_id timeuuid, - user_name text, - action_type text, - action_data text, - action_status text, - action_failure_details text, - PRIMARY KEY ((tenant_id, partition), id) -); - -CREATE TABLE IF NOT EXISTS thingsboard.audit_log_by_tenant_id_partitions ( - tenant_id timeuuid, - partition bigint, - PRIMARY KEY (( tenant_id ), partition) -) WITH CLUSTERING ORDER BY ( partition ASC ) -AND compaction = { 'class' : 'LeveledCompactionStrategy' }; - -CREATE TABLE IF NOT EXISTS thingsboard.msg_queue ( - node_id timeuuid, - cluster_partition bigint, - ts_partition bigint, - ts bigint, - msg blob, - PRIMARY KEY ((node_id, cluster_partition, ts_partition), ts)) -WITH CLUSTERING ORDER BY (ts DESC) -AND compaction = { - 'class': 'org.apache.cassandra.db.compaction.DateTieredCompactionStrategy', - 'min_threshold': '5', - 'base_time_seconds': '43200', - 'max_window_size_seconds': '43200', - 'tombstone_threshold': '0.9', - 'unchecked_tombstone_compaction': 'true' -}; - -CREATE TABLE IF NOT EXISTS thingsboard.msg_ack_queue ( - node_id timeuuid, - cluster_partition bigint, - ts_partition bigint, - msg_id timeuuid, - PRIMARY KEY ((node_id, cluster_partition, ts_partition), msg_id)) -WITH CLUSTERING ORDER BY (msg_id DESC) -AND compaction = { - 'class': 'org.apache.cassandra.db.compaction.DateTieredCompactionStrategy', - 'min_threshold': '5', - 'base_time_seconds': '43200', - 'max_window_size_seconds': '43200', - 'tombstone_threshold': '0.9', - 'unchecked_tombstone_compaction': 'true' -}; - -CREATE TABLE IF NOT EXISTS thingsboard.processed_msg_partitions ( - node_id timeuuid, - cluster_partition bigint, - ts_partition bigint, - PRIMARY KEY ((node_id, cluster_partition), ts_partition)) -WITH CLUSTERING ORDER BY (ts_partition DESC) -AND compaction = { - 'class': 'org.apache.cassandra.db.compaction.DateTieredCompactionStrategy', - 'min_threshold': '5', - 'base_time_seconds': '43200', - 'max_window_size_seconds': '43200', - 'tombstone_threshold': '0.9', - 'unchecked_tombstone_compaction': 'true' -}; - -CREATE TABLE IF NOT EXISTS thingsboard.rule_chain ( - id uuid, - tenant_id uuid, - name text, - search_text text, - first_rule_node_id uuid, - root boolean, - debug_mode boolean, - configuration text, - additional_info text, - PRIMARY KEY (id, tenant_id) -); - -CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.rule_chain_by_tenant_and_search_text AS - SELECT * - from thingsboard.rule_chain - WHERE tenant_id IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL - PRIMARY KEY ( tenant_id, search_text, id ) - WITH CLUSTERING ORDER BY ( search_text ASC, id DESC ); - -CREATE TABLE IF NOT EXISTS thingsboard.rule_node ( - id uuid, - rule_chain_id uuid, - type text, - name text, - debug_mode boolean, - search_text text, - configuration text, - additional_info text, - PRIMARY KEY (id) -); diff --git a/dao/src/main/resources/sql/schema.sql b/dao/src/main/resources/sql/schema.sql deleted file mode 100644 index 91e77da503..0000000000 --- a/dao/src/main/resources/sql/schema.sql +++ /dev/null @@ -1,253 +0,0 @@ --- --- Copyright © 2016-2018 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), - 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), - search_text varchar(255), - tenant_id varchar(31), - type varchar(255) -); - -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, - last_update_ts bigint, - CONSTRAINT attribute_kv_unq_key UNIQUE (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, - 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), - search_text varchar(255), - tenant_id varchar(31) -); - -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) -); - -CREATE TABLE IF NOT EXISTS event ( - id varchar(31) NOT NULL CONSTRAINT event_pkey PRIMARY KEY, - body varchar, - 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_unq_key UNIQUE (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 ts_kv ( - entity_type varchar(255) NOT NULL, - entity_id varchar(31) NOT NULL, - key varchar(255) NOT NULL, - ts bigint NOT NULL, - bool_v boolean, - str_v varchar(10000000), - long_v bigint, - dbl_v double precision, - CONSTRAINT ts_kv_unq_key UNIQUE (entity_type, entity_id, key, ts) -); - -CREATE TABLE IF NOT EXISTS ts_kv_latest ( - entity_type varchar(255) NOT NULL, - entity_id varchar(31) NOT NULL, - key varchar(255) NOT NULL, - ts bigint NOT NULL, - bool_v boolean, - str_v varchar(10000000), - long_v bigint, - dbl_v double precision, - CONSTRAINT ts_kv_latest_unq_key UNIQUE (entity_type, entity_id, key) -); - -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) -); diff --git a/dao/src/test/java/org/thingsboard/server/dao/JpaDaoTestSuite.java b/dao/src/test/java/org/thingsboard/server/dao/JpaDaoTestSuite.java index 48ba2fae68..a9d2cbc4f9 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/JpaDaoTestSuite.java +++ b/dao/src/test/java/org/thingsboard/server/dao/JpaDaoTestSuite.java @@ -30,7 +30,7 @@ public class JpaDaoTestSuite { @ClassRule public static CustomSqlUnit sqlUnit = new CustomSqlUnit( - Arrays.asList("sql/schema.sql", "sql/system-data.sql"), + Arrays.asList("sql/schema-ts.sql", "sql/schema-entities.sql", "sql/system-data.sql"), "sql/drop-all-tables.sql", "sql-test.properties" ); diff --git a/dao/src/test/java/org/thingsboard/server/dao/NoSqlDaoServiceTestSuite.java b/dao/src/test/java/org/thingsboard/server/dao/NoSqlDaoServiceTestSuite.java index f10462d77f..55c2f70f1d 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/NoSqlDaoServiceTestSuite.java +++ b/dao/src/test/java/org/thingsboard/server/dao/NoSqlDaoServiceTestSuite.java @@ -34,7 +34,9 @@ public class NoSqlDaoServiceTestSuite { @ClassRule public static CustomCassandraCQLUnit cassandraUnit = new CustomCassandraCQLUnit( - Arrays.asList(new ClassPathCQLDataSet("cassandra/schema.cql", false, false), + Arrays.asList( + new ClassPathCQLDataSet("cassandra/schema-ts.cql", false, false), + new ClassPathCQLDataSet("cassandra/schema-entities.cql", false, false), new ClassPathCQLDataSet("cassandra/system-data.cql", false, false), new ClassPathCQLDataSet("cassandra/system-test.cql", false, false)), "cassandra-test.yaml", 30000L); diff --git a/dao/src/test/java/org/thingsboard/server/dao/SqlDaoServiceTestSuite.java b/dao/src/test/java/org/thingsboard/server/dao/SqlDaoServiceTestSuite.java index 3f65184bfb..9e56d64aba 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/SqlDaoServiceTestSuite.java +++ b/dao/src/test/java/org/thingsboard/server/dao/SqlDaoServiceTestSuite.java @@ -30,7 +30,7 @@ public class SqlDaoServiceTestSuite { @ClassRule public static CustomSqlUnit sqlUnit = new CustomSqlUnit( - Arrays.asList("sql/schema.sql", "sql/system-data.sql", "sql/system-test.sql"), + Arrays.asList("sql/schema-ts.sql", "sql/schema-entities.sql", "sql/system-data.sql", "sql/system-test.sql"), "sql/drop-all-tables.sql", "sql-test.properties" ); diff --git a/dao/src/test/resources/nosql-test.properties b/dao/src/test/resources/nosql-test.properties index e37e228b40..06a92faa01 100644 --- a/dao/src/test/resources/nosql-test.properties +++ b/dao/src/test/resources/nosql-test.properties @@ -1,4 +1,5 @@ -database.type=cassandra +database.entities.type=cassandra +database.ts.type=cassandra cassandra.queue.partitioning=HOURS cassandra.queue.ack.ttl=3600 diff --git a/dao/src/test/resources/sql-test.properties b/dao/src/test/resources/sql-test.properties index 1f34b98222..3357425fce 100644 --- a/dao/src/test/resources/sql-test.properties +++ b/dao/src/test/resources/sql-test.properties @@ -1,4 +1,5 @@ -database.type=sql +database.ts.type=sql +database.entities.type=sql sql.ts_inserts_executor_type=fixed sql.ts_inserts_fixed_thread_pool_size=10 diff --git a/docker/k8s/cassandra-setup.yaml b/docker/k8s/cassandra-setup.yaml index 381df77e65..03a2739446 100644 --- a/docker/k8s/cassandra-setup.yaml +++ b/docker/k8s/cassandra-setup.yaml @@ -30,7 +30,9 @@ spec: value: "cassandra-headless" - name : CASSANDRA_PORT value: "9042" - - name : DATABASE_TYPE + - name : DATABASE_ENTITIES_TYPE + value: "cassandra" + - name : DATABASE_TS_TYPE value: "cassandra" - name : CASSANDRA_URL value: "cassandra-headless:9042" diff --git a/docker/k8s/cassandra-upgrade.yaml b/docker/k8s/cassandra-upgrade.yaml index a78136ec41..9276776678 100644 --- a/docker/k8s/cassandra-upgrade.yaml +++ b/docker/k8s/cassandra-upgrade.yaml @@ -30,7 +30,9 @@ spec: value: "cassandra-headless" - name : CASSANDRA_PORT value: "9042" - - name : DATABASE_TYPE + - name : DATABASE_ENTITIES_TYPE + value: "cassandra" + - name : DATABASE_TS_TYPE value: "cassandra" - name : CASSANDRA_URL value: "cassandra-headless:9042" diff --git a/docker/k8s/tb.yaml b/docker/k8s/tb.yaml index f38e1f12b1..741bbe0f26 100644 --- a/docker/k8s/tb.yaml +++ b/docker/k8s/tb.yaml @@ -120,7 +120,12 @@ spec: configMapKeyRef: name: tb-config key: cassandra.url - - name: DATABASE_TYPE + - name: DATABASE_ENTITIES_TYPE + valueFrom: + configMapKeyRef: + name: tb-config + key: database.type + - name: DATABASE_TS_TYPE valueFrom: configMapKeyRef: name: tb-config diff --git a/docker/tb.env b/docker/tb.env index 76afc29962..bc92de5c8b 100644 --- a/docker/tb.env +++ b/docker/tb.env @@ -8,7 +8,8 @@ COAP_BIND_PORT=5683 ZOOKEEPER_URL=zk:2181 # type of database to use: sql[DEFAULT] or cassandra -DATABASE_TYPE=sql +DATABASE_TS_TYPE=sql +DATABASE_ENTITIES_TYPE=sql # cassandra db config CASSANDRA_URL=cassandra:9042 diff --git a/docker/tb/run-application.sh b/docker/tb/run-application.sh index a2a1e2beba..e6b59f9aee 100755 --- a/docker/tb/run-application.sh +++ b/docker/tb/run-application.sh @@ -23,7 +23,7 @@ printenv | awk -F "=" '{print "export " $1 "='\''" $2 "'\''"}' >> /usr/share/thi cat /usr/share/thingsboard/conf/thingsboard.conf -if [ "$DATABASE_TYPE" == "cassandra" ]; then +if [ "$DATABASE_ENTITIES_TYPE" == "cassandra" ]; then until nmap $CASSANDRA_HOST -p $CASSANDRA_PORT | grep "$CASSANDRA_PORT/tcp open\|filtered" do echo "Wait for cassandra db to start..." @@ -31,7 +31,7 @@ if [ "$DATABASE_TYPE" == "cassandra" ]; then done fi -if [ "$DATABASE_TYPE" == "sql" ]; then +if [ "$DATABASE_ENTITIES_TYPE" == "sql" ]; then if [ "$SPRING_DRIVER_CLASS_NAME" == "org.postgresql.Driver" ]; then until nmap $POSTGRES_HOST -p $POSTGRES_PORT | grep "$POSTGRES_PORT/tcp open" do From 014ed4f97939583246e3f0032408276bdf142525 Mon Sep 17 00:00:00 2001 From: Volodymyr Babak Date: Thu, 20 Sep 2018 17:08:03 +0300 Subject: [PATCH 8/8] Fixed tests --- .../service/install/CassandraEntityDatabaseSchemaService.java | 2 ++ .../service/install/CassandraTsDatabaseSchemaService.java | 2 ++ .../server/service/install/SqlEntityDatabaseSchemaService.java | 2 ++ .../server/service/install/SqlTsDatabaseSchemaService.java | 2 ++ 4 files changed, 8 insertions(+) diff --git a/application/src/main/java/org/thingsboard/server/service/install/CassandraEntityDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/CassandraEntityDatabaseSchemaService.java index 7937ef23be..b492a51cbc 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/CassandraEntityDatabaseSchemaService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/CassandraEntityDatabaseSchemaService.java @@ -15,11 +15,13 @@ */ package org.thingsboard.server.service.install; +import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Service; import org.thingsboard.server.dao.util.NoSqlDao; @Service @NoSqlDao +@Profile("install") public class CassandraEntityDatabaseSchemaService extends CassandraAbstractDatabaseSchemaService implements EntityDatabaseSchemaService { public CassandraEntityDatabaseSchemaService() { diff --git a/application/src/main/java/org/thingsboard/server/service/install/CassandraTsDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/CassandraTsDatabaseSchemaService.java index ba18b57ff1..1a3a2e429b 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/CassandraTsDatabaseSchemaService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/CassandraTsDatabaseSchemaService.java @@ -15,11 +15,13 @@ */ package org.thingsboard.server.service.install; +import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Service; import org.thingsboard.server.dao.util.NoSqlTsDao; @Service @NoSqlTsDao +@Profile("install") public class CassandraTsDatabaseSchemaService extends CassandraAbstractDatabaseSchemaService implements TsDatabaseSchemaService { public CassandraTsDatabaseSchemaService() { diff --git a/application/src/main/java/org/thingsboard/server/service/install/SqlEntityDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/SqlEntityDatabaseSchemaService.java index 16453c6ebb..bfd371b28a 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/SqlEntityDatabaseSchemaService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/SqlEntityDatabaseSchemaService.java @@ -15,11 +15,13 @@ */ package org.thingsboard.server.service.install; +import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Service; import org.thingsboard.server.dao.util.SqlDao; @Service @SqlDao +@Profile("install") public class SqlEntityDatabaseSchemaService extends SqlAbstractDatabaseSchemaService implements EntityDatabaseSchemaService { public SqlEntityDatabaseSchemaService() { diff --git a/application/src/main/java/org/thingsboard/server/service/install/SqlTsDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/SqlTsDatabaseSchemaService.java index 82daf904df..03a35178bf 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/SqlTsDatabaseSchemaService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/SqlTsDatabaseSchemaService.java @@ -15,11 +15,13 @@ */ package org.thingsboard.server.service.install; +import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Service; import org.thingsboard.server.dao.util.SqlTsDao; @Service @SqlTsDao +@Profile("install") public class SqlTsDatabaseSchemaService extends SqlAbstractDatabaseSchemaService implements TsDatabaseSchemaService { public SqlTsDatabaseSchemaService() {