Commit 4976a5326f5953a11a8edb80261aa21ffc9add10

Authored by hagaic
1 parent c9fc57b3

install hybrid db schema

... ... @@ -116,7 +116,7 @@ public class ThingsboardInstallService {
116 116
117 117 log.info("Installing DataBase schema...");
118 118
119   - databaseSchemaService.createDatabaseSchema();//TODO issue 1005 - create both SQL and C* schemas in hybrid mode
  119 + databaseSchemaService.createDatabaseSchema();
120 120
121 121 log.info("Loading system data...");
122 122
... ...
  1 +/**
  2 + * Copyright © 2016-2018 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.service.install;
  17 +
  18 +import lombok.extern.slf4j.Slf4j;
  19 +import org.springframework.beans.factory.annotation.Autowired;
  20 +import org.thingsboard.server.dao.cassandra.CassandraInstallCluster;
  21 +import org.thingsboard.server.service.install.cql.CQLStatementsParser;
  22 +
  23 +import java.nio.file.Path;
  24 +import java.nio.file.Paths;
  25 +import java.util.List;
  26 +
  27 +@Slf4j
  28 +public abstract class CassandraAbstractDatabaseSchemaService /*implements DatabaseSchemaService*/ {
  29 +
  30 + private static final String CASSANDRA_DIR = "cassandra";
  31 +
  32 + @Autowired
  33 + private CassandraInstallCluster cluster;
  34 +
  35 + @Autowired
  36 + private InstallScripts installScripts;
  37 +
  38 + private final String schemaCql;
  39 +
  40 + protected CassandraAbstractDatabaseSchemaService(String schemaCql) {
  41 + this.schemaCql = schemaCql;
  42 + }
  43 +
  44 + //@Override
  45 + public void createDatabaseSchema() throws Exception {
  46 + log.info("Installing Cassandra DataBase schema part: " + schemaCql);
  47 + Path schemaFile = Paths.get(installScripts.getDataDir(), CASSANDRA_DIR, schemaCql);
  48 + loadCql(schemaFile);
  49 +
  50 + }
  51 +
  52 + private void loadCql(Path cql) throws Exception {
  53 + List<String> statements = new CQLStatementsParser(cql).getStatements();
  54 + statements.forEach(statement -> cluster.getSession().execute(statement));
  55 + }
  56 +}
... ...
... ... @@ -19,39 +19,25 @@ import lombok.extern.slf4j.Slf4j;
19 19 import org.springframework.beans.factory.annotation.Autowired;
20 20 import org.springframework.context.annotation.Profile;
21 21 import org.springframework.stereotype.Service;
22   -import org.thingsboard.server.dao.cassandra.CassandraInstallCluster;
23   -import org.thingsboard.server.dao.util.NoSqlAnyDao;
24   -import org.thingsboard.server.service.install.cql.CQLStatementsParser;
25   -
26   -import java.nio.file.Path;
27   -import java.nio.file.Paths;
28   -import java.util.List;
  22 +import org.thingsboard.server.dao.util.NoSqlDao;
29 23
30 24 @Service
31   -@NoSqlAnyDao
  25 +@NoSqlDao
32 26 @Profile("install")
33 27 @Slf4j
34 28 public class CassandraDatabaseSchemaService implements DatabaseSchemaService {
35 29
36   - private static final String CASSANDRA_DIR = "cassandra";
37   - private static final String SCHEMA_CQL = "schema.cql";
38   -
39 30 @Autowired
40   - private CassandraInstallCluster cluster;
  31 + private CassandraEntityDatabaseSchemaService cassandraEntityDatabaseSchemaService;
41 32
42 33 @Autowired
43   - private InstallScripts installScripts;
  34 + private CassandraTsDatabaseSchemaService cassandraTsDatabaseSchemaService;
  35 +
44 36
45 37 @Override
46 38 public void createDatabaseSchema() throws Exception {
47 39 log.info("Installing Cassandra DataBase schema...");
48   - Path schemaFile = Paths.get(installScripts.getDataDir(), CASSANDRA_DIR, SCHEMA_CQL);
49   - loadCql(schemaFile);
50   -
51   - }
52   -
53   - private void loadCql(Path cql) throws Exception {
54   - List<String> statements = new CQLStatementsParser(cql).getStatements();
55   - statements.forEach(statement -> cluster.getSession().execute(statement));
  40 + cassandraEntityDatabaseSchemaService.createDatabaseSchema();
  41 + cassandraTsDatabaseSchemaService.createDatabaseSchema();
56 42 }
57 43 }
... ...
  1 +/**
  2 + * Copyright © 2016-2018 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.service.install;
  17 +
  18 +import org.springframework.stereotype.Service;
  19 +
  20 +@Service
  21 +public class CassandraEntityDatabaseSchemaService extends CassandraAbstractDatabaseSchemaService {
  22 + public CassandraEntityDatabaseSchemaService() {
  23 + super("schema-entities.cql");
  24 + }
  25 +}
... ...
  1 +/**
  2 + * Copyright © 2016-2018 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.service.install;
  17 +
  18 +import org.springframework.stereotype.Service;
  19 +
  20 +@Service
  21 +public class CassandraTsDatabaseSchemaService extends CassandraAbstractDatabaseSchemaService {
  22 + public CassandraTsDatabaseSchemaService() {
  23 + super("schema-ts.cql");
  24 + }
  25 +}
... ...
  1 +/**
  2 + * Copyright © 2016-2018 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.service.install;
  17 +
  18 +import lombok.extern.slf4j.Slf4j;
  19 +import org.springframework.beans.factory.annotation.Autowired;
  20 +import org.springframework.context.annotation.Profile;
  21 +import org.springframework.stereotype.Service;
  22 +import org.thingsboard.server.dao.util.HybridDao;
  23 +
  24 +@Service
  25 +@Profile("install")
  26 +@Slf4j
  27 +@HybridDao
  28 +public class HybridDatabaseSchemaService implements DatabaseSchemaService {
  29 +
  30 + @Autowired
  31 + private SqlEntityDatabaseSchemaService sqlEntityDatabaseSchemaService;
  32 +
  33 + @Autowired
  34 + private CassandraTsDatabaseSchemaService cassandraTsDatabaseSchemaService;
  35 +
  36 +
  37 + @Override
  38 + public void createDatabaseSchema() throws Exception {
  39 + log.info("Installing Hybrid SQL/Cassandra DataBase schema...");
  40 + sqlEntityDatabaseSchemaService.createDatabaseSchema();
  41 + cassandraTsDatabaseSchemaService.createDatabaseSchema();
  42 + }
  43 +
  44 +}
... ...
  1 +/**
  2 + * Copyright © 2016-2018 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.service.install;
  17 +
  18 +import lombok.extern.slf4j.Slf4j;
  19 +import org.springframework.beans.factory.annotation.Autowired;
  20 +import org.springframework.beans.factory.annotation.Value;
  21 +
  22 +import java.nio.charset.Charset;
  23 +import java.nio.file.Files;
  24 +import java.nio.file.Path;
  25 +import java.nio.file.Paths;
  26 +import java.sql.Connection;
  27 +import java.sql.DriverManager;
  28 +
  29 +@Slf4j
  30 +public abstract class SqlAbstractDatabaseSchemaService /*implements DatabaseSchemaService*/ {
  31 +
  32 + private static final String SQL_DIR = "sql";
  33 +
  34 + @Value("${spring.datasource.url}")
  35 + private String dbUrl;
  36 +
  37 + @Value("${spring.datasource.username}")
  38 + private String dbUserName;
  39 +
  40 + @Value("${spring.datasource.password}")
  41 + private String dbPassword;
  42 +
  43 + @Autowired
  44 + private InstallScripts installScripts;
  45 +
  46 + private final String schemaSql;
  47 +
  48 + protected SqlAbstractDatabaseSchemaService(String schemaSql) {
  49 + this.schemaSql = schemaSql;
  50 + }
  51 +
  52 + //@Override
  53 + public void createDatabaseSchema() throws Exception {
  54 +
  55 + log.info("Installing SQL DataBase schema part: " + schemaSql);
  56 +
  57 + Path schemaFile = Paths.get(installScripts.getDataDir(), SQL_DIR, schemaSql);
  58 + try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
  59 + String sql = new String(Files.readAllBytes(schemaFile), Charset.forName("UTF-8"));
  60 + conn.createStatement().execute(sql); //NOSONAR, ignoring because method used to load initial thingsboard database schema
  61 + }
  62 +
  63 + }
  64 +
  65 +}
... ...
... ... @@ -17,50 +17,28 @@ package org.thingsboard.server.service.install;
17 17
18 18 import lombok.extern.slf4j.Slf4j;
19 19 import org.springframework.beans.factory.annotation.Autowired;
20   -import org.springframework.beans.factory.annotation.Value;
21 20 import org.springframework.context.annotation.Profile;
22 21 import org.springframework.stereotype.Service;
23 22 import org.thingsboard.server.dao.util.SqlDao;
24 23
25   -import java.nio.charset.Charset;
26   -import java.nio.file.Files;
27   -import java.nio.file.Path;
28   -import java.nio.file.Paths;
29   -import java.sql.Connection;
30   -import java.sql.DriverManager;
31   -
32 24 @Service
33 25 @Profile("install")
34 26 @Slf4j
35 27 @SqlDao
36 28 public class SqlDatabaseSchemaService implements DatabaseSchemaService {
37 29
38   - private static final String SQL_DIR = "sql";
39   - private static final String SCHEMA_SQL = "schema.sql";
40   -
41   - @Value("${spring.datasource.url}")
42   - private String dbUrl;
43   -
44   - @Value("${spring.datasource.username}")
45   - private String dbUserName;
46   -
47   - @Value("${spring.datasource.password}")
48   - private String dbPassword;
  30 + @Autowired
  31 + private SqlEntityDatabaseSchemaService sqlEntityDatabaseSchemaService;
49 32
50 33 @Autowired
51   - private InstallScripts installScripts;
  34 + private SqlTsDatabaseSchemaService sqlTsDatabaseSchemaService;
  35 +
52 36
53 37 @Override
54 38 public void createDatabaseSchema() throws Exception {
55   -
56 39 log.info("Installing SQL DataBase schema...");
57   -
58   - Path schemaFile = Paths.get(installScripts.getDataDir(), SQL_DIR, SCHEMA_SQL);
59   - try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
60   - String sql = new String(Files.readAllBytes(schemaFile), Charset.forName("UTF-8"));
61   - conn.createStatement().execute(sql); //NOSONAR, ignoring because method used to load initial thingsboard database schema
62   - }
63   -
  40 + sqlEntityDatabaseSchemaService.createDatabaseSchema();
  41 + sqlTsDatabaseSchemaService.createDatabaseSchema();
64 42 }
65 43
66 44 }
... ...
  1 +/**
  2 + * Copyright © 2016-2018 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.service.install;
  17 +
  18 +import org.springframework.stereotype.Service;
  19 +
  20 +@Service
  21 +public class SqlEntityDatabaseSchemaService extends SqlAbstractDatabaseSchemaService {
  22 + public SqlEntityDatabaseSchemaService() {
  23 + super("schema-entities.sql");
  24 + }
  25 +}
... ...
  1 +/**
  2 + * Copyright © 2016-2018 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.service.install;
  17 +
  18 +import org.springframework.stereotype.Service;
  19 +
  20 +@Service
  21 +public class SqlTsDatabaseSchemaService extends SqlAbstractDatabaseSchemaService {
  22 + public SqlTsDatabaseSchemaService() {
  23 + super("schema-ts.sql");
  24 + }
  25 +}
\ No newline at end of file
... ...
  1 +/**
  2 + * Copyright © 2016-2018 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.dao.util;
  17 +
  18 +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
  19 +
  20 +@ConditionalOnExpression("'${database.entity.type}'=='sql' && '${database.ts.type}'=='cassandra'")
  21 +public @interface HybridDao {
  22 +}
... ...
  1 +--
  2 +-- Copyright © 2016-2018 The Thingsboard Authors
  3 +--
  4 +-- Licensed under the Apache License, Version 2.0 (the "License");
  5 +-- you may not use this file except in compliance with the License.
  6 +-- You may obtain a copy of the License at
  7 +--
  8 +-- http://www.apache.org/licenses/LICENSE-2.0
  9 +--
  10 +-- Unless required by applicable law or agreed to in writing, software
  11 +-- distributed under the License is distributed on an "AS IS" BASIS,
  12 +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 +-- See the License for the specific language governing permissions and
  14 +-- limitations under the License.
  15 +--
  16 +
  17 +CREATE KEYSPACE IF NOT EXISTS thingsboard
  18 +WITH replication = {
  19 + 'class' : 'SimpleStrategy',
  20 + 'replication_factor' : 1
  21 +};
  22 +
  23 +CREATE TABLE IF NOT EXISTS thingsboard.user (
  24 + id timeuuid,
  25 + tenant_id timeuuid,
  26 + customer_id timeuuid,
  27 + email text,
  28 + search_text text,
  29 + authority text,
  30 + first_name text,
  31 + last_name text,
  32 + additional_info text,
  33 + PRIMARY KEY (id, tenant_id, customer_id, authority)
  34 +);
  35 +
  36 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.user_by_email AS
  37 + SELECT *
  38 + from thingsboard.user
  39 + 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
  40 + NULL
  41 + PRIMARY KEY ( email, tenant_id, customer_id, id, authority );
  42 +
  43 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.user_by_tenant_and_search_text AS
  44 + SELECT *
  45 + from thingsboard.user
  46 + 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
  47 + IS NOT NULL
  48 + PRIMARY KEY ( tenant_id, customer_id, authority, search_text, id )
  49 + WITH CLUSTERING ORDER BY ( customer_id DESC, authority DESC, search_text ASC, id DESC );
  50 +
  51 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.user_by_customer_and_search_text AS
  52 + SELECT *
  53 + from thingsboard.user
  54 + 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
  55 + IS NOT NULL
  56 + PRIMARY KEY ( customer_id, tenant_id, authority, search_text, id )
  57 + WITH CLUSTERING ORDER BY ( tenant_id DESC, authority DESC, search_text ASC, id DESC );
  58 +
  59 +CREATE TABLE IF NOT EXISTS thingsboard.user_credentials (
  60 + id timeuuid PRIMARY KEY,
  61 + user_id timeuuid,
  62 + enabled boolean,
  63 + password text,
  64 + activate_token text,
  65 + reset_token text
  66 +);
  67 +
  68 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.user_credentials_by_user AS
  69 + SELECT *
  70 + from thingsboard.user_credentials
  71 + WHERE user_id IS NOT NULL AND id IS NOT NULL
  72 + PRIMARY KEY ( user_id, id );
  73 +
  74 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.user_credentials_by_activate_token AS
  75 + SELECT *
  76 + from thingsboard.user_credentials
  77 + WHERE activate_token IS NOT NULL AND id IS NOT NULL
  78 + PRIMARY KEY ( activate_token, id );
  79 +
  80 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.user_credentials_by_reset_token AS
  81 + SELECT *
  82 + from thingsboard.user_credentials
  83 + WHERE reset_token IS NOT NULL AND id IS NOT NULL
  84 + PRIMARY KEY ( reset_token, id );
  85 +
  86 +CREATE TABLE IF NOT EXISTS thingsboard.admin_settings (
  87 + id timeuuid PRIMARY KEY,
  88 + key text,
  89 + json_value text
  90 +);
  91 +
  92 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.admin_settings_by_key AS
  93 + SELECT *
  94 + from thingsboard.admin_settings
  95 + WHERE key IS NOT NULL AND id IS NOT NULL
  96 + PRIMARY KEY ( key, id )
  97 + WITH CLUSTERING ORDER BY ( id DESC );
  98 +
  99 +CREATE TABLE IF NOT EXISTS thingsboard.tenant (
  100 + id timeuuid,
  101 + title text,
  102 + search_text text,
  103 + region text,
  104 + country text,
  105 + state text,
  106 + city text,
  107 + address text,
  108 + address2 text,
  109 + zip text,
  110 + phone text,
  111 + email text,
  112 + additional_info text,
  113 + PRIMARY KEY (id, region)
  114 +);
  115 +
  116 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.tenant_by_region_and_search_text AS
  117 + SELECT *
  118 + from thingsboard.tenant
  119 + WHERE region IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL
  120 + PRIMARY KEY ( region, search_text, id )
  121 + WITH CLUSTERING ORDER BY ( search_text ASC, id DESC );
  122 +
  123 +CREATE TABLE IF NOT EXISTS thingsboard.customer (
  124 + id timeuuid,
  125 + tenant_id timeuuid,
  126 + title text,
  127 + search_text text,
  128 + country text,
  129 + state text,
  130 + city text,
  131 + address text,
  132 + address2 text,
  133 + zip text,
  134 + phone text,
  135 + email text,
  136 + additional_info text,
  137 + PRIMARY KEY (id, tenant_id)
  138 +);
  139 +
  140 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.customer_by_tenant_and_title AS
  141 + SELECT *
  142 + from thingsboard.customer
  143 + WHERE tenant_id IS NOT NULL AND title IS NOT NULL AND id IS NOT NULL
  144 + PRIMARY KEY ( tenant_id, title, id )
  145 + WITH CLUSTERING ORDER BY ( title ASC, id DESC );
  146 +
  147 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.customer_by_tenant_and_search_text AS
  148 + SELECT *
  149 + from thingsboard.customer
  150 + WHERE tenant_id IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL
  151 + PRIMARY KEY ( tenant_id, search_text, id )
  152 + WITH CLUSTERING ORDER BY ( search_text ASC, id DESC );
  153 +
  154 +CREATE TABLE IF NOT EXISTS thingsboard.device (
  155 + id timeuuid,
  156 + tenant_id timeuuid,
  157 + customer_id timeuuid,
  158 + name text,
  159 + type text,
  160 + search_text text,
  161 + additional_info text,
  162 + PRIMARY KEY (id, tenant_id, customer_id, type)
  163 +);
  164 +
  165 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.device_by_tenant_and_name AS
  166 + SELECT *
  167 + from thingsboard.device
  168 + 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
  169 + PRIMARY KEY ( tenant_id, name, id, customer_id, type)
  170 + WITH CLUSTERING ORDER BY ( name ASC, id DESC, customer_id DESC);
  171 +
  172 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.device_by_tenant_and_search_text AS
  173 + SELECT *
  174 + from thingsboard.device
  175 + 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
  176 + PRIMARY KEY ( tenant_id, search_text, id, customer_id, type)
  177 + WITH CLUSTERING ORDER BY ( search_text ASC, id DESC, customer_id DESC);
  178 +
  179 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.device_by_tenant_by_type_and_search_text AS
  180 + SELECT *
  181 + from thingsboard.device
  182 + 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
  183 + PRIMARY KEY ( tenant_id, type, search_text, id, customer_id)
  184 + WITH CLUSTERING ORDER BY ( type ASC, search_text ASC, id DESC, customer_id DESC);
  185 +
  186 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.device_by_customer_and_search_text AS
  187 + SELECT *
  188 + from thingsboard.device
  189 + 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
  190 + PRIMARY KEY ( customer_id, tenant_id, search_text, id, type )
  191 + WITH CLUSTERING ORDER BY ( tenant_id DESC, search_text ASC, id DESC );
  192 +
  193 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.device_by_customer_by_type_and_search_text AS
  194 + SELECT *
  195 + from thingsboard.device
  196 + 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
  197 + PRIMARY KEY ( customer_id, tenant_id, type, search_text, id )
  198 + WITH CLUSTERING ORDER BY ( tenant_id DESC, type ASC, search_text ASC, id DESC );
  199 +
  200 +CREATE TABLE IF NOT EXISTS thingsboard.device_credentials (
  201 + id timeuuid PRIMARY KEY,
  202 + device_id timeuuid,
  203 + credentials_type text,
  204 + credentials_id text,
  205 + credentials_value text
  206 +);
  207 +
  208 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.device_credentials_by_device AS
  209 + SELECT *
  210 + from thingsboard.device_credentials
  211 + WHERE device_id IS NOT NULL AND id IS NOT NULL
  212 + PRIMARY KEY ( device_id, id );
  213 +
  214 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.device_credentials_by_credentials_id AS
  215 + SELECT *
  216 + from thingsboard.device_credentials
  217 + WHERE credentials_id IS NOT NULL AND id IS NOT NULL
  218 + PRIMARY KEY ( credentials_id, id );
  219 +
  220 +CREATE TABLE IF NOT EXISTS thingsboard.asset (
  221 + id timeuuid,
  222 + tenant_id timeuuid,
  223 + customer_id timeuuid,
  224 + name text,
  225 + type text,
  226 + search_text text,
  227 + additional_info text,
  228 + PRIMARY KEY (id, tenant_id, customer_id, type)
  229 +);
  230 +
  231 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.asset_by_tenant_and_name AS
  232 + SELECT *
  233 + from thingsboard.asset
  234 + 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
  235 + PRIMARY KEY ( tenant_id, name, id, customer_id, type)
  236 + WITH CLUSTERING ORDER BY ( name ASC, id DESC, customer_id DESC);
  237 +
  238 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.asset_by_tenant_and_search_text AS
  239 + SELECT *
  240 + from thingsboard.asset
  241 + 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
  242 + PRIMARY KEY ( tenant_id, search_text, id, customer_id, type)
  243 + WITH CLUSTERING ORDER BY ( search_text ASC, id DESC, customer_id DESC);
  244 +
  245 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.asset_by_tenant_by_type_and_search_text AS
  246 + SELECT *
  247 + from thingsboard.asset
  248 + 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
  249 + PRIMARY KEY ( tenant_id, type, search_text, id, customer_id)
  250 + WITH CLUSTERING ORDER BY ( type ASC, search_text ASC, id DESC, customer_id DESC);
  251 +
  252 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.asset_by_customer_and_search_text AS
  253 + SELECT *
  254 + from thingsboard.asset
  255 + 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
  256 + PRIMARY KEY ( customer_id, tenant_id, search_text, id, type )
  257 + WITH CLUSTERING ORDER BY ( tenant_id DESC, search_text ASC, id DESC );
  258 +
  259 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.asset_by_customer_by_type_and_search_text AS
  260 + SELECT *
  261 + from thingsboard.asset
  262 + 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
  263 + PRIMARY KEY ( customer_id, tenant_id, type, search_text, id )
  264 + WITH CLUSTERING ORDER BY ( tenant_id DESC, type ASC, search_text ASC, id DESC );
  265 +
  266 +CREATE TABLE IF NOT EXISTS thingsboard.entity_subtype (
  267 + tenant_id timeuuid,
  268 + entity_type text, // (DEVICE, ASSET)
  269 + type text,
  270 + PRIMARY KEY (tenant_id, entity_type, type)
  271 +);
  272 +
  273 +CREATE TABLE IF NOT EXISTS thingsboard.alarm (
  274 + id timeuuid,
  275 + tenant_id timeuuid,
  276 + type text,
  277 + originator_id timeuuid,
  278 + originator_type text,
  279 + severity text,
  280 + status text,
  281 + start_ts bigint,
  282 + end_ts bigint,
  283 + ack_ts bigint,
  284 + clear_ts bigint,
  285 + details text,
  286 + propagate boolean,
  287 + PRIMARY KEY ((tenant_id, originator_id, originator_type), type, id)
  288 +) WITH CLUSTERING ORDER BY ( type ASC, id DESC);
  289 +
  290 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.alarm_by_id AS
  291 + SELECT *
  292 + from thingsboard.alarm
  293 + WHERE tenant_id IS NOT NULL AND originator_id IS NOT NULL AND originator_type IS NOT NULL AND type IS NOT NULL
  294 + AND type IS NOT NULL AND id IS NOT NULL
  295 + PRIMARY KEY (id, tenant_id, originator_id, originator_type, type)
  296 + WITH CLUSTERING ORDER BY ( tenant_id ASC, originator_id ASC, originator_type ASC, type ASC);
  297 +
  298 +CREATE TABLE IF NOT EXISTS thingsboard.relation (
  299 + from_id timeuuid,
  300 + from_type text,
  301 + to_id timeuuid,
  302 + to_type text,
  303 + relation_type_group text,
  304 + relation_type text,
  305 + additional_info text,
  306 + PRIMARY KEY ((from_id, from_type), relation_type_group, relation_type, to_id, to_type)
  307 +) WITH CLUSTERING ORDER BY ( relation_type_group ASC, relation_type ASC, to_id ASC, to_type ASC);
  308 +
  309 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.relation_by_type_and_child_type AS
  310 + SELECT *
  311 + from thingsboard.relation
  312 + 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
  313 + PRIMARY KEY ((from_id, from_type), relation_type_group, relation_type, to_type, to_id)
  314 + WITH CLUSTERING ORDER BY ( relation_type_group ASC, relation_type ASC, to_type ASC, to_id DESC);
  315 +
  316 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.reverse_relation AS
  317 + SELECT *
  318 + from thingsboard.relation
  319 + 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
  320 + PRIMARY KEY ((to_id, to_type), relation_type_group, relation_type, from_id, from_type)
  321 + WITH CLUSTERING ORDER BY ( relation_type_group ASC, relation_type ASC, from_id ASC, from_type ASC);
  322 +
  323 +CREATE TABLE IF NOT EXISTS thingsboard.widgets_bundle (
  324 + id timeuuid,
  325 + tenant_id timeuuid,
  326 + alias text,
  327 + title text,
  328 + search_text text,
  329 + image blob,
  330 + PRIMARY KEY (id, tenant_id)
  331 +);
  332 +
  333 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.widgets_bundle_by_tenant_and_search_text AS
  334 + SELECT *
  335 + from thingsboard.widgets_bundle
  336 + WHERE tenant_id IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL
  337 + PRIMARY KEY ( tenant_id, search_text, id )
  338 + WITH CLUSTERING ORDER BY ( search_text ASC, id DESC );
  339 +
  340 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.widgets_bundle_by_tenant_and_alias AS
  341 + SELECT *
  342 + from thingsboard.widgets_bundle
  343 + WHERE tenant_id IS NOT NULL AND alias IS NOT NULL AND id IS NOT NULL
  344 + PRIMARY KEY ( tenant_id, alias, id )
  345 + WITH CLUSTERING ORDER BY ( alias ASC, id DESC );
  346 +
  347 +CREATE TABLE IF NOT EXISTS thingsboard.widget_type (
  348 + id timeuuid,
  349 + tenant_id timeuuid,
  350 + bundle_alias text,
  351 + alias text,
  352 + name text,
  353 + descriptor text,
  354 + PRIMARY KEY (id, tenant_id, bundle_alias)
  355 +);
  356 +
  357 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.widget_type_by_tenant_and_aliases AS
  358 + SELECT *
  359 + from thingsboard.widget_type
  360 + WHERE tenant_id IS NOT NULL AND bundle_alias IS NOT NULL AND alias IS NOT NULL AND id IS NOT NULL
  361 + PRIMARY KEY ( tenant_id, bundle_alias, alias, id )
  362 + WITH CLUSTERING ORDER BY ( bundle_alias ASC, alias ASC, id DESC );
  363 +
  364 +CREATE TABLE IF NOT EXISTS thingsboard.dashboard (
  365 + id timeuuid,
  366 + tenant_id timeuuid,
  367 + title text,
  368 + search_text text,
  369 + assigned_customers text,
  370 + configuration text,
  371 + PRIMARY KEY (id, tenant_id)
  372 +);
  373 +
  374 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.dashboard_by_tenant_and_search_text AS
  375 + SELECT *
  376 + from thingsboard.dashboard
  377 + WHERE tenant_id IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL
  378 + PRIMARY KEY ( tenant_id, search_text, id )
  379 + WITH CLUSTERING ORDER BY ( search_text ASC, id DESC );
  380 +
  381 +CREATE TABLE IF NOT EXISTS thingsboard.attributes_kv_cf (
  382 + entity_type text, // (DEVICE, CUSTOMER, TENANT)
  383 + entity_id timeuuid,
  384 + attribute_type text, // (CLIENT_SIDE, SHARED, SERVER_SIDE)
  385 + attribute_key text,
  386 + bool_v boolean,
  387 + str_v text,
  388 + long_v bigint,
  389 + dbl_v double,
  390 + last_update_ts bigint,
  391 + PRIMARY KEY ((entity_type, entity_id, attribute_type), attribute_key)
  392 +) WITH compaction = { 'class' : 'LeveledCompactionStrategy' };
  393 +
  394 +CREATE TABLE IF NOT EXISTS thingsboard.component_descriptor (
  395 + id timeuuid,
  396 + type text,
  397 + scope text,
  398 + name text,
  399 + search_text text,
  400 + clazz text,
  401 + configuration_descriptor text,
  402 + actions text,
  403 + PRIMARY KEY (clazz, id, type, scope)
  404 +);
  405 +
  406 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.component_desc_by_type_search_text AS
  407 + SELECT *
  408 + from thingsboard.component_descriptor
  409 + 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
  410 + PRIMARY KEY ( type, search_text, id, clazz, scope)
  411 + WITH CLUSTERING ORDER BY ( search_text DESC);
  412 +
  413 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.component_desc_by_scope_type_search_text AS
  414 + SELECT *
  415 + from thingsboard.component_descriptor
  416 + 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
  417 + PRIMARY KEY ( (scope, type), search_text, id, clazz)
  418 + WITH CLUSTERING ORDER BY ( search_text DESC);
  419 +
  420 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.component_desc_by_id AS
  421 + SELECT *
  422 + from thingsboard.component_descriptor
  423 + WHERE type IS NOT NULL AND scope IS NOT NULL AND id IS NOT NULL AND clazz IS NOT NULL
  424 + PRIMARY KEY ( id, clazz, scope, type )
  425 + WITH CLUSTERING ORDER BY ( clazz ASC, scope ASC, type DESC);
  426 +
  427 +CREATE TABLE IF NOT EXISTS thingsboard.event (
  428 + tenant_id timeuuid, // tenant or system
  429 + id timeuuid,
  430 + event_type text,
  431 + event_uid text,
  432 + entity_type text,
  433 + entity_id timeuuid,
  434 + body text,
  435 + PRIMARY KEY ((tenant_id, entity_type, entity_id), event_type, event_uid)
  436 +);
  437 +
  438 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.event_by_type_and_id AS
  439 + SELECT *
  440 + FROM thingsboard.event
  441 + WHERE tenant_id IS NOT NULL AND entity_type IS NOT NULL AND entity_id IS NOT NULL AND id IS NOT NULL
  442 + AND event_type IS NOT NULL AND event_uid IS NOT NULL
  443 + PRIMARY KEY ((tenant_id, entity_type, entity_id), event_type, id, event_uid)
  444 + WITH CLUSTERING ORDER BY (event_type ASC, id ASC, event_uid ASC);
  445 +
  446 +
  447 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.event_by_id AS
  448 + SELECT *
  449 + FROM thingsboard.event
  450 + WHERE tenant_id IS NOT NULL AND entity_type IS NOT NULL AND entity_id IS NOT NULL AND id IS NOT NULL
  451 + AND event_type IS NOT NULL AND event_uid IS NOT NULL
  452 + PRIMARY KEY ((tenant_id, entity_type, entity_id), id, event_type, event_uid)
  453 + WITH CLUSTERING ORDER BY (id ASC, event_type ASC, event_uid ASC);
  454 +
  455 +CREATE TABLE IF NOT EXISTS thingsboard.audit_log_by_entity_id (
  456 + tenant_id timeuuid,
  457 + id timeuuid,
  458 + customer_id timeuuid,
  459 + entity_id timeuuid,
  460 + entity_type text,
  461 + entity_name text,
  462 + user_id timeuuid,
  463 + user_name text,
  464 + action_type text,
  465 + action_data text,
  466 + action_status text,
  467 + action_failure_details text,
  468 + PRIMARY KEY ((tenant_id, entity_id, entity_type), id)
  469 +);
  470 +
  471 +CREATE TABLE IF NOT EXISTS thingsboard.audit_log_by_customer_id (
  472 + tenant_id timeuuid,
  473 + id timeuuid,
  474 + customer_id timeuuid,
  475 + entity_id timeuuid,
  476 + entity_type text,
  477 + entity_name text,
  478 + user_id timeuuid,
  479 + user_name text,
  480 + action_type text,
  481 + action_data text,
  482 + action_status text,
  483 + action_failure_details text,
  484 + PRIMARY KEY ((tenant_id, customer_id), id)
  485 +);
  486 +
  487 +CREATE TABLE IF NOT EXISTS thingsboard.audit_log_by_user_id (
  488 + tenant_id timeuuid,
  489 + id timeuuid,
  490 + customer_id timeuuid,
  491 + entity_id timeuuid,
  492 + entity_type text,
  493 + entity_name text,
  494 + user_id timeuuid,
  495 + user_name text,
  496 + action_type text,
  497 + action_data text,
  498 + action_status text,
  499 + action_failure_details text,
  500 + PRIMARY KEY ((tenant_id, user_id), id)
  501 +);
  502 +
  503 +CREATE TABLE IF NOT EXISTS thingsboard.audit_log_by_tenant_id (
  504 + tenant_id timeuuid,
  505 + id timeuuid,
  506 + partition bigint,
  507 + customer_id timeuuid,
  508 + entity_id timeuuid,
  509 + entity_type text,
  510 + entity_name text,
  511 + user_id timeuuid,
  512 + user_name text,
  513 + action_type text,
  514 + action_data text,
  515 + action_status text,
  516 + action_failure_details text,
  517 + PRIMARY KEY ((tenant_id, partition), id)
  518 +);
  519 +
  520 +CREATE TABLE IF NOT EXISTS thingsboard.audit_log_by_tenant_id_partitions (
  521 + tenant_id timeuuid,
  522 + partition bigint,
  523 + PRIMARY KEY (( tenant_id ), partition)
  524 +) WITH CLUSTERING ORDER BY ( partition ASC )
  525 +AND compaction = { 'class' : 'LeveledCompactionStrategy' };
  526 +
  527 +CREATE TABLE IF NOT EXISTS thingsboard.msg_queue (
  528 + node_id timeuuid,
  529 + cluster_partition bigint,
  530 + ts_partition bigint,
  531 + ts bigint,
  532 + msg blob,
  533 + PRIMARY KEY ((node_id, cluster_partition, ts_partition), ts))
  534 +WITH CLUSTERING ORDER BY (ts DESC)
  535 +AND compaction = {
  536 + 'class': 'org.apache.cassandra.db.compaction.DateTieredCompactionStrategy',
  537 + 'min_threshold': '5',
  538 + 'base_time_seconds': '43200',
  539 + 'max_window_size_seconds': '43200',
  540 + 'tombstone_threshold': '0.9',
  541 + 'unchecked_tombstone_compaction': 'true'
  542 +};
  543 +
  544 +CREATE TABLE IF NOT EXISTS thingsboard.msg_ack_queue (
  545 + node_id timeuuid,
  546 + cluster_partition bigint,
  547 + ts_partition bigint,
  548 + msg_id timeuuid,
  549 + PRIMARY KEY ((node_id, cluster_partition, ts_partition), msg_id))
  550 +WITH CLUSTERING ORDER BY (msg_id DESC)
  551 +AND compaction = {
  552 + 'class': 'org.apache.cassandra.db.compaction.DateTieredCompactionStrategy',
  553 + 'min_threshold': '5',
  554 + 'base_time_seconds': '43200',
  555 + 'max_window_size_seconds': '43200',
  556 + 'tombstone_threshold': '0.9',
  557 + 'unchecked_tombstone_compaction': 'true'
  558 +};
  559 +
  560 +CREATE TABLE IF NOT EXISTS thingsboard.processed_msg_partitions (
  561 + node_id timeuuid,
  562 + cluster_partition bigint,
  563 + ts_partition bigint,
  564 + PRIMARY KEY ((node_id, cluster_partition), ts_partition))
  565 +WITH CLUSTERING ORDER BY (ts_partition DESC)
  566 +AND compaction = {
  567 + 'class': 'org.apache.cassandra.db.compaction.DateTieredCompactionStrategy',
  568 + 'min_threshold': '5',
  569 + 'base_time_seconds': '43200',
  570 + 'max_window_size_seconds': '43200',
  571 + 'tombstone_threshold': '0.9',
  572 + 'unchecked_tombstone_compaction': 'true'
  573 +};
  574 +
  575 +CREATE TABLE IF NOT EXISTS thingsboard.rule_chain (
  576 + id uuid,
  577 + tenant_id uuid,
  578 + name text,
  579 + search_text text,
  580 + first_rule_node_id uuid,
  581 + root boolean,
  582 + debug_mode boolean,
  583 + configuration text,
  584 + additional_info text,
  585 + PRIMARY KEY (id, tenant_id)
  586 +);
  587 +
  588 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.rule_chain_by_tenant_and_search_text AS
  589 + SELECT *
  590 + from thingsboard.rule_chain
  591 + WHERE tenant_id IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL
  592 + PRIMARY KEY ( tenant_id, search_text, id )
  593 + WITH CLUSTERING ORDER BY ( search_text ASC, id DESC );
  594 +
  595 +CREATE TABLE IF NOT EXISTS thingsboard.rule_node (
  596 + id uuid,
  597 + rule_chain_id uuid,
  598 + type text,
  599 + name text,
  600 + debug_mode boolean,
  601 + search_text text,
  602 + configuration text,
  603 + additional_info text,
  604 + PRIMARY KEY (id)
  605 +);
... ...
  1 +--
  2 +-- Copyright © 2016-2018 The Thingsboard Authors
  3 +--
  4 +-- Licensed under the Apache License, Version 2.0 (the "License");
  5 +-- you may not use this file except in compliance with the License.
  6 +-- You may obtain a copy of the License at
  7 +--
  8 +-- http://www.apache.org/licenses/LICENSE-2.0
  9 +--
  10 +-- Unless required by applicable law or agreed to in writing, software
  11 +-- distributed under the License is distributed on an "AS IS" BASIS,
  12 +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 +-- See the License for the specific language governing permissions and
  14 +-- limitations under the License.
  15 +--
  16 +
  17 +CREATE KEYSPACE IF NOT EXISTS thingsboard
  18 +WITH replication = {
  19 + 'class' : 'SimpleStrategy',
  20 + 'replication_factor' : 1
  21 +};
  22 +
  23 +CREATE TABLE IF NOT EXISTS thingsboard.ts_kv_cf (
  24 + entity_type text, // (DEVICE, CUSTOMER, TENANT)
  25 + entity_id timeuuid,
  26 + key text,
  27 + partition bigint,
  28 + ts bigint,
  29 + bool_v boolean,
  30 + str_v text,
  31 + long_v bigint,
  32 + dbl_v double,
  33 + PRIMARY KEY (( entity_type, entity_id, key, partition ), ts)
  34 +);
  35 +
  36 +CREATE TABLE IF NOT EXISTS thingsboard.ts_kv_partitions_cf (
  37 + entity_type text, // (DEVICE, CUSTOMER, TENANT)
  38 + entity_id timeuuid,
  39 + key text,
  40 + partition bigint,
  41 + PRIMARY KEY (( entity_type, entity_id, key ), partition)
  42 +) WITH CLUSTERING ORDER BY ( partition ASC )
  43 + AND compaction = { 'class' : 'LeveledCompactionStrategy' };
  44 +
  45 +CREATE TABLE IF NOT EXISTS thingsboard.ts_kv_latest_cf (
  46 + entity_type text, // (DEVICE, CUSTOMER, TENANT)
  47 + entity_id timeuuid,
  48 + key text,
  49 + ts bigint,
  50 + bool_v boolean,
  51 + str_v text,
  52 + long_v bigint,
  53 + dbl_v double,
  54 + PRIMARY KEY (( entity_type, entity_id ), key)
  55 +) WITH compaction = { 'class' : 'LeveledCompactionStrategy' };
... ...
  1 +--
  2 +-- Copyright © 2016-2018 The Thingsboard Authors
  3 +--
  4 +-- Licensed under the Apache License, Version 2.0 (the "License");
  5 +-- you may not use this file except in compliance with the License.
  6 +-- You may obtain a copy of the License at
  7 +--
  8 +-- http://www.apache.org/licenses/LICENSE-2.0
  9 +--
  10 +-- Unless required by applicable law or agreed to in writing, software
  11 +-- distributed under the License is distributed on an "AS IS" BASIS,
  12 +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 +-- See the License for the specific language governing permissions and
  14 +-- limitations under the License.
  15 +--
  16 +
  17 +
  18 +CREATE TABLE IF NOT EXISTS admin_settings (
  19 + id varchar(31) NOT NULL CONSTRAINT admin_settings_pkey PRIMARY KEY,
  20 + json_value varchar,
  21 + key varchar(255)
  22 +);
  23 +
  24 +CREATE TABLE IF NOT EXISTS alarm (
  25 + id varchar(31) NOT NULL CONSTRAINT alarm_pkey PRIMARY KEY,
  26 + ack_ts bigint,
  27 + clear_ts bigint,
  28 + additional_info varchar,
  29 + end_ts bigint,
  30 + originator_id varchar(31),
  31 + originator_type integer,
  32 + propagate boolean,
  33 + severity varchar(255),
  34 + start_ts bigint,
  35 + status varchar(255),
  36 + tenant_id varchar(31),
  37 + type varchar(255)
  38 +);
  39 +
  40 +CREATE TABLE IF NOT EXISTS asset (
  41 + id varchar(31) NOT NULL CONSTRAINT asset_pkey PRIMARY KEY,
  42 + additional_info varchar,
  43 + customer_id varchar(31),
  44 + name varchar(255),
  45 + search_text varchar(255),
  46 + tenant_id varchar(31),
  47 + type varchar(255)
  48 +);
  49 +
  50 +CREATE TABLE IF NOT EXISTS audit_log (
  51 + id varchar(31) NOT NULL CONSTRAINT audit_log_pkey PRIMARY KEY,
  52 + tenant_id varchar(31),
  53 + customer_id varchar(31),
  54 + entity_id varchar(31),
  55 + entity_type varchar(255),
  56 + entity_name varchar(255),
  57 + user_id varchar(31),
  58 + user_name varchar(255),
  59 + action_type varchar(255),
  60 + action_data varchar(1000000),
  61 + action_status varchar(255),
  62 + action_failure_details varchar(1000000)
  63 +);
  64 +
  65 +CREATE TABLE IF NOT EXISTS attribute_kv (
  66 + entity_type varchar(255),
  67 + entity_id varchar(31),
  68 + attribute_type varchar(255),
  69 + attribute_key varchar(255),
  70 + bool_v boolean,
  71 + str_v varchar(10000000),
  72 + long_v bigint,
  73 + dbl_v double precision,
  74 + last_update_ts bigint,
  75 + CONSTRAINT attribute_kv_unq_key UNIQUE (entity_type, entity_id, attribute_type, attribute_key)
  76 +);
  77 +
  78 +CREATE TABLE IF NOT EXISTS component_descriptor (
  79 + id varchar(31) NOT NULL CONSTRAINT component_descriptor_pkey PRIMARY KEY,
  80 + actions varchar(255),
  81 + clazz varchar,
  82 + configuration_descriptor varchar,
  83 + name varchar(255),
  84 + scope varchar(255),
  85 + search_text varchar(255),
  86 + type varchar(255)
  87 +);
  88 +
  89 +CREATE TABLE IF NOT EXISTS customer (
  90 + id varchar(31) NOT NULL CONSTRAINT customer_pkey PRIMARY KEY,
  91 + additional_info varchar,
  92 + address varchar,
  93 + address2 varchar,
  94 + city varchar(255),
  95 + country varchar(255),
  96 + email varchar(255),
  97 + phone varchar(255),
  98 + search_text varchar(255),
  99 + state varchar(255),
  100 + tenant_id varchar(31),
  101 + title varchar(255),
  102 + zip varchar(255)
  103 +);
  104 +
  105 +CREATE TABLE IF NOT EXISTS dashboard (
  106 + id varchar(31) NOT NULL CONSTRAINT dashboard_pkey PRIMARY KEY,
  107 + configuration varchar(10000000),
  108 + assigned_customers varchar(1000000),
  109 + search_text varchar(255),
  110 + tenant_id varchar(31),
  111 + title varchar(255)
  112 +);
  113 +
  114 +CREATE TABLE IF NOT EXISTS device (
  115 + id varchar(31) NOT NULL CONSTRAINT device_pkey PRIMARY KEY,
  116 + additional_info varchar,
  117 + customer_id varchar(31),
  118 + type varchar(255),
  119 + name varchar(255),
  120 + search_text varchar(255),
  121 + tenant_id varchar(31)
  122 +);
  123 +
  124 +CREATE TABLE IF NOT EXISTS device_credentials (
  125 + id varchar(31) NOT NULL CONSTRAINT device_credentials_pkey PRIMARY KEY,
  126 + credentials_id varchar,
  127 + credentials_type varchar(255),
  128 + credentials_value varchar,
  129 + device_id varchar(31)
  130 +);
  131 +
  132 +CREATE TABLE IF NOT EXISTS event (
  133 + id varchar(31) NOT NULL CONSTRAINT event_pkey PRIMARY KEY,
  134 + body varchar,
  135 + entity_id varchar(31),
  136 + entity_type varchar(255),
  137 + event_type varchar(255),
  138 + event_uid varchar(255),
  139 + tenant_id varchar(31),
  140 + CONSTRAINT event_unq_key UNIQUE (tenant_id, entity_type, entity_id, event_type, event_uid)
  141 +);
  142 +
  143 +CREATE TABLE IF NOT EXISTS relation (
  144 + from_id varchar(31),
  145 + from_type varchar(255),
  146 + to_id varchar(31),
  147 + to_type varchar(255),
  148 + relation_type_group varchar(255),
  149 + relation_type varchar(255),
  150 + additional_info varchar,
  151 + CONSTRAINT relation_unq_key UNIQUE (from_id, from_type, relation_type_group, relation_type, to_id, to_type)
  152 +);
  153 +
  154 +CREATE TABLE IF NOT EXISTS tb_user (
  155 + id varchar(31) NOT NULL CONSTRAINT tb_user_pkey PRIMARY KEY,
  156 + additional_info varchar,
  157 + authority varchar(255),
  158 + customer_id varchar(31),
  159 + email varchar(255) UNIQUE,
  160 + first_name varchar(255),
  161 + last_name varchar(255),
  162 + search_text varchar(255),
  163 + tenant_id varchar(31)
  164 +);
  165 +
  166 +CREATE TABLE IF NOT EXISTS tenant (
  167 + id varchar(31) NOT NULL CONSTRAINT tenant_pkey PRIMARY KEY,
  168 + additional_info varchar,
  169 + address varchar,
  170 + address2 varchar,
  171 + city varchar(255),
  172 + country varchar(255),
  173 + email varchar(255),
  174 + phone varchar(255),
  175 + region varchar(255),
  176 + search_text varchar(255),
  177 + state varchar(255),
  178 + title varchar(255),
  179 + zip varchar(255)
  180 +);
  181 +
  182 +CREATE TABLE IF NOT EXISTS user_credentials (
  183 + id varchar(31) NOT NULL CONSTRAINT user_credentials_pkey PRIMARY KEY,
  184 + activate_token varchar(255) UNIQUE,
  185 + enabled boolean,
  186 + password varchar(255),
  187 + reset_token varchar(255) UNIQUE,
  188 + user_id varchar(31) UNIQUE
  189 +);
  190 +
  191 +CREATE TABLE IF NOT EXISTS widget_type (
  192 + id varchar(31) NOT NULL CONSTRAINT widget_type_pkey PRIMARY KEY,
  193 + alias varchar(255),
  194 + bundle_alias varchar(255),
  195 + descriptor varchar(1000000),
  196 + name varchar(255),
  197 + tenant_id varchar(31)
  198 +);
  199 +
  200 +CREATE TABLE IF NOT EXISTS widgets_bundle (
  201 + id varchar(31) NOT NULL CONSTRAINT widgets_bundle_pkey PRIMARY KEY,
  202 + alias varchar(255),
  203 + search_text varchar(255),
  204 + tenant_id varchar(31),
  205 + title varchar(255)
  206 +);
  207 +
  208 +CREATE TABLE IF NOT EXISTS rule_chain (
  209 + id varchar(31) NOT NULL CONSTRAINT rule_chain_pkey PRIMARY KEY,
  210 + additional_info varchar,
  211 + configuration varchar(10000000),
  212 + name varchar(255),
  213 + first_rule_node_id varchar(31),
  214 + root boolean,
  215 + debug_mode boolean,
  216 + search_text varchar(255),
  217 + tenant_id varchar(31)
  218 +);
  219 +
  220 +CREATE TABLE IF NOT EXISTS rule_node (
  221 + id varchar(31) NOT NULL CONSTRAINT rule_node_pkey PRIMARY KEY,
  222 + rule_chain_id varchar(31),
  223 + additional_info varchar,
  224 + configuration varchar(10000000),
  225 + type varchar(255),
  226 + name varchar(255),
  227 + debug_mode boolean,
  228 + search_text varchar(255)
  229 +);
... ...
  1 +--
  2 +-- Copyright © 2016-2018 The Thingsboard Authors
  3 +--
  4 +-- Licensed under the Apache License, Version 2.0 (the "License");
  5 +-- you may not use this file except in compliance with the License.
  6 +-- You may obtain a copy of the License at
  7 +--
  8 +-- http://www.apache.org/licenses/LICENSE-2.0
  9 +--
  10 +-- Unless required by applicable law or agreed to in writing, software
  11 +-- distributed under the License is distributed on an "AS IS" BASIS,
  12 +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 +-- See the License for the specific language governing permissions and
  14 +-- limitations under the License.
  15 +--
  16 +
  17 +CREATE TABLE IF NOT EXISTS ts_kv (
  18 + entity_type varchar(255) NOT NULL,
  19 + entity_id varchar(31) NOT NULL,
  20 + key varchar(255) NOT NULL,
  21 + ts bigint NOT NULL,
  22 + bool_v boolean,
  23 + str_v varchar(10000000),
  24 + long_v bigint,
  25 + dbl_v double precision,
  26 + CONSTRAINT ts_kv_unq_key UNIQUE (entity_type, entity_id, key, ts)
  27 +);
  28 +
  29 +CREATE TABLE IF NOT EXISTS ts_kv_latest (
  30 + entity_type varchar(255) NOT NULL,
  31 + entity_id varchar(31) NOT NULL,
  32 + key varchar(255) NOT NULL,
  33 + ts bigint NOT NULL,
  34 + bool_v boolean,
  35 + str_v varchar(10000000),
  36 + long_v bigint,
  37 + dbl_v double precision,
  38 + CONSTRAINT ts_kv_latest_unq_key UNIQUE (entity_type, entity_id, key)
  39 +);
... ...