在我当前的项目中,有一个数据库团队在将所有脚本应用于生产之前检查所有脚本。
我们正在使用 Liquibase 将变更集应用于开发,但对于生产,我们需要能够生成包含所有语句的 *.sql 文件。
根据 liquibase-maven-plugin 的文档,updateSQL 应该是我想要的: http://www.liquibase.org/documentation/maven/maven_updatesql.html 。
所以我创建了两个 Maven 配置文件。一个将更改应用到本地开发数据库(使用 liquibase:update),另一个只生成脚本。问题是:执行 liquibase:updateSQL 确实会生成 *.sql 文件(如预期),但它也会尝试连接到数据库并应用更改(不是预期的)。我相信 updateSQL 的文档会导致错误,因为它说:
Generates the SQL that is required to update the database to the current version as specified in the DatabaseChangeLogs.
没有提到它实际上会应用变更集,就像 liquibase:update 那样。
我可能误解了这里的文档,但是 updateSQL 不应该只生成 sql 还是它实际上应该执行更新 + 生成 sql?
这是我的插件配置:
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.0.5</version>
<configuration>
<changeLogFile>src/main/resources/db/liquibase_changeset.xml</changeLogFile>
<driver>oracle.jdbc.driver.OracleDriver</driver>
<url>${liquibase.db.url}</url>
<username>${liquibase.db.user}</username>
<password>${liquibase.db.password}</password>
<promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
</configuration>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>${liquibase.exec.goal}</goal>
</goals>
</execution>
</executions>
</plugin>
我创建了这样的配置文件:
<profiles>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<liquibase.exec.goal>update</liquibase.exec.goal>
<liquibase.exec.prompt>false</liquibase.exec.prompt>
<liquibase.db.url>jdbc:oracle:thin:@host:1521:xe</liquibase.db.url>
<liquibase.db.user>user</liquibase.db.user>
<liquibase.db.password>password</liquibase.db.password>
</properties>
</profile>
<profile>
<id>uat</id>
<properties>
<liquibase.exec.goal>updateSQL</liquibase.exec.goal>
<liquibase.exec.prompt>true</liquibase.exec.prompt>
<liquibase.db.url>jdbc:oracle:thin:@host2:1521:xe</liquibase.db.url>
<liquibase.db.user>user2</liquibase.db.user>
<liquibase.db.password>password2</liquibase.db.password>
</properties>
</profile>
</profiles>
鉴于我的 Maven 配置和我的理解,我希望
mvn install -P uat
只生成脚本而不尝试连接到数据库。
我被迫指定数据库属性(驱动程序等)这一事实让我相信这是为了始终更改数据库,但我认为应该可以只生成脚本而不尝试对数据库应用更改。
有什么想法吗?有可能吗,但我完全走错了路?或者我缺少一些简单的属性?或者它根本不支持?
提前致谢。
最佳答案
UpdateSQL 实际上并不更新数据库,它只是输出 SQL。
之所以需要数据库连接信息并进行实际连接,是因为它需要从 databasechangelog 表中进行选择,以确定哪些 changeSet 已运行,哪些尚未运行。
关于maven - Liquibase - 生成脚本而不将变更集应用于数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22941876/