|
|
@ -1,7 +1,6 @@ |
|
|
|
package org.ros.chatto.service; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.sql.Connection; |
|
|
|
import java.sql.PreparedStatement; |
|
|
|
import java.sql.ResultSet; |
|
|
@ -15,9 +14,6 @@ import org.hibernate.Session; |
|
|
|
import org.ros.chatto.config.DBInitializerConfig; |
|
|
|
import org.springframework.boot.context.event.ApplicationReadyEvent; |
|
|
|
import org.springframework.context.event.EventListener; |
|
|
|
import org.springframework.core.io.ClassPathResource; |
|
|
|
import org.springframework.core.io.support.EncodedResource; |
|
|
|
import org.springframework.jdbc.datasource.init.ScriptUtils; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
@ -33,16 +29,6 @@ public class DBInitializerService { |
|
|
|
@PersistenceContext |
|
|
|
private EntityManager entityManager; |
|
|
|
|
|
|
|
|
|
|
|
public int getNumTables(final Connection connection) throws SQLException { |
|
|
|
final PreparedStatement preparedStatement = connection.prepareStatement(dbInitializerConfig.getNumTablesQuery()); |
|
|
|
preparedStatement.setString(1, dbInitializerConfig.getDbName()); |
|
|
|
final ResultSet resultSet = preparedStatement.executeQuery(); |
|
|
|
resultSet.next(); |
|
|
|
final int numTables = resultSet.getInt("num_tables"); |
|
|
|
return numTables; |
|
|
|
} |
|
|
|
|
|
|
|
@EventListener(ApplicationReadyEvent.class) |
|
|
|
@Transactional |
|
|
|
public void doSomethingAfterStartup() throws SQLException, IOException { |
|
|
@ -50,16 +36,10 @@ public class DBInitializerService { |
|
|
|
|
|
|
|
final Session session = entityManager.unwrap(Session.class); |
|
|
|
|
|
|
|
// session.doWork(connection -> { |
|
|
|
// if (getNumTables(connection) == 0) { |
|
|
|
// log.info("Database is empty. Populating tables and roles"); |
|
|
|
// try { |
|
|
|
// populateDB(connection); |
|
|
|
// } catch (final IOException e) { |
|
|
|
// log.error("IO error", e); |
|
|
|
// } |
|
|
|
// } |
|
|
|
// }); |
|
|
|
session.doWork(connection -> { |
|
|
|
log.info("Clearing tokens"); |
|
|
|
clearAllTokens(connection); |
|
|
|
}); |
|
|
|
|
|
|
|
session.doWork(connection -> { |
|
|
|
log.info("Resetting all user sessions"); |
|
|
@ -67,16 +47,15 @@ public class DBInitializerService { |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
private void populateDB(final Connection connection) throws SQLException, IOException { |
|
|
|
ScriptUtils.executeSqlScript(connection, |
|
|
|
new EncodedResource(new ClassPathResource("scheme.sql"), StandardCharsets.UTF_8)); |
|
|
|
ScriptUtils.executeSqlScript(connection, |
|
|
|
new EncodedResource(new ClassPathResource("datae.sql"), StandardCharsets.UTF_8)); |
|
|
|
|
|
|
|
private void resetAllUserSessions(final Connection connection) throws SQLException { |
|
|
|
final PreparedStatement preparedStatement = connection |
|
|
|
.prepareStatement(dbInitializerConfig.getResetSessionsQuery()); |
|
|
|
preparedStatement.executeUpdate(); |
|
|
|
} |
|
|
|
|
|
|
|
private void resetAllUserSessions(final Connection connection) throws SQLException { |
|
|
|
final PreparedStatement preparedStatement = connection.prepareStatement(dbInitializerConfig.getResetSessionsQuery()); |
|
|
|
private void clearAllTokens(final Connection connection) throws SQLException { |
|
|
|
final PreparedStatement preparedStatement = connection |
|
|
|
.prepareStatement(dbInitializerConfig.getClearTokensQuery()); |
|
|
|
preparedStatement.executeUpdate(); |
|
|
|
} |
|
|
|
|