From c4f155bf9d5bb1831003bcac82461582d86cdb46 Mon Sep 17 00:00:00 2001 From: Julien Herr Date: Sun, 16 Jul 2023 19:48:47 +0200 Subject: [PATCH 1/3] chore: Use testcontainers maven plugin for the generation of jooq code --- pom.xml | 239 ++++++++++++++++++++++++++------------------------------ 1 file changed, 111 insertions(+), 128 deletions(-) diff --git a/pom.xml b/pom.xml index a6b1cfe1d..211bd740b 100644 --- a/pom.xml +++ b/pom.xml @@ -43,10 +43,15 @@ 10.0.14 1.18.28 2.15.2 + 1.18.3 + 4.2 + 0.0.2 - - jdbc:mysql://${db.ip}:${db.port}/${db.schema}?useSSL=true&serverTimezone=UTC + steve + changeme + stevedb + com.mysql.cj.jdbc.Driver @@ -319,141 +324,119 @@ - org.flywaydb - flyway-maven-plugin - 7.15.0 - - - - - generate-sources - - migrate - - - - - - - SET default_storage_engine=InnoDB; - - true - - - schema_version
- - true - com.mysql.cj.jdbc.Driver - ${jdbcUrl} - ${db.user} - ${db.password} - - ${db.schema} - - - filesystem:src/main/resources/db/migration - -
-
- - - - org.jooq - jooq-codegen-maven - ${jooq.version} - - - - - generate-sources - - generate - - - - - + org.testcontainers + testcontainers-jooq-codegen-maven-plugin + ${plugin.testcontainers-jooq-codegen.version} - org.jooq - jooq-meta - ${jooq.version} - - - org.jooq - jooq-codegen - ${jooq.version} + org.testcontainers + mysql + ${testcontainers.version} mysql mysql-connector-java ${mysql.jdbc.version} + + org.jooq + jooq-codegen + ${jooq.version} + - - - - - com.mysql.cj.jdbc.Driver - ${jdbcUrl} - ${db.user} - ${db.password} - - - - - org.jooq.meta.mysql.MySQLDatabase - .* - - ${db.schema} - false - - - - BOOLEAN - - (?i:(TINY|SMALL|MEDIUM|BIG)?INT(UNSIGNED)?\(1\)) - - - BOOLEAN - .*\.OCPP_TAG_ACTIVITY\.(IN_TRANSACTION|BLOCKED) - - - org.joda.time.DateTime - de.rwth.idsg.steve.utils.DateTimeConverter - .* - .*(TIMESTAMP|DATETIME).* - - - org.joda.time.LocalDate - de.rwth.idsg.steve.utils.DateConverter - .*\.BIRTH_DAY - .*(DATE).* - - - - - true - - false - - - - jooq.steve.db - ${project.build.directory}/generated-sources - - - + + + generate-jooq-sources + + generate + + generate-sources + + + MYSQL + mysql:8.0 + root + ${db.password} + ${db.schema} + + + + SET default_storage_engine=InnoDB; + + true + + + schema_version
+ + true + ${db.driver} + + filesystem:src/main/resources/db/migration + +
+ + + + ${db.driver} + + + + + org.jooq.meta.mysql.MySQLDatabase + .* + flyway_schema_history + ${db.schema} + false + + + + BOOLEAN + + (?i:(TINY|SMALL|MEDIUM|BIG)?INT(UNSIGNED)?\(1\)) + + + BOOLEAN + .*\.OCPP_TAG_ACTIVITY\.(IN_TRANSACTION|BLOCKED) + + + org.joda.time.DateTime + de.rwth.idsg.steve.utils.DateTimeConverter + .* + .*(TIMESTAMP|DATETIME).* + + + org.joda.time.LocalDate + de.rwth.idsg.steve.utils.DateConverter + .*\.BIRTH_DAY + .*(DATE).* + + + + + true + + false + + + + jooq.steve.db + ${project.build.directory}/generated-sources + + + +
+
+
From 175aa6c651ae881976a7efd60278093dfc5211f9 Mon Sep 17 00:00:00 2001 From: Julien Herr Date: Wed, 26 Jul 2023 18:15:21 +0200 Subject: [PATCH 2/3] Upgrade database schema during preparation for tests Based on d5a661782d926a1c7cf4f4b3c02304ac378ac0e6 --- .github/workflows/main.yml | 29 +++++++--- pom.xml | 33 +++++------ .../rwth/idsg/steve/SteveConfiguration.java | 4 ++ .../idsg/steve/config/BeanConfiguration.java | 2 +- src/main/resources/config/dev/main.properties | 6 +- .../resources/config/test/main.properties | 6 +- .../steve/utils/FlywayMigrationRunner.java | 58 +++++++++++++++++++ .../steve/utils/__DatabasePreparer__.java | 5 +- 8 files changed, 106 insertions(+), 37 deletions(-) create mode 100644 src/test/java/de/rwth/idsg/steve/utils/FlywayMigrationRunner.java diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 1bd4610c0..ecc5391a0 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -3,6 +3,10 @@ on: [ push, pull_request ] jobs: build: + env: + DB_USER: steve + DB_PASSWORD: changeme + DB_SCHEMA: stevedb_test_2aa6a783d47d strategy: fail-fast: false matrix: @@ -22,24 +26,31 @@ jobs: steps: - uses: actions/checkout@v3 - - name: Set up Java ${{ matrix.Java }} + - name: Set up Java 17 for build uses: actions/setup-java@v3 with: - java-version: ${{ matrix.java }} + java-version: 17 distribution: 'temurin' cache: maven - - name: Set up MySQL + - name: Set up MySQL for tests run: | mysql -h 127.0.0.1 -P 3306 -uroot -proot -e "SELECT @@VERSION;" - mysql -h 127.0.0.1 -P 3306 -uroot -proot -e "CREATE DATABASE stevedb_test_2aa6a783d47d;" -v - mysql -h 127.0.0.1 -P 3306 -uroot -proot -e "CREATE USER 'steve'@'%' IDENTIFIED BY 'changeme';" -v - mysql -h 127.0.0.1 -P 3306 -uroot -proot -e "GRANT ALL PRIVILEGES ON stevedb_test_2aa6a783d47d.* TO 'steve'@'%';" -v - mysql -h 127.0.0.1 -P 3306 -uroot -proot -e "GRANT SELECT ON mysql.proc TO 'steve'@'%';" -v || true - mysql -h 127.0.0.1 -P 3306 -uroot -proot -e "GRANT SUPER ON *.* TO 'steve'@'%';" -v || true + mysql -h 127.0.0.1 -P 3306 -uroot -proot -e "CREATE DATABASE $DB_SCHEMA;" -v + mysql -h 127.0.0.1 -P 3306 -uroot -proot -e "CREATE USER '$DB_USER'@'%' IDENTIFIED BY '$DB_PASSWORD';" -v + mysql -h 127.0.0.1 -P 3306 -uroot -proot -e "GRANT ALL PRIVILEGES ON $DB_SCHEMA.* TO '$DB_USER'@'%';" -v + mysql -h 127.0.0.1 -P 3306 -uroot -proot -e "GRANT SELECT ON mysql.proc TO '$DB_USER'@'%';" -v || true + mysql -h 127.0.0.1 -P 3306 -uroot -proot -e "GRANT SUPER ON *.* TO '$DB_USER'@'%';" -v || true - name: Build with Maven - run: ./mvnw -B -V -Dmaven.javadoc.skip=true -Ptest clean package --file pom.xml + run: ./mvnw -B -V -Dmaven.javadoc.skip=true -Ddb.user=$DB_USER -Ddb.password=$DB_PASSWORD -Ddb.schema=$DB_SCHEMA -Ptest clean package --file pom.xml + + - name: Set up Java ${{ matrix.Java }} for run + uses: actions/setup-java@v2 + with: + java-version: ${{ matrix.java }} + distribution: 'temurin' + cache: maven - name: Start the app and visit signin web page run: | diff --git a/pom.xml b/pom.xml index 211bd740b..7e3f3e44e 100644 --- a/pom.xml +++ b/pom.xml @@ -43,6 +43,8 @@ 10.0.14 1.18.28 2.15.2 + 7.15.0 + 5.9.3 1.18.3 4.2 @@ -193,6 +195,11 @@ org.apache.maven.plugins maven-surefire-plugin 3.1.2 + + + ${db.schema} + + @@ -249,26 +256,6 @@ - - - org.codehaus.mojo - properties-maven-plugin - 1.2.0 - - - initialize - - read-project-properties - - - - src/main/resources/config/${envName}/main.properties - - - - - - org.apache.maven.plugins maven-dependency-plugin @@ -718,6 +705,12 @@ junit-jupiter-params test + + org.flywaydb + flyway-core + ${flyway.version} + test + org.mockito mockito-junit-jupiter diff --git a/src/main/java/de/rwth/idsg/steve/SteveConfiguration.java b/src/main/java/de/rwth/idsg/steve/SteveConfiguration.java index 7aeb2e911..ba0722c18 100644 --- a/src/main/java/de/rwth/idsg/steve/SteveConfiguration.java +++ b/src/main/java/de/rwth/idsg/steve/SteveConfiguration.java @@ -180,6 +180,10 @@ public static class DB { private final String userName; private final String password; private final boolean sqlLogging; + + public String getJdbcUrl() { + return "jdbc:mysql://" + ip + ":" + port + "/" + schema; + } } // Credentials for Web interface access diff --git a/src/main/java/de/rwth/idsg/steve/config/BeanConfiguration.java b/src/main/java/de/rwth/idsg/steve/config/BeanConfiguration.java index 234c2caad..dca3fbc47 100644 --- a/src/main/java/de/rwth/idsg/steve/config/BeanConfiguration.java +++ b/src/main/java/de/rwth/idsg/steve/config/BeanConfiguration.java @@ -94,7 +94,7 @@ private void initDataSource() { HikariConfig hc = new HikariConfig(); // set standard params - hc.setJdbcUrl("jdbc:mysql://" + dbConfig.getIp() + ":" + dbConfig.getPort() + "/" + dbConfig.getSchema()); + hc.setJdbcUrl(dbConfig.getJdbcUrl()); hc.setUsername(dbConfig.getUserName()); hc.setPassword(dbConfig.getPassword()); diff --git a/src/main/resources/config/dev/main.properties b/src/main/resources/config/dev/main.properties index 84afdf0f8..08e17db8a 100644 --- a/src/main/resources/config/dev/main.properties +++ b/src/main/resources/config/dev/main.properties @@ -8,9 +8,9 @@ context.path = steve # db.ip = localhost db.port = 3306 -db.schema = stevedb -db.user = steve -db.password = changeme +db.schema = ${db.schema} +db.user = ${db.user} +db.password = ${db.password} # Credentials for Web interface access # diff --git a/src/main/resources/config/test/main.properties b/src/main/resources/config/test/main.properties index 66c566de9..0d4a9f1a8 100644 --- a/src/main/resources/config/test/main.properties +++ b/src/main/resources/config/test/main.properties @@ -8,9 +8,9 @@ context.path = steve # db.ip = localhost db.port = 3306 -db.schema = stevedb_test_2aa6a783d47d -db.user = steve -db.password = changeme +db.schema = ${db.schema} +db.user = ${db.user} +db.password = ${db.password} # Credentials for Web interface access # diff --git a/src/test/java/de/rwth/idsg/steve/utils/FlywayMigrationRunner.java b/src/test/java/de/rwth/idsg/steve/utils/FlywayMigrationRunner.java new file mode 100644 index 000000000..9f8b6f3ee --- /dev/null +++ b/src/test/java/de/rwth/idsg/steve/utils/FlywayMigrationRunner.java @@ -0,0 +1,58 @@ +/* + * SteVe - SteckdosenVerwaltung - https://github.com/steve-community/steve + * Copyright (C) 2013-2023 SteVe Community Team + * All Rights Reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package de.rwth.idsg.steve.utils; + +import de.rwth.idsg.steve.SteveConfiguration; +import org.flywaydb.core.api.configuration.FluentConfiguration; + +/** + * @author Sevket Goekay + * @since 08.08.2021 + */ +public class FlywayMigrationRunner { + + private static final String INIT_SQL = "SET default_storage_engine=InnoDB;"; + private static final String BOOKKEEPING_TABLE = "schema_version"; + private static final String LOCATION_OF_MIGRATIONS = "filesystem:src/main/resources/db/migration"; + + public static void run(SteveConfiguration sc) { + try { + getConfig(sc.getDb()).load().migrate(); + } catch (RuntimeException e) { + throw e; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** + * This configuration should be kept in-sync with the one of the flyway-maven-plugin in pom.xml + */ + private static FluentConfiguration getConfig(SteveConfiguration.DB dbConfig) { + return new FluentConfiguration() + .initSql(INIT_SQL) + .outOfOrder(true) + .table(BOOKKEEPING_TABLE) + .cleanDisabled(true) + .schemas(dbConfig.getSchema()) + .defaultSchema(dbConfig.getSchema()) + .locations(LOCATION_OF_MIGRATIONS) + .dataSource(dbConfig.getJdbcUrl(), dbConfig.getUserName(), dbConfig.getPassword()); + } +} diff --git a/src/test/java/de/rwth/idsg/steve/utils/__DatabasePreparer__.java b/src/test/java/de/rwth/idsg/steve/utils/__DatabasePreparer__.java index 0d5650812..dc9495319 100644 --- a/src/test/java/de/rwth/idsg/steve/utils/__DatabasePreparer__.java +++ b/src/test/java/de/rwth/idsg/steve/utils/__DatabasePreparer__.java @@ -19,6 +19,7 @@ package de.rwth.idsg.steve.utils; import com.google.common.collect.Sets; +import de.rwth.idsg.steve.SteveConfiguration; import de.rwth.idsg.steve.config.BeanConfiguration; import de.rwth.idsg.steve.repository.dto.ChargePoint; import de.rwth.idsg.steve.repository.dto.ConnectorStatus; @@ -66,7 +67,7 @@ */ public class __DatabasePreparer__ { - private static final String SCHEMA_TO_TRUNCATE = "stevedb_test_2aa6a783d47d"; + private static final String SCHEMA_TO_TRUNCATE = System.getProperty("schemaToTruncate", "stevedb_test_2aa6a783d47d"); private static final String REGISTERED_CHARGE_BOX_ID = "charge_box_2aa6a783d47d"; private static final String REGISTERED_CHARGE_BOX_ID_2 = "charge_box_2aa6a783d47d_2"; private static final String REGISTERED_OCPP_TAG = "id_tag_2aa6a783d47d"; @@ -75,6 +76,8 @@ public class __DatabasePreparer__ { private static final DSLContext dslContext = beanConfiguration.dslContext(); public static void prepare() { + SteveConfiguration sc = SteveConfiguration.CONFIG; + FlywayMigrationRunner.run(sc); runOperation(ctx -> { truncateTables(ctx); insertChargeBox(ctx); From 3a9837ee56b560785d253940e228da5937193e25 Mon Sep 17 00:00:00 2001 From: Julien Herr Date: Mon, 11 Sep 2023 14:37:37 +0200 Subject: [PATCH 3/3] feat: use migration from classpath --- pom.xml | 7 +++++++ .../de/rwth/idsg/steve/utils/FlywayMigrationRunner.java | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 7e3f3e44e..36e98398f 100644 --- a/pom.xml +++ b/pom.xml @@ -112,6 +112,13 @@ webapp/WEB-INF/web.xml + + src/main/resources + false + + db/migration/** + + diff --git a/src/test/java/de/rwth/idsg/steve/utils/FlywayMigrationRunner.java b/src/test/java/de/rwth/idsg/steve/utils/FlywayMigrationRunner.java index 9f8b6f3ee..adf2ce395 100644 --- a/src/test/java/de/rwth/idsg/steve/utils/FlywayMigrationRunner.java +++ b/src/test/java/de/rwth/idsg/steve/utils/FlywayMigrationRunner.java @@ -29,7 +29,7 @@ public class FlywayMigrationRunner { private static final String INIT_SQL = "SET default_storage_engine=InnoDB;"; private static final String BOOKKEEPING_TABLE = "schema_version"; - private static final String LOCATION_OF_MIGRATIONS = "filesystem:src/main/resources/db/migration"; + private static final String LOCATION_OF_MIGRATIONS = "classpath:db/migration"; public static void run(SteveConfiguration sc) { try {