fix: migrate database error.

pull/471/head^2
ruibaby 2019-12-24 19:48:47 +08:00
parent c758c76f20
commit 4cdfc2247c
5 changed files with 31 additions and 14 deletions

View File

@ -1,7 +1,9 @@
package run.halo.app.listener;
import lombok.extern.slf4j.Slf4j;
import org.flywaydb.core.Flyway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
@ -14,7 +16,6 @@ import run.halo.app.config.properties.HaloProperties;
import run.halo.app.model.properties.PrimaryProperties;
import run.halo.app.service.OptionService;
import run.halo.app.service.ThemeService;
import run.halo.app.service.UserService;
import run.halo.app.utils.FileUtils;
import java.io.IOException;
@ -43,13 +44,20 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
@Autowired
private ThemeService themeService;
@Autowired
private UserService userService;
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
this.printStartInfo();
this.migrate();
this.initThemes();
this.printStartInfo();
}
private void printStartInfo() {
@ -63,6 +71,22 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
log.info("Halo has started successfully!");
}
/**
* Migrate database.
*/
private void migrate() {
log.info("Starting migrate database...");
Flyway flyway = Flyway
.configure()
.locations("classpath:/migration")
.baselineVersion("1")
.baselineOnMigrate(true)
.dataSource(url, username, password)
.load();
flyway.migrate();
log.info("Migrate database succeed.");
}
/**
* Init internal themes
*/

View File

@ -37,9 +37,7 @@ spring:
show-sql: true
open-in-view: false
flyway:
locations: classpath:/migration
baseline-on-migrate: true
baseline-version: 1
enabled: false
servlet:
multipart:
max-file-size: 10240MB

View File

@ -36,9 +36,7 @@ spring:
ddl-auto: update
show-sql: true
flyway:
locations: classpath:/migration
baseline-on-migrate: true
baseline-version: 1
enabled: false
servlet:
multipart:
max-file-size: 10MB

View File

@ -39,9 +39,7 @@ spring:
show-sql: false
open-in-view: false
flyway:
locations: classpath:/migration
baseline-on-migrate: true
baseline-version: 1
enabled: false
servlet:
multipart:
max-file-size: 10240MB

View File

@ -2,5 +2,4 @@
-- Migrate journals Table
alter table journals modify content text not null;
alter table journals add source_content varchar(1023) default '' not null;
update journals set `source_content`=`content`