Skip to content

Commit

Permalink
Upgrade database schema during preparation for tests
Browse files Browse the repository at this point in the history
Based on d5a6617
  • Loading branch information
juherr committed Jul 28, 2023
1 parent 8b8e2ec commit 49b744c
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 39 deletions.
29 changes: 20 additions & 9 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -22,24 +26,31 @@ jobs:

steps:
- uses: actions/checkout@v2
- name: Set up Java ${{ matrix.Java }}
- name: Set up Java 17 for build
uses: actions/setup-java@v2
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: |
Expand Down
37 changes: 15 additions & 22 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
<jetty.version>10.0.14</jetty.version>
<lombok.version>1.18.28</lombok.version>
<jackson.version>2.15.2</jackson.version>
<flyway.version>7.15.0</flyway.version>
<junit.version>5.9.3</junit.version>
<testcontainers.version>1.18.3</testcontainers.version>

<plugin.license-maven.version>4.2</plugin.license-maven.version>
Expand Down Expand Up @@ -190,6 +192,11 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<systemPropertyVariables>
<schemaToTruncate>${db.schema}</schemaToTruncate>
</systemPropertyVariables>
</configuration>
</plugin>

<!-- Static analysis and check style -->
Expand Down Expand Up @@ -246,26 +253,6 @@
</configuration>
</plugin>

<!-- Read from main.properties the DB configuration -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.1.0</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>src/main/resources/config/${envName}/main.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
Expand Down Expand Up @@ -701,13 +688,19 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.3</version>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.9.3</version>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>${flyway.version}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/de/rwth/idsg/steve/SteveConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/config/dev/main.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/config/test/main.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
58 changes: 58 additions & 0 deletions src/test/java/de/rwth/idsg/steve/utils/FlywayMigrationRunner.java
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/
package de.rwth.idsg.steve.utils;

import de.rwth.idsg.steve.SteveConfiguration;
import org.flywaydb.core.api.configuration.FluentConfiguration;

/**
* @author Sevket Goekay <[email protected]>
* @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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand All @@ -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);
Expand Down

0 comments on commit 49b744c

Please sign in to comment.