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; package run.halo.app.listener;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.flywaydb.core.Flyway;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration; 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.model.properties.PrimaryProperties;
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.service.UserService;
import run.halo.app.utils.FileUtils; import run.halo.app.utils.FileUtils;
import java.io.IOException; import java.io.IOException;
@ -43,13 +44,20 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
@Autowired @Autowired
private ThemeService themeService; private ThemeService themeService;
@Autowired @Value("${spring.datasource.url}")
private UserService userService; private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Override @Override
public void onApplicationEvent(ApplicationStartedEvent event) { public void onApplicationEvent(ApplicationStartedEvent event) {
this.printStartInfo(); this.migrate();
this.initThemes(); this.initThemes();
this.printStartInfo();
} }
private void printStartInfo() { private void printStartInfo() {
@ -63,6 +71,22 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
log.info("Halo has started successfully!"); 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 * Init internal themes
*/ */

View File

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

View File

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

View File

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

View File

@ -2,5 +2,4 @@
-- Migrate journals Table -- Migrate journals Table
alter table journals modify content text not null; 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` update journals set `source_content`=`content`