当前,一旦应用程序成功启动,我们正在尝试基于ApplicationReadyEvent
运行数据库升级。
private void upgradeDatabase() {
int currentVersion = // initially saved in db_version table - say 1
if (currentVersion < 2) {
// the code to upgrade from current version (1) to new version (2)
// update version number in db_version table
}
if (currentVersion < 3) {
// the code to upgrade from current version (2) to new version (3)
// update version number in db_version table
}
}
由于数据升级背后有一些业务逻辑,这些逻辑无法通过Liquibase或Flyway甚至SQL来处理。
Spring Boot是否提供了更好的方法来处理数据库升级?
有一个Liquibase Java API,您可以随时使用它。查看这篇文章
直接运行Liquibase最简单的方法如下:
java.sql.Connection connection = openConnection(); //your openConnection logic here
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
Liquibase liquibase = new liquibase.Liquibase("path/to/changelog.xml", new ClassLoaderResourceAccessor(), database);
liquibase.update(new Contexts(), new LabelExpression());