Skip to content

Commit

Permalink
Issue #91: Extracted schema setup to separate method.
Browse files Browse the repository at this point in the history
  • Loading branch information
haumacher committed Dec 1, 2024
1 parent ffc8ae6 commit 81a28da
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions phoneblock/src/main/java/de/haumacher/phoneblock/db/DB.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,31 @@ public DB(SecureRandom rnd, boolean sendHelpMails, DataSource dataSource, IndexU
configuration.addMapper(Domains.class);
_sessionFactory = new SqlSessionFactoryBuilder().build(configuration);

setupSchema();

try {
_sha256 = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException ex) {
throw new RuntimeException("Digest algorithm not supported: " + ex.getMessage(), ex);
}

cleanup();

Date timeCleanup = schedule(20, this::cleanup);
LOG.info("Scheduled next DB cleanup: " + timeCleanup);

Date timeHistory = schedule(0, this::runUpdateHistory);
LOG.info("Scheduled search history cleanup: " + timeHistory);

if (sendHelpMails && _mailService != null) {
Date supportMails = schedule(18, this::sendSupportMails);
LOG.info("Scheduled support mails: " + supportMails);
} else {
LOG.info("Support mails are disabled.");
}
}

private void setupSchema() throws SQLException {
Set<String> tableNames = new HashSet<>();
try (SqlSession session = openSession()) {
Connection connection = session.getConnection();
Expand Down Expand Up @@ -284,27 +309,6 @@ else if (!tableNames.contains("NUMBERS")) {
connection.commit();
}
}

try {
_sha256 = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException ex) {
throw new RuntimeException("Digest algorithm not supported: " + ex.getMessage(), ex);
}

cleanup();

Date timeCleanup = schedule(20, this::cleanup);
LOG.info("Scheduled next DB cleanup: " + timeCleanup);

Date timeHistory = schedule(0, this::runUpdateHistory);
LOG.info("Scheduled search history cleanup: " + timeHistory);

if (sendHelpMails && _mailService != null) {
Date supportMails = schedule(18, this::sendSupportMails);
LOG.info("Scheduled support mails: " + supportMails);
} else {
LOG.info("Support mails are disabled.");
}
}

private Date schedule(int atHour, Runnable command) {
Expand Down

0 comments on commit 81a28da

Please sign in to comment.