From 50422363f09273e23c49fd14f273aea8418ded1b Mon Sep 17 00:00:00 2001 From: Viacheslav Klimov Date: Mon, 28 Mar 2022 14:26:56 +0300 Subject: [PATCH 1/3] Fix install script for fk_device_profile_ota_package constraint to work on repeated install --- dao/src/main/resources/sql/schema-entities.sql | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/dao/src/main/resources/sql/schema-entities.sql b/dao/src/main/resources/sql/schema-entities.sql index 4e2596485b..2930ac7414 100644 --- a/dao/src/main/resources/sql/schema-entities.sql +++ b/dao/src/main/resources/sql/schema-entities.sql @@ -242,10 +242,17 @@ CREATE TABLE IF NOT EXISTS device_profile ( CONSTRAINT fk_software_device_profile FOREIGN KEY (software_id) REFERENCES ota_package(id) ); -ALTER TABLE ota_package - ADD CONSTRAINT fk_device_profile_ota_package - FOREIGN KEY (device_profile_id) REFERENCES device_profile (id) - ON DELETE CASCADE; +DO +$$ + BEGIN + IF NOT EXISTS(SELECT 1 FROM pg_constraint WHERE conname = 'fk_device_profile_ota_package') THEN + ALTER TABLE ota_package + ADD CONSTRAINT fk_device_profile_ota_package + FOREIGN KEY (device_profile_id) REFERENCES device_profile (id) + ON DELETE CASCADE; + END IF; + END; +$$; -- We will use one-to-many relation in the first release and extend this feature in case of user requests -- CREATE TABLE IF NOT EXISTS device_profile_firmware ( From 6182ae7415d3ae90d315e812626fbd1e384b476f Mon Sep 17 00:00:00 2001 From: Viacheslav Klimov Date: Mon, 28 Mar 2022 15:41:09 +0300 Subject: [PATCH 2/3] Add test for schema reinstall --- .../server/dao/SqlDaoServiceTestSuite.java | 1 + .../install/sql/EntitiesSchemaSqlTest.java | 83 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 dao/src/test/java/org/thingsboard/server/dao/service/install/sql/EntitiesSchemaSqlTest.java 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 bc7abf8342..12f77ece14 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/SqlDaoServiceTestSuite.java +++ b/dao/src/test/java/org/thingsboard/server/dao/SqlDaoServiceTestSuite.java @@ -25,6 +25,7 @@ import org.junit.runner.RunWith; "org.thingsboard.server.dao.service.event.sql.*SqlTest", "org.thingsboard.server.dao.service.sql.*SqlTest", "org.thingsboard.server.dao.service.timeseries.sql.*SqlTest", + "org.thingsboard.server.dao.service.install.sql.*SqlTest" }) public class SqlDaoServiceTestSuite { } diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/install/sql/EntitiesSchemaSqlTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/install/sql/EntitiesSchemaSqlTest.java new file mode 100644 index 0000000000..b8e57d4dd2 --- /dev/null +++ b/dao/src/test/java/org/thingsboard/server/dao/service/install/sql/EntitiesSchemaSqlTest.java @@ -0,0 +1,83 @@ +/** + * Copyright © 2016-2022 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.service.install.sql; + +import org.assertj.core.api.Assertions; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Value; +import org.thingsboard.server.dao.service.AbstractServiceTest; +import org.thingsboard.server.dao.service.DaoSqlTest; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +@DaoSqlTest +public class EntitiesSchemaSqlTest extends AbstractServiceTest { + + @Value("${spring.datasource.url}") + private String dbUrl; + @Value("${spring.datasource.username}") + private String dbUserName; + @Value("${spring.datasource.password}") + private String dbPassword; + + @Value("${classpath:sql/schema-entities.sql}") + private Path installScriptPath; + + @Test + public void testRepeatedInstall() throws IOException { + String installScript = Files.readString(installScriptPath); + try (Connection connection = getConnection()) { + for (int i = 1; i <= 2; i++) { + connection.createStatement().execute(installScript); + } + } catch (SQLException e) { + Assertions.fail("Failed to execute reinstall", e); + } + } + + @Test + public void testRepeatedInstall_badScript() throws SQLException { + String illegalInstallScript = "CREATE TABLE IF NOT EXISTS qwerty ();\n" + + "ALTER TABLE qwerty ADD COLUMN first VARCHAR(10);"; + + Connection connection = getConnection(); + try { + assertDoesNotThrow(() -> { + connection.createStatement().execute(illegalInstallScript); + }); + + assertThatThrownBy(() -> { + connection.createStatement().execute(illegalInstallScript); + }).hasMessageContaining("column").hasMessageContaining("already exists"); + } finally { + connection.createStatement().execute("DROP TABLE qwerty;"); + connection.close(); + } + } + + private Connection getConnection() throws SQLException { + return DriverManager.getConnection(dbUrl, dbUserName, dbPassword); + } + +} From b396aa4a3863dc240ccbb261d50d9e8fa97de445 Mon Sep 17 00:00:00 2001 From: Viacheslav Klimov Date: Mon, 28 Mar 2022 16:26:05 +0300 Subject: [PATCH 3/3] Use JdbcTemplate in EntitiesSchemaSqlTest --- .../install/sql/EntitiesSchemaSqlTest.java | 43 +++++++------------ 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/install/sql/EntitiesSchemaSqlTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/install/sql/EntitiesSchemaSqlTest.java index b8e57d4dd2..285ea5bb50 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/install/sql/EntitiesSchemaSqlTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/install/sql/EntitiesSchemaSqlTest.java @@ -17,16 +17,15 @@ package org.thingsboard.server.dao.service.install.sql; import org.assertj.core.api.Assertions; import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.jdbc.core.JdbcTemplate; import org.thingsboard.server.dao.service.AbstractServiceTest; import org.thingsboard.server.dao.service.DaoSqlTest; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; @@ -34,50 +33,40 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; @DaoSqlTest public class EntitiesSchemaSqlTest extends AbstractServiceTest { - @Value("${spring.datasource.url}") - private String dbUrl; - @Value("${spring.datasource.username}") - private String dbUserName; - @Value("${spring.datasource.password}") - private String dbPassword; - @Value("${classpath:sql/schema-entities.sql}") private Path installScriptPath; + @Autowired + private JdbcTemplate jdbcTemplate; + @Test public void testRepeatedInstall() throws IOException { String installScript = Files.readString(installScriptPath); - try (Connection connection = getConnection()) { + try { for (int i = 1; i <= 2; i++) { - connection.createStatement().execute(installScript); + jdbcTemplate.execute(installScript); } - } catch (SQLException e) { + } catch (Exception e) { Assertions.fail("Failed to execute reinstall", e); } } @Test - public void testRepeatedInstall_badScript() throws SQLException { + public void testRepeatedInstall_badScript() { String illegalInstallScript = "CREATE TABLE IF NOT EXISTS qwerty ();\n" + "ALTER TABLE qwerty ADD COLUMN first VARCHAR(10);"; - Connection connection = getConnection(); - try { - assertDoesNotThrow(() -> { - connection.createStatement().execute(illegalInstallScript); - }); + assertDoesNotThrow(() -> { + jdbcTemplate.execute(illegalInstallScript); + }); + try { assertThatThrownBy(() -> { - connection.createStatement().execute(illegalInstallScript); - }).hasMessageContaining("column").hasMessageContaining("already exists"); + jdbcTemplate.execute(illegalInstallScript); + }).getCause().hasMessageContaining("column").hasMessageContaining("already exists"); } finally { - connection.createStatement().execute("DROP TABLE qwerty;"); - connection.close(); + jdbcTemplate.execute("DROP TABLE qwerty;"); } } - private Connection getConnection() throws SQLException { - return DriverManager.getConnection(dbUrl, dbUserName, dbPassword); - } - }