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 @Bean
@ConditionalOnMissingBean(value = DataBaseProperties.class) @ConditionalOnMissingBean(value = DataBaseProperties.class)
public DataBaseProperties dataBaseProperties(@Value("${hippo4j.database.init_script:sql-script/h2/schema.sql}") String initScript, public DataBaseProperties dataBaseProperties(@Value("${hippo4j.database.dialect:h2}") String dialect,
@Value("${hippo4j.database.init_enable:true}") Boolean initEnable) { @Value("${hippo4j.database.init_script:sql-script/h2/schema.sql}") String initScript,
@Value("${hippo4j.database.init_enable:false}") Boolean initEnable) {
DataBaseProperties dataSourceProperties = new DataBaseProperties(); DataBaseProperties dataSourceProperties = new DataBaseProperties();
dataSourceProperties.setDialect(dialect);
dataSourceProperties.setInitScript(initScript); dataSourceProperties.setInitScript(initScript);
dataSourceProperties.setInitEnable(initEnable); dataSourceProperties.setInitEnable(initEnable);
return dataSourceProperties; return dataSourceProperties;

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

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

Loading…
Cancel
Save