Unverified Commit bee21d7d authored by Ryan Wang's avatar Ryan Wang Committed by GitHub
Browse files

refactor: get database version. (#698)

parent 40f510f3
Showing with 31 additions and 20 deletions
+31 -20
......@@ -2,6 +2,7 @@ package run.halo.app.listener;
import lombok.extern.slf4j.Slf4j;
import org.flywaydb.core.Flyway;
import org.flywaydb.core.internal.jdbc.JdbcUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ansi.AnsiColor;
......@@ -16,6 +17,7 @@ import org.springframework.util.Assert;
import org.springframework.util.ResourceUtils;
import run.halo.app.config.properties.HaloProperties;
import run.halo.app.model.properties.PrimaryProperties;
import run.halo.app.model.support.HaloConst;
import run.halo.app.service.OptionService;
import run.halo.app.service.ThemeService;
import run.halo.app.utils.FileUtils;
......@@ -23,6 +25,9 @@ import run.halo.app.utils.FileUtils;
import java.io.IOException;
import java.net.URI;
import java.nio.file.*;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Collections;
/**
......@@ -57,7 +62,11 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
this.migrate();
try {
this.migrate();
} catch (SQLException e) {
e.printStackTrace();
}
this.initThemes();
this.initDirectory();
this.printStartInfo();
......@@ -77,8 +86,9 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
/**
* Migrate database.
*/
private void migrate() {
private void migrate() throws SQLException {
log.info("Starting migrate database...");
Flyway flyway = Flyway
.configure()
.locations("classpath:/migration")
......@@ -88,6 +98,19 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
.load();
flyway.repair();
flyway.migrate();
// Gets database connection
Connection connection = flyway.getConfiguration().getDataSource().getConnection();
// Gets database metadata
DatabaseMetaData databaseMetaData = JdbcUtils.getDatabaseMetaData(connection);
// Gets database product name
HaloConst.DATABASE_PRODUCT_NAME = databaseMetaData.getDatabaseProductName() + " " + databaseMetaData.getDatabaseProductVersion();
// Close connection.
connection.close();
log.info("Migrate database succeed.");
}
......
......@@ -54,17 +54,14 @@ public class HaloConst {
* Version constant. (Available in production environment)
*/
public static final String HALO_VERSION;
/**
* Path separator.
*/
public static final String FILE_SEPARATOR = File.separator;
/**
* Suffix of freemarker template file
*/
public static final String SUFFIX_FTL = ".ftl";
/**
* Custom freemarker tag method key.
*/
......@@ -135,12 +132,12 @@ public class HaloConst {
* Content api token param name
*/
public final static String API_ACCESS_KEY_QUERY_NAME = "api_access_key";
public final static String ONE_TIME_TOKEN_QUERY_NAME = "ott";
public final static String ONE_TIME_TOKEN_HEADER_NAME = "ott";
/**
* Database product name.
*/
public static String DATABASE_PRODUCT_NAME = null;
/**
* user_session
*/
......
......@@ -5,7 +5,6 @@ import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.NonNull;
......@@ -93,10 +92,6 @@ public class AdminServiceImpl implements AdminService {
private final ApplicationEventPublisher eventPublisher;
private final String driverClassName;
private final String mode;
public AdminServiceImpl(PostService postService,
SheetService sheetService,
AttachmentService attachmentService,
......@@ -110,9 +105,7 @@ public class AdminServiceImpl implements AdminService {
AbstractStringCacheStore cacheStore,
RestTemplate restTemplate,
HaloProperties haloProperties,
ApplicationEventPublisher eventPublisher,
@Value("${spring.datasource.driver-class-name}") String driverClassName,
@Value("${spring.profiles.active:prod}") String mode) {
ApplicationEventPublisher eventPublisher) {
this.postService = postService;
this.sheetService = sheetService;
this.attachmentService = attachmentService;
......@@ -127,8 +120,6 @@ public class AdminServiceImpl implements AdminService {
this.restTemplate = restTemplate;
this.haloProperties = haloProperties;
this.eventPublisher = eventPublisher;
this.driverClassName = driverClassName;
this.mode = mode;
}
......@@ -297,7 +288,7 @@ public class AdminServiceImpl implements AdminService {
// Get application start time.
environmentDTO.setStartTime(ManagementFactory.getRuntimeMXBean().getStartTime());
environmentDTO.setDatabase("org.h2.Driver".equals(driverClassName) ? "H2" : "MySQL");
environmentDTO.setDatabase(DATABASE_PRODUCT_NAME);
environmentDTO.setVersion(HaloConst.HALO_VERSION);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment