Using getTables to clear database

master
Markus Kreth 7 years ago
parent b18db77e4d
commit 7f3e1653c0
  1. 54
      src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/TestDatabaseHelperImpl.java
  2. 21
      src/test/resources/truncateTables.sql

@ -2,10 +2,10 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.dao;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDate;
import java.time.ZoneId;
@ -17,9 +17,7 @@ import java.util.function.Consumer;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import org.apache.commons.io.IOUtils;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -30,28 +28,46 @@ import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubeventHasPerson;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person;
@Component
public final class TestDatabaseHelperImpl implements TestDatabaseHelper {
public class TestDatabaseHelperImpl implements TestDatabaseHelper {
private static final AtomicInteger eventIdSequence = new AtomicInteger();
private final String truncateString = createTruncateString();
@Autowired
protected EntityManager entityManager;
private String createTruncateString() {
try {
InputStream resource = getClass().getResourceAsStream("/truncateTables.sql");
return IOUtils.toString(resource, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void cleanDatabase() {
Query truncateQuery = entityManager.createNativeQuery(truncateString);
transactional(() -> truncateQuery.executeUpdate());
Session session = (Session) entityManager;
session.doWork(conn -> {
List<String> tableNames = loadTables(conn);
try (Statement stm = conn.createStatement()) {
stm.addBatch("SET FOREIGN_KEY_CHECKS=0");
for (String table : tableNames) {
stm.addBatch("TRUNCATE TABLE " + table);
}
stm.addBatch("SET FOREIGN_KEY_CHECKS=1");
stm.executeBatch();
}
if (conn.getAutoCommit() == false) {
conn.commit();
}
});
}
public List<String> loadTables(Connection conn) throws SQLException {
List<String> tableNames = new ArrayList<>();
DatabaseMetaData meta = conn.getMetaData();
String[] types = { "TABLE" };
try (ResultSet tables = meta.getTables(null, null, null, types)) {
while (tables.next()) {
tableNames.add(tables.getString("TABLE_NAME"));
}
}
return tableNames;
}
/**

@ -1,21 +0,0 @@
SET FOREIGN_KEY_CHECKS=0;
-- TRUNCATE TABLE clubevent_addon;
-- TRUNCATE TABLE event_has_altersgruppe;
TRUNCATE TABLE altersgruppe;
TRUNCATE TABLE pflichten;
TRUNCATE TABLE clubevent_has_person;
TRUNCATE TABLE ClubEvent;
-- TRUNCATE TABLE persongroup;
TRUNCATE TABLE startpass_startrechte;
TRUNCATE TABLE deleted_entries;
TRUNCATE TABLE clubevent;
TRUNCATE TABLE relative;
TRUNCATE TABLE groupdef;
TRUNCATE TABLE startpaesse;
TRUNCATE TABLE contact;
TRUNCATE TABLE adress;
TRUNCATE TABLE attendance;
TRUNCATE TABLE person;
SET FOREIGN_KEY_CHECKS=1;
Loading…
Cancel
Save