问题描述
我的包裹结构如下:
在/db.changelog/db.changelod-master.xml 中,我包含/db.changelog/v1/db.changelog-1.0.xml /db.changelog/v1/changeset 包中的所有更改日志.
In /db.changelog/db.changelod-master.xml i include /db.changelog/v1/db.changelog-1.0.xml where i also include all changelogs from /db.changelog/v1/changeset package.
在我的应用程序中,我有两个配置文件: dev 和 prod ,并且我需要根据Liquibase的最佳实践"来划分软件包的结构.某些变更日志可以位于 prod 和 dev 环境中.
In my application, I have two profiles: dev and prod, and I need to divide the structure of packages according to "Best Practises" of Liquibase. Some changelogs can be in prod and dev environment.
此外,我可以在 changeset 标记中使用 context 属性,并明确设置 dev 或 prod 值,但是这种解决方法不是可取的.
Also, I can use context attribute in changeset tag and explicitly set dev or prod value, but this workaround is not preferable.
简单用法如下:我切换到 prod 配置文件,并且将不会创建某些表,或者会跳过对数据库的某些插入.
Simple usage looks like: i switch to prod profile and some tables will not be created or some inserts to Database will be skipped.
根据Liquibase的最佳做法",您能帮我重构软件包的结构吗?
Can you please help me refactor structure of packages according to Liquibase "Best Practices??
推荐答案
解决方案1:
您需要在yaml文件中定义"liquibase.contexts"属性.像下面这样.
Solution1:
You need to define 'liquibase.contexts' property into your yaml file. Something like below.
spring:
profiles: dev
datasource:
url: jdbc:postgresql://localhost:5432/dev
username: postgres
password: password
driver-class-name: org.postgresql.Driver
liquibase:
contexts: dev
添加此更改后,以下更改集将仅在本地配置文件为"dev"时执行(即spring-boot:run -Dspring.profiles.active = dev)
After adding this the below change set will only execute when your local profile is 'dev' (i.e. 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>
解决方案2:
如果您不想使用liquibase.context,则可以使用maven来过滤资源:关键是将maven filter 元素与 resource 元素结合使用,如 Liquibase文档.
Solution2:
If you don't want to use liquibase.context, You might can use maven to filter recourses :The key was to use the maven filter element in conjunction with the resource element as explained in Liquibase Documentation.
另外,在maven命令中包含 resources 目标也很重要:
Also it's important to include the resources goal in the maven command:
mvn resources:resources liquibase:update -Plocal
这是我使用的文件层次结构:
This is the file hierarchy I used:
|-- pom.xml
`-- src
`-- main
|-- resources
| `-- liquibase.properties
| |-- changelog
| `-- db-changelog-master.xml
| `-- db-changelog-1.0.xml
|-- filters
|-- local
| `-- db.properties
|-- dev
| `-- db.properties
db.properties文件如下所示:
The db.properties file would look like the following:
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
liquibase.properties文件如下所示:
The liquibase.properties file would look like the following:
changeLogFile: changelog/${database.changelogfile}
driver: ${database.driver}
url: ${database.url}
username: ${database.username}
password: ${database.password}
verbose: true
POM文件如下所示:
The POM file would look like the following:
<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>
这篇关于如何在Spring Boot中为开发和生产环境划分Liquibase软件包结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!