我建議你看看Liquibase這將允許應用程序保持自己的數據庫最新。
如果您使用的春天,它很容易,你只需要一個Spring bean,如:
@Bean
public SpringLiquibase liquibase() {
log.debug("Configuring Liquibase");
SpringLiquibase liquibase = new SpringLiquibase();
liquibase.setDataSource(dataSource());
liquibase.setChangeLog("classpath:config/liquibase/master.xml");
liquibase.setContexts("development, production");
return liquibase;
}
然後,你必須修改日誌:
databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<changeSet id="1" author="bob">
<createTable tableName="department">
<column name="id" type="int">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="name" type="varchar(50)">
<constraints nullable="false"/>
</column>
<column name="active" type="boolean" defaultValueBoolean="true"/>
</createTable>
</changeSet>
</databaseChangeLog>
每個應用程序啓動時,它會檢查,如果時間有任何需要應用的更改。
感謝您的建議。我從來沒有嘗試過Liquibase。將做一些PoC,看看它是如何幫助。 – ak2205