Database initialization logic is perfect

pull/743/head
chen.ma 2 years ago
parent 4024401618
commit 8d529bca4d

@ -32,9 +32,11 @@ public class DataBaseConfiguration {
@Bean
@ConditionalOnMissingBean(value = DataBaseProperties.class)
public DataBaseProperties dataBaseProperties(@Value("${hippo4j.database.init_script:sql-script/h2/schema.sql}") String initScript,
@Value("${hippo4j.database.init_enable:true}") Boolean initEnable) {
public DataBaseProperties dataBaseProperties(@Value("${hippo4j.database.dialect:h2}") String dialect,
@Value("${hippo4j.database.init_script:sql-script/h2/schema.sql}") String initScript,
@Value("${hippo4j.database.init_enable:false}") Boolean initEnable) {
DataBaseProperties dataSourceProperties = new DataBaseProperties();
dataSourceProperties.setDialect(dialect);
dataSourceProperties.setInitScript(initScript);
dataSourceProperties.setInitEnable(initEnable);
return dataSourceProperties;

@ -29,6 +29,11 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "hippo4j.database")
public class DataBaseProperties {
/**
* Dialect
*/
private String dialect;
/**
* Init script
*/

@ -38,6 +38,7 @@ import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.sql.*;
import java.util.List;
import java.util.Objects;
/**
* Local datasource loader.
@ -64,31 +65,36 @@ public class LocalDataSourceLoader implements InstantiationAwareBeanPostProcesso
private void init(final DataSourceProperties properties) {
try {
String jdbcUrl = properties.getUrl();
// If jdbcUrl in the configuration file specifies the hippo4j database, it is removed,
// because the hippo4j database does not need to be specified when executing the SQL file,
// otherwise the hippo4j database will be disconnected when the hippo4j database does not exist
String jdbcUrl = StringUtils.replace(properties.getUrl(), "/hippo4j_manager?", "?");
if (Objects.equals(dataBaseProperties.getDialect(), "mysql")) {
jdbcUrl = StringUtils.replace(properties.getUrl(), "/hippo4j_manager?", "?");
}
Connection connection = DriverManager.getConnection(jdbcUrl, properties.getUsername(), properties.getPassword());
// TODO Compatible with h2 to execute `INSERT IGNORE INTO` statement error
if (ifExecute(connection)) {
execute(connection, dataBaseProperties.getInitScript());
if (Objects.equals(dataBaseProperties.getDialect(), "h2") && ifNonExecute(connection)) {
return;
}
execute(connection, dataBaseProperties.getInitScript());
} catch (Exception ex) {
log.error("Datasource init error.", ex);
throw new RuntimeException(ex.getMessage());
}
}
private boolean ifExecute(final Connection conn) throws SQLException {
private boolean ifNonExecute(final Connection conn) throws SQLException {
try (Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT COUNT(*) FROM `user`")) {
if (resultSet.next()) {
int countUser = resultSet.getInt(1);
return countUser > 0 ? false : true;
return countUser > 0 ? true : false;
}
} catch (Exception ignored) {
log.error("Query data for errors.", ignored);
}
return true;
return false;
}
private void execute(final Connection conn, final String script) throws Exception {

Loading…
Cancel
Save