Commit 4f06c390ef9233606f7c70f3d3cd8c072bb21c98

Authored by Volodymyr Babak
1 parent 4b67d428

Unifying SQL DAO

Showing 22 changed files with 104 additions and 89 deletions
... ... @@ -249,11 +249,6 @@
249 249 <artifactId>spring-test-dbunit</artifactId>
250 250 <scope>test</scope>
251 251 </dependency>
252   - <dependency>
253   - <groupId>ru.yandex.qatools.embed</groupId>
254   - <artifactId>postgresql-embedded</artifactId>
255   - <scope>test</scope>
256   - </dependency>
257 252 </dependencies>
258 253
259 254 <build>
... ...
... ... @@ -106,7 +106,7 @@ coap:
106 106 timeout: "${COAP_TIMEOUT:10000}"
107 107
108 108 database:
109   - type: "${DATABASE_TYPE:cassandra}" # cassandra OR postgres
  109 + type: "${DATABASE_TYPE:cassandra}" # cassandra OR sql
110 110
111 111 # Cassandra driver configuration parameters
112 112 cassandra:
... ...
... ... @@ -18,7 +18,7 @@ package org.thingsboard.server.system;
18 18 import org.junit.ClassRule;
19 19 import org.junit.extensions.cpsuite.ClasspathSuite;
20 20 import org.junit.runner.RunWith;
21   -import org.thingsboard.server.dao.CustomPostgresUnit;
  21 +import org.thingsboard.server.dao.CustomSqlUnit;
22 22
23 23 import java.util.Arrays;
24 24
... ... @@ -30,9 +30,10 @@ import java.util.Arrays;
30 30 public class SystemSqlTestSuite {
31 31
32 32 @ClassRule
33   - public static CustomPostgresUnit postgresUnit = new CustomPostgresUnit(
34   - Arrays.asList("postgres/schema.sql", "postgres/system-data.sql"),
35   - "postgres-embedded-test.properties");
  33 + public static CustomSqlUnit sqlUnit = new CustomSqlUnit(
  34 + Arrays.asList("sql/schema.sql", "sql/system-data.sql"),
  35 + "sql-test.properties",
  36 + "sql/drop-all-tables.sql");
36 37
37 38
38 39 }
... ...
... ... @@ -170,8 +170,8 @@
170 170 <scope>test</scope>
171 171 </dependency>
172 172 <dependency>
173   - <groupId>ru.yandex.qatools.embed</groupId>
174   - <artifactId>postgresql-embedded</artifactId>
  173 + <groupId>org.hsqldb</groupId>
  174 + <artifactId>hsqldb</artifactId>
175 175 <scope>test</scope>
176 176 </dependency>
177 177 </dependencies>
... ...
... ... @@ -45,7 +45,7 @@ public abstract class JpaAbstractDao<E extends BaseEntity<D>, D>
45 45 protected void setSearchText(E entity) {}
46 46
47 47 @Override
48   - @Transactional(propagation = REQUIRES_NEW)
  48 + @Transactional
