Abz*_*han 5 java spring liquibase spring-boot liquibase-hibernate
我的包结构如下:
在/db.changelog/db.changelod-master.xml中,我包含/db.changelog/v1/db.changelog-1.0.xml,其中,我还包括/db.changelog/v1/changeset软件包中的所有更改日志。
在我的应用程序中,我有两个配置文件:dev和prod,我需要根据Liquibase的“最佳实践”划分软件包的结构。有些变更日志可以在产品和开发环境中使用。
另外,我可以在changeset标记中使用context属性并显式设置dev或prod值,但是这种解决方法不是可取的。
简单用法如下:我切换到产品概要文件,并且将不会创建某些表,或者会跳过对数据库的某些插入。
您能帮我根据Liquibase“最佳实践”重构软件包的结构吗?
解决方案 1:
您需要在 yaml 文件中定义 'liquibase.contexts' 属性。像下面这样的东西。
spring:
profiles: dev
datasource:
url: jdbc:postgresql://localhost:5432/dev
username: postgres
password: password
driver-class-name: org.postgresql.Driver
liquibase:
contexts: dev
Run Code Online (Sandbox Code Playgroud)
添加此后,以下更改集只会在您的本地配置文件为“dev”时执行(即 spring-boot:run -Dspring.profiles.active=dev)
<changeSet id="20161016_my_first_change2" author="krudland" context="dev">
<sql>
insert into customer (firstname, lastname) values ('Franklin','Ike');
</sql>
<rollback>
delete from customer where firstname = 'Franklin' and lastname = 'Ike';
</rollback>
</changeSet>
Run Code Online (Sandbox Code Playgroud)
解决方案 2:
如果您不想使用 liquibase.context,您可以使用 maven 来过滤资源:关键是将 maven过滤器元素与资源元素结合使用,如Liquibase 文档中所述。
在 maven 命令中包含资源目标也很重要:
mvn resources:resources liquibase:update -Plocal
Run Code Online (Sandbox Code Playgroud)
这是我使用的文件层次结构:
|-- pom.xml
`-- src
`-- main
|-- resources
| `-- liquibase.properties
| |-- changelog
| `-- db-changelog-master.xml
| `-- db-changelog-1.0.xml
|-- filters
|-- local
| `-- db.properties
|-- dev
| `-- db.properties
Run Code Online (Sandbox Code Playgroud)
db.properties 文件如下所示:
database.driver = oracle.jdbc.driver.OracleDriver
database.url = jdbc:oracle:thin:@<host_name>:<port_number>/instance
database.username = user
database.password = password123
database.changelogfile = db.changelog-master.xml
Run Code Online (Sandbox Code Playgroud)
liquibase.properties 文件如下所示:
changeLogFile: changelog/${database.changelogfile}
driver: ${database.driver}
url: ${database.url}
username: ${database.username}
password: ${database.password}
verbose: true
Run Code Online (Sandbox Code Playgroud)
POM 文件如下所示:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<propertyFile>target/classes/liquibase.properties</propertyFile>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<profiles>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<filters>
<filter>src/main/filters/local/db.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</profile>
<profile>
<id>dev</id>
<build>
<filters>
<filter>src/main/filters/dev/db.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</profile>
</profiles>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1284 次 |
| 最近记录: |