refactor: get database version. (#698)

pull/705/head
Ryan Wang 2020-03-20 22:54:05 +08:00 committed by GitHub
parent 40f510f371
commit bee21d7de8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 20 deletions

View File

@ -2,6 +2,7 @@ package run.halo.app.listener;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.flywaydb.core.Flyway; 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.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ansi.AnsiColor; import org.springframework.boot.ansi.AnsiColor;
@ -16,6 +17,7 @@ import org.springframework.util.Assert;
import org.springframework.util.ResourceUtils; import org.springframework.util.ResourceUtils;
import run.halo.app.config.properties.HaloProperties; import run.halo.app.config.properties.HaloProperties;
import run.halo.app.model.properties.PrimaryProperties; 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.OptionService;
import run.halo.app.service.ThemeService; import run.halo.app.service.ThemeService;
import run.halo.app.utils.FileUtils; import run.halo.app.utils.FileUtils;
@ -23,6 +25,9 @@ import run.halo.app.utils.FileUtils;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.nio.file.*; import java.nio.file.*;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Collections; import java.util.Collections;
/** /**
@ -57,7 +62,11 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
@Override @Override
public void onApplicationEvent(ApplicationStartedEvent event) { public void onApplicationEvent(ApplicationStartedEvent event) {
this.migrate(); try {
this.migrate();
} catch (SQLException e) {
e.printStackTrace();
}
this.initThemes(); this.initThemes();
this.initDirectory(); this.initDirectory();
this.printStartInfo(); this.printStartInfo();
@ -77,8 +86,9 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
/** /**
* Migrate database. * Migrate database.
*/ */
private void migrate() { private void migrate() throws SQLException {
log.info("Starting migrate database..."); log.info("Starting migrate database...");
Flyway flyway = Flyway Flyway flyway = Flyway
.configure() .configure()
.locations("classpath:/migration") .locations("classpath:/migration")
@ -88,6 +98,19 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
.load(); .load();
flyway.repair(); flyway.repair();
flyway.migrate(); 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."); log.info("Migrate database succeed.");
} }

View File

@ -54,17 +54,14 @@ public class HaloConst {
* Version constant. (Available in production environment) * Version constant. (Available in production environment)
*/ */
public static final String HALO_VERSION; public static final String HALO_VERSION;
/** /**
* Path separator. * Path separator.
*/ */
public static final String FILE_SEPARATOR = File.separator; public static final String FILE_SEPARATOR = File.separator;
/** /**
* Suffix of freemarker template file * Suffix of freemarker template file
*/ */
public static final String SUFFIX_FTL = ".ftl"; public static final String SUFFIX_FTL = ".ftl";
/** /**
* Custom freemarker tag method key. * Custom freemarker tag method key.
*/ */
@ -135,12 +132,12 @@ public class HaloConst {
* Content api token param name * Content api token param name
*/ */
public final static String API_ACCESS_KEY_QUERY_NAME = "api_access_key"; 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_QUERY_NAME = "ott";
public final static String ONE_TIME_TOKEN_HEADER_NAME = "ott"; public final static String ONE_TIME_TOKEN_HEADER_NAME = "ott";
/**
* Database product name.
*/
public static String DATABASE_PRODUCT_NAME = null;
/** /**
* user_session * user_session
*/ */

View File

@ -5,7 +5,6 @@ import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
@ -93,10 +92,6 @@ public class AdminServiceImpl implements AdminService {
private final ApplicationEventPublisher eventPublisher; private final ApplicationEventPublisher eventPublisher;
private final String driverClassName;
private final String mode;
public AdminServiceImpl(PostService postService, public AdminServiceImpl(PostService postService,
SheetService sheetService, SheetService sheetService,
AttachmentService attachmentService, AttachmentService attachmentService,
@ -110,9 +105,7 @@ public class AdminServiceImpl implements AdminService {
AbstractStringCacheStore cacheStore, AbstractStringCacheStore cacheStore,
RestTemplate restTemplate, RestTemplate restTemplate,
HaloProperties haloProperties, HaloProperties haloProperties,
ApplicationEventPublisher eventPublisher, ApplicationEventPublisher eventPublisher) {
@Value("${spring.datasource.driver-class-name}") String driverClassName,
@Value("${spring.profiles.active:prod}") String mode) {
this.postService = postService; this.postService = postService;
this.sheetService = sheetService; this.sheetService = sheetService;
this.attachmentService = attachmentService; this.attachmentService = attachmentService;
@ -127,8 +120,6 @@ public class AdminServiceImpl implements AdminService {
this.restTemplate = restTemplate; this.restTemplate = restTemplate;
this.haloProperties = haloProperties; this.haloProperties = haloProperties;
this.eventPublisher = eventPublisher; this.eventPublisher = eventPublisher;
this.driverClassName = driverClassName;
this.mode = mode;
} }
@ -297,7 +288,7 @@ public class AdminServiceImpl implements AdminService {
// Get application start time. // Get application start time.
environmentDTO.setStartTime(ManagementFactory.getRuntimeMXBean().getStartTime()); environmentDTO.setStartTime(ManagementFactory.getRuntimeMXBean().getStartTime());
environmentDTO.setDatabase("org.h2.Driver".equals(driverClassName) ? "H2" : "MySQL"); environmentDTO.setDatabase(DATABASE_PRODUCT_NAME);
environmentDTO.setVersion(HaloConst.HALO_VERSION); environmentDTO.setVersion(HaloConst.HALO_VERSION);