49 49 public D save(D domain) {
50 50 E entity;
51 51 try {
... ... @@ -64,7 +64,6 @@ public abstract class JpaAbstractDao<E extends BaseEntity<D>, D>
64 64 }
65 65
66 66 @Override
67   - @Transactional(propagation = REQUIRES_NEW)
68 67 public D findById(UUID key) {
69 68 log.debug("Get entity by key {}", key);
70 69 E entity = getCrudRepository().findOne(key);
... ... @@ -78,7 +77,7 @@ public abstract class JpaAbstractDao<E extends BaseEntity<D>, D>
78 77 }
79 78
80 79 @Override
81   - @Transactional(propagation = REQUIRES_NEW)
  80 + @Transactional
82 81 public boolean removeById(UUID key) {
83 82 getCrudRepository().delete(key);
84 83 log.debug("Remove request: {}", key);
... ...
... ... @@ -73,7 +73,6 @@ public class JpaAlarmDao extends JpaAbstractDao<AlarmEntity, Alarm> implements A
73 73 }
74 74
75 75 @Override
76   - @Transactional(propagation = REQUIRES_NEW)
77 76 public ListenableFuture<Alarm> findLatestByOriginatorAndType(TenantId tenantId, EntityId originator, String type) {
78 77 return service.submit(() -> {
79 78 List<AlarmEntity> latest = alarmRepository.findLatestByOriginatorAndType(
... ...
... ... @@ -80,7 +80,5 @@ public interface DeviceRepository extends CrudRepository<DeviceEntity, UUID> {
80 80
81 81 List<DeviceEntity> findDevicesByTenantIdAndCustomerIdAndIdIn(UUID tenantId, UUID customerId, List<UUID> deviceIds);
82 82
83   - List<DeviceEntity> findDevicesByTenantId(UUID tenantId);
84   -
85 83 List<DeviceEntity> findDevicesByTenantIdAndIdIn(UUID tenantId, List<UUID> deviceIds);
86 84 }
... ...
... ... @@ -30,12 +30,12 @@ import org.thingsboard.server.common.data.page.TimePageLink;
30 30 import org.thingsboard.server.common.data.relation.EntityRelation;
31 31 import org.thingsboard.server.common.data.relation.RelationTypeGroup;
32 32 import org.thingsboard.server.dao.DaoUtil;
33   -import org.thingsboard.server.dao.util.SqlDao;
34 33 import org.thingsboard.server.dao.model.sql.RelationCompositeKey;
35 34 import org.thingsboard.server.dao.model.sql.RelationEntity;
36 35 import org.thingsboard.server.dao.relation.RelationDao;
37 36 import org.thingsboard.server.dao.sql.JpaAbstractDaoListeningExecutorService;
38 37 import org.thingsboard.server.dao.sql.JpaAbstractSearchTimeDao;
  38 +import org.thingsboard.server.dao.util.SqlDao;
39 39
40 40 import javax.persistence.criteria.CriteriaBuilder;
41 41 import javax.persistence.criteria.CriteriaQuery;
... ...
... ... @@ -17,11 +17,11 @@ package org.thingsboard.server.dao.sql.relation;
17 17
18 18 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
19 19 import org.springframework.data.repository.CrudRepository;
20   -import org.thingsboard.server.dao.util.SqlDao;
  20 +import org.springframework.transaction.annotation.Transactional;
21 21 import org.thingsboard.server.dao.model.sql.RelationCompositeKey;
22 22 import org.thingsboard.server.dao.model.sql.RelationEntity;
  23 +import org.thingsboard.server.dao.util.SqlDao;
23 24
24   -import javax.transaction.Transactional;
25 25 import java.util.List;
26 26 import java.util.UUID;
27 27
... ... @@ -51,5 +51,5 @@ public interface RelationRepository
51 51 String fromType);
52 52
53 53 @Transactional
54   - List<RelationEntity> deleteByFromIdAndFromType(UUID fromId, String fromType);
  54 + void deleteByFromIdAndFromType(UUID fromId, String fromType);
55 55 }
... ...
... ... @@ -17,6 +17,6 @@ package org.thingsboard.server.dao.util;
17 17
18 18 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
19 19
20   -@ConditionalOnProperty(prefix = "database", value = "type", havingValue = "postgres")
  20 +@ConditionalOnProperty(prefix = "database", value = "type", havingValue = "sql")
21 21 public @interface SqlDao {
22 22 }
... ...
dao/src/main/resources/sql/demo-data.sql renamed from dao/src/main/resources/postgres/demo-data.sql
dao/src/main/resources/sql/schema.sql renamed from dao/src/main/resources/postgres/schema.sql
... ... @@ -17,7 +17,7 @@
17 17
18 18 CREATE TABLE IF NOT EXISTS admin_settings (
19 19 id uuid NOT NULL CONSTRAINT admin_settings_pkey PRIMARY KEY,
20   - json_value text,
  20 + json_value varchar,
21 21 key character varying(255)
22 22 );
23 23
... ... @@ -25,7 +25,7 @@ CREATE TABLE IF NOT EXISTS alarm (
25 25 id uuid NOT NULL CONSTRAINT alarm_pkey PRIMARY KEY,
26 26 ack_ts bigint,
27 27 clear_ts bigint,
28   - additional_info text,
  28 + additional_info varchar,
29 29 end_ts bigint,
30 30 originator_id uuid,
31 31 originator_type integer,
... ... @@ -39,7 +39,7 @@ CREATE TABLE IF NOT EXISTS alarm (
39 39
40 40 CREATE TABLE IF NOT EXISTS asset (
41 41 id uuid NOT NULL CONSTRAINT asset_pkey PRIMARY KEY,
42   - additional_info text,
  42 + additional_info varchar,
43 43 customer_id uuid,
44 44 name character varying(255),
45 45 search_text character varying(255),
... ... @@ -64,7 +64,7 @@ CREATE TABLE IF NOT EXISTS component_descriptor (
64 64 id uuid NOT NULL CONSTRAINT component_descriptor_pkey PRIMARY KEY,
65 65 actions character varying(255),
66 66 clazz character varying(255),
67   - configuration_descriptor text,
  67 + configuration_descriptor varchar,
68 68 name character varying(255),
69 69 scope character varying(255),
70 70 search_text character varying(255),
... ... @@ -73,7 +73,7 @@ CREATE TABLE IF NOT EXISTS component_descriptor (
73 73
74 74 CREATE TABLE IF NOT EXISTS customer (
75 75 id uuid NOT NULL CONSTRAINT customer_pkey PRIMARY KEY,
76   - additional_info text,
  76 + additional_info varchar,
77 77 address character varying(255),
78 78 address2 character varying(255),
79 79 city character varying(255),
... ... @@ -89,7 +89,7 @@ CREATE TABLE IF NOT EXISTS customer (
89 89
90 90 CREATE TABLE IF NOT EXISTS dashboard (
91 91 id uuid NOT NULL CONSTRAINT dashboard_pkey PRIMARY KEY,
92   - configuration text,
  92 + configuration varchar,
93 93 customer_id uuid,
94 94 search_text character varying(255),
95 95 tenant_id uuid,
... ... @@ -98,7 +98,7 @@ CREATE TABLE IF NOT EXISTS dashboard (
98 98
99 99 CREATE TABLE IF NOT EXISTS device (
100 100 id uuid NOT NULL CONSTRAINT device_pkey PRIMARY KEY,
101   - additional_info text,
  101 + additional_info varchar,
102 102 customer_id uuid,
103 103 type character varying(255),
104 104 name character varying(255),
... ... @@ -116,7 +116,7 @@ CREATE TABLE IF NOT EXISTS device_credentials (
116 116
117 117 CREATE TABLE IF NOT EXISTS event (
118 118 id uuid NOT NULL CONSTRAINT event_pkey PRIMARY KEY,
119   - body text,
  119 + body varchar,
120 120 entity_id uuid,
121 121 entity_type character varying(255),
122 122 event_type character varying(255),
... ... @@ -127,10 +127,10 @@ CREATE TABLE IF NOT EXISTS event (
127 127
128 128 CREATE TABLE IF NOT EXISTS plugin (
129 129 id uuid NOT NULL CONSTRAINT plugin_pkey PRIMARY KEY,
130   - additional_info text,
  130 + additional_info varchar,
131 131 api_token character varying(255),
132 132 plugin_class character varying(255),
133   - configuration text,
  133 + configuration varchar,
134 134 name character varying(255),
135 135 public_access boolean,
136 136 search_text character varying(255),
... ... @@ -145,18 +145,18 @@ CREATE TABLE IF NOT EXISTS relation (
145 145 to_type character varying(255),
146 146 relation_type_group character varying(255),
147 147 relation_type character varying(255),
148   - additional_info text,
  148 + additional_info varchar,
149 149 CONSTRAINT relation_unq_key UNIQUE (from_id, from_type, relation_type_group, relation_type, to_id, to_type)
150 150 );
151 151
152 152 CREATE TABLE IF NOT EXISTS rule (
153 153 id uuid NOT NULL CONSTRAINT rule_pkey PRIMARY KEY,
154   - action text,
155   - additional_info text,
156   - filters text,
  154 + action varchar,
  155 + additional_info varchar,
  156 + filters varchar,
157 157 name character varying(255),
158 158 plugin_token character varying(255),
159   - processor text,
  159 + processor varchar,
160 160 search_text character varying(255),
161 161 state character varying(255),
162 162 tenant_id uuid,
... ... @@ -165,7 +165,7 @@ CREATE TABLE IF NOT EXISTS rule (
165 165
166 166 CREATE TABLE IF NOT EXISTS tb_user (
167 167 id uuid NOT NULL CONSTRAINT tb_user_pkey PRIMARY KEY,
168   - additional_info text,
  168 + additional_info varchar,
169 169 authority character varying(255),
170 170 customer_id uuid,
171 171 email character varying(255) UNIQUE,
... ... @@ -177,7 +177,7 @@ CREATE TABLE IF NOT EXISTS tb_user (
177 177
178 178 CREATE TABLE IF NOT EXISTS tenant (
179 179 id uuid NOT NULL CONSTRAINT tenant_pkey PRIMARY KEY,
180   - additional_info text,
  180 + additional_info varchar,
181 181 address character varying(255),
182 182 address2 character varying(255),
183 183 city character varying(255),
... ... @@ -228,7 +228,7 @@ CREATE TABLE IF NOT EXISTS widget_type (
228 228 id uuid NOT NULL CONSTRAINT widget_type_pkey PRIMARY KEY,
229 229 alias character varying(255),
230 230 bundle_alias character varying(255),
231   - descriptor text,
  231 + descriptor varchar(2000000),
232 232 name character varying(255),
233 233 tenant_id uuid
234 234 );
... ... @@ -236,7 +236,6 @@ CREATE TABLE IF NOT EXISTS widget_type (
236 236 CREATE TABLE IF NOT EXISTS widgets_bundle (
237 237 id uuid NOT NULL CONSTRAINT widgets_bundle_pkey PRIMARY KEY,
238 238 alias character varying(255),
239   - image bytea,
240 239 search_text character varying(255),
241 240 tenant_id uuid,
242 241 title character varying(255)
... ...
dao/src/main/resources/sql/system-data.sql renamed from dao/src/main/resources/postgres/system-data.sql
dao/src/test/java/org/thingsboard/server/dao/CustomSqlUnit.java renamed from dao/src/test/java/org/thingsboard/server/dao/CustomPostgresUnit.java
... ... @@ -19,7 +19,6 @@ import com.google.common.base.Charsets;
19 19 import com.google.common.io.Resources;
20 20 import lombok.extern.slf4j.Slf4j;
21 21 import org.junit.rules.ExternalResource;
22   -import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres;
23 22
24 23 import java.io.IOException;
25 24 import java.io.InputStream;
... ... @@ -30,58 +29,58 @@ import java.sql.SQLException;
30 29 import java.util.List;
31 30 import java.util.Properties;
32 31
33   -import static ru.yandex.qatools.embed.postgresql.distribution.Version.Main.V9_6;
34 32
35 33 /**
36 34 * Created by Valerii Sosliuk on 6/24/2017.
37 35 */
38 36 @Slf4j
39   -public class CustomPostgresUnit extends ExternalResource {
40   -
41   - private static final String HOST = "host";
42   - private static final String PORT = "port";
43   - private static final String DATABASE = "database";
44   - private static final String USERNAME = "username";
45   - private static final String PASSWORD = "password";
  37 +public class CustomSqlUnit extends ExternalResource {
46 38
47 39 private List<String> sqlFiles;
48 40 private Properties properties;
  41 + private String dropAllTablesSqlFile;
49 42
50   - private EmbeddedPostgres postgres;
51   -
52   - public CustomPostgresUnit(List<String> sqlFiles, String configurationFileName) {
  43 + public CustomSqlUnit(List<String> sqlFiles, String configurationFileName, String dropAllTablesSqlFile) {
53 44 this.sqlFiles = sqlFiles;
54 45 this.properties = loadProperties(configurationFileName);
  46 + this.dropAllTablesSqlFile = dropAllTablesSqlFile;
55 47 }
56 48
57 49 @Override
58 50 public void before() {
59   - postgres = new EmbeddedPostgres(V9_6);
60   - load();
61   - }
62   -
63   - @Override
64   - public void after() {
65   - postgres.stop();
66   - }
67   -
68   - private void load() {
69 51 Connection conn = null;
70 52 try {
71   - String url = postgres.start(properties.getProperty(HOST),
72   - Integer.parseInt(properties.getProperty(PORT)),
73   - properties.getProperty(DATABASE),
74   - properties.getProperty(USERNAME),
75   - properties.getProperty(PASSWORD));
76   -
77   - conn = DriverManager.getConnection(url);
  53 + String url = properties.getProperty("spring.datasource.url");
  54 + conn = DriverManager.getConnection(url, "sa", "");
78 55 for (String sqlFile : sqlFiles) {
79 56 URL sqlFileUrl = Resources.getResource(sqlFile);
80 57 String sql = Resources.toString(sqlFileUrl, Charsets.UTF_8);
81 58 conn.createStatement().execute(sql);
82 59 }
83 60 } catch (IOException | SQLException e) {
84   - throw new RuntimeException("Unable to start embedded postgres. Reason: " + e.getMessage(), e);
  61 + throw new RuntimeException("Unable to start embedded hsqldb. Reason: " + e.getMessage(), e);
  62 + } finally {
  63 + if (conn != null) {
  64 + try {
  65 + conn.close();
  66 + } catch (SQLException e) {
  67 + log.error(e.getMessage(), e);
  68 + }
  69 + }
  70 + }
  71 + }
  72 +
  73 + @Override
  74 + public void after() {
  75 + Connection conn = null;
  76 + try {
  77 + String url = properties.getProperty("spring.datasource.url");
  78 + conn = DriverManager.getConnection(url, "sa", "");
  79 + URL dropAllTableSqlFileUrl = Resources.getResource(dropAllTablesSqlFile);
  80 + String dropAllTablesSql = Resources.toString(dropAllTableSqlFileUrl, Charsets.UTF_8);
  81 + conn.createStatement().execute(dropAllTablesSql);
  82 + } catch (IOException | SQLException e) {
  83 + throw new RuntimeException("Unable to clean up embedded hsqldb. Reason: " + e.getMessage(), e);
85 84 } finally {
86 85 if (conn != null) {
87 86 try {
... ...
... ... @@ -24,13 +24,14 @@ import java.util.Arrays;
24 24
25 25 @RunWith(ClasspathSuite.class)
26 26 @ClassnameFilters({
27   - "org.thingsboard.server.dao.sql.*Test"
  27 + "org.thingsboard.server.dao.sql.*ABTest"
28 28 })
29 29 public class JpaDaoTestSuite {
30 30
31 31 @ClassRule
32   - public static CustomPostgresUnit postgresUnit = new CustomPostgresUnit(
33   - Arrays.asList("postgres/schema.sql", "postgres/system-data.sql"),
34   - "postgres-embedded-test.properties");
  32 + public static CustomSqlUnit sqlUnit = new CustomSqlUnit(
  33 + Arrays.asList("sql/schema.sql", "sql/system-data.sql"),
  34 + "sql-test.properties",
  35 + "sql/drop-all-tables.sql");
35 36
36 37 }
... ...
... ... @@ -18,6 +18,7 @@ package org.thingsboard.server.dao;
18 18 import com.github.springtestdbunit.bean.DatabaseConfigBean;
19 19 import com.github.springtestdbunit.bean.DatabaseDataSourceConnectionFactoryBean;
20 20 import org.dbunit.DatabaseUnitException;
  21 +import org.dbunit.ext.hsqldb.HsqldbDataTypeFactory;
21 22 import org.dbunit.ext.postgresql.PostgresqlDataTypeFactory;
22 23 import org.springframework.beans.factory.annotation.Autowired;
23 24 import org.springframework.context.annotation.Bean;
... ... @@ -41,7 +42,7 @@ public class JpaDbunitTestConfig {
41 42 @Bean
42 43 public DatabaseConfigBean databaseConfigBean() {
43 44 DatabaseConfigBean databaseConfigBean = new DatabaseConfigBean();
44   - databaseConfigBean.setDatatypeFactory(new PostgresqlDataTypeFactory());
  45 + databaseConfigBean.setDatatypeFactory(new HsqldbDataTypeFactory());
45 46 return databaseConfigBean;
46 47 }
47 48
... ...
... ... @@ -29,8 +29,9 @@ import java.util.Arrays;
29 29 public class SqlDaoServiceTestSuite {
30 30
31 31 @ClassRule
32   - public static CustomPostgresUnit postgresUnit = new CustomPostgresUnit(
33   - Arrays.asList("postgres/schema.sql", "postgres/system-data.sql", "postgres/system-test.sql"),
34   - "postgres-embedded-test.properties");
  32 + public static CustomSqlUnit sqlUnit = new CustomSqlUnit(
  33 + Arrays.asList("sql/schema.sql", "sql/system-data.sql", "sql/system-test.sql"),
  34 + "sql-test.properties",
  35 + "sql/drop-all-tables.sql");
35 36
36 37 }
... ...
1   -host: localhost
2   -port: 5433
3   -database: thingsboard-test
4   -username: postgres
5   -password: postgres
\ No newline at end of file
1   -database.type=postgres
  1 +database.type=sql
2 2
3 3 spring.jpa.show-sql=false
4 4 spring.jpa.hibernate.ddl-auto=validate
5 5
6   -spring.datasource.url=jdbc:postgresql://localhost:5433/thingsboard-test
7   -spring.datasource.username=postgres
8   -spring.datasource.password=postgres
  6 +spring.datasource.username=sa
  7 +spring.datasource.password=
  8 +spring.datasource.url=jdbc:hsqldb:mem:thingsboardTestDb;sql.enforce_size=false
... ...
  1 +TRUNCATE TABLE admin_settings;
  2 +TRUNCATE TABLE alarm;
  3 +TRUNCATE TABLE asset;
  4 +TRUNCATE TABLE attribute_kv;
  5 +TRUNCATE TABLE component_descriptor;
  6 +TRUNCATE TABLE customer;
  7 +TRUNCATE TABLE dashboard;
  8 +TRUNCATE TABLE device;
  9 +TRUNCATE TABLE device_credentials;
  10 +TRUNCATE TABLE event;
  11 +TRUNCATE TABLE plugin;
  12 +TRUNCATE TABLE relation;
  13 +TRUNCATE TABLE rule;
  14 +TRUNCATE TABLE tb_user;
  15 +TRUNCATE TABLE tenant;
  16 +TRUNCATE TABLE ts_kv;
  17 +TRUNCATE TABLE ts_kv_latest;
  18 +TRUNCATE TABLE user_credentials;
  19 +TRUNCATE TABLE widget_type;
  20 +TRUNCATE TABLE widgets_bundle;
\ No newline at end of file
... ...
dao/src/test/resources/sql/system-test.sql renamed from dao/src/test/resources/postgres/system-test.sql
... ... @@ -71,6 +71,7 @@
71 71 <springfox-swagger.version>2.6.1</springfox-swagger.version>
72 72 <bouncycastle.version>1.56</bouncycastle.version>
73 73 <winsw.version>2.0.1</winsw.version>
  74 + <hsqldb.version>2.4.0</hsqldb.version>
74 75 <dbunit.version>2.5.3</dbunit.version>
75 76 <spring-test-dbunit.version>1.2.1</spring-test-dbunit.version>
76 77 <postgresql.driver.version>9.4.1211</postgresql.driver.version>
... ... @@ -747,6 +748,12 @@
747 748 <version>${bouncycastle.version}</version>
748 749 </dependency>
749 750 <dependency>
  751 + <groupId>org.hsqldb</groupId>
  752 + <artifactId>hsqldb</artifactId>
  753 + <version>${hsqldb.version}</version>
  754 + <scope>test</scope>
  755 + </dependency>
  756 + <dependency>
750 757 <groupId>ru.yandex.qatools.embed</groupId>
751 758 <artifactId>postgresql-embedded</artifactId>
752 759 <version>2.2</version>
... ...