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 26, 2023
1 parent 8b8e2ec commit fde56e0
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 9 deletions.
12 changes: 10 additions & 2 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 @@ -701,13 +703,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
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 <sevketgokay@gmail.com>
* @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());
}
}
10 changes: 4 additions & 6 deletions src/test/java/de/rwth/idsg/steve/utils/__DatabasePreparer__.java
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,6 @@
*/
public class __DatabasePreparer__ {

private static final String SCHEMA_TO_TRUNCATE = "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 +75,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 Expand Up @@ -159,11 +161,7 @@ private static void truncateTables(DSLContext ctx) {
);

ctx.transaction(configuration -> {
Schema schema = DefaultCatalog.DEFAULT_CATALOG.getSchemas()
.stream()
.filter(s -> SCHEMA_TO_TRUNCATE.equals(s.getName()))
.findFirst()
.orElseThrow(() -> new RuntimeException("Could not find schema"));
Schema schema = DefaultCatalog.DEFAULT_CATALOG.STEVEDB;

List<Table<?>> tables = schema.getTables()
.stream()
Expand Down

0 comments on commit fde56e0

Please sign in to comment.