Abz*_*han 4 java postgresql database-migration liquibase spring-boot
我正在使用 Spring Boot 2 和 Liquibase (Core 3.6.2),我的数据库是 PostgreSQL。我正在 db.changelog-master.xml 中通过此变更集创建表:
<changeSet author="system" id="1">
<createTable tableName="test">
<column name="id" type="UUID">
<constraints nullable="false"/>
</column>
<column name="note" type="VARCHAR(4096)"/>
</createTable>
</changeSet>
Run Code Online (Sandbox Code Playgroud)
下一个变更集用于将 csv 文件中的值插入到此表中:
<changeSet author="system" id="2">
<loadData encoding="UTF-8" file="classpath:liquibase/data/test.csv" quotchar=""" separator="," tableName="test">
<column header="id" name="id" type="STRING" />
<column header="note" name="note" type="STRING"/>
</loadData>
</changeSet>
Run Code Online (Sandbox Code Playgroud)
如果我在列id中指定类型 UUID而不是 STRING,liquibase 会告诉我:
loadData type of uuid is not supported. Please use BOOLEAN, NUMERIC, DATE, STRING, COMPUTED or SKIP
Run Code Online (Sandbox Code Playgroud)
test.csv文件的内容:
"id","note"
"18d892e0-e88d-4b18-a5c0-c209983ea3c0","test-note"
Run Code Online (Sandbox Code Playgroud)
当我运行应用程序时,liquibase 创建了表,当它尝试插入值时,我收到此消息:
ERROR: column "id" is of type uuid but expression is of type character varying
Run Code Online (Sandbox Code Playgroud)
问题出在位于 liquibase-core 依赖项中的类 ExecutablePreparedStatementBase 中,以及此类中创建此错误的方法行:
private void applyColumnParameter(PreparedStatement stmt, int i, ColumnConfig col) throws SQLException,
DatabaseException {
if (col.getValue() != null) {
LOG.debug(LogType.LOG, "value is string = " + col.getValue());
stmt.setString(i, col.getValue());
}
Run Code Online (Sandbox Code Playgroud)
Liquibase 使用 JDBC 和PreparedStatement 来执行查询。问题是因为表test的列类型是uuid,而 liquibase 尝试插入string。如果我们使用 JDBC 手动将值插入到该表中,我们应该使用setObject方法PreparedStatement而不是setString。但是如果这个问题位于 liquibase-core.jar 中,我该如何解决这个问题?有人能帮我吗?
这很痛苦,但我找到了解决方案。您需要stringtype=unspecified在 JDBC URL 属性中指定参数。例如在 application.properties 中:
spring.liquibase.url=jdbc:postgresql://127.0.0.1:5432/postgres?stringtype=unspecified
Run Code Online (Sandbox Code Playgroud)
我希望这个答案会对某人有所帮助。