Joã*_*ves 6 java mysql time liquibase sql-timestamp
我在 MySQL 8 中使用 Liquibase 时遇到一个问题,其中以下脚本没有放置“time(3)”类型的小数部分,它仅将“time”放置在列的类型上。我们之前在 MySQL 5 上运行过这个脚本,效果很好。
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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-4.16.xsd"
logicalFilePath="20220901.xml">
<changeSet author="MyUser" id="Change column 'time' to Datatype to milliseconds">
<modifyDataType
columnName="time"
newDataType="TIME(3)"
schemaName="${defaultSchema}"
tableName="table1"/>
<addNotNullConstraint
columnDataType="TIME(3)"
columnName="time"
schemaName="${defaultSchema}"
tableName="table1" />
</changeSet>
</databaseChangeLog>
Run Code Online (Sandbox Code Playgroud)
我尝试将 liquibase.core(至 4.16.1)和 mysql-connector-java(至 8.0.30)的 Maven 依赖项更新到最新版本,问题仍然存在。
经过多次测试,我发现问题可能出在 liquibase 生成的查询上,该查询不包含分数部分“(3)”,因此作为解决方法,我使用“modifySql”在最后更改查询。
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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-4.16.xsd"
logicalFilePath="20220901.xml">
<!-- WORK-AROUND - Liquibase was generating a query with type 'TIME' instead of 'TIME(3)' so
we use 'REPLACE_WITH_TIME' as auxiliary type to replace all of it's occurrences in query
by 'TIME(3)' with 'modifySql'. -->
<changeSet author="MyUser"
id="Fix time column type to time(3) - 2022-10-06">
<modifyDataType columnName="time"
newDataType="REPLACE_WITH_TIME" schemaName="${defaultSchema}"
tableName="table1" />
<addNotNullConstraint columnDataType="REPLACE_WITH_TIME"
columnName="time" schemaName="${defaultSchema}"
tableName="table1" />
<modifySql>
<replace replace="REPLACE_WITH_TIME" with="TIME(3)" />
</modifySql>
</changeSet>
</databaseChangeLog>
Run Code Online (Sandbox Code Playgroud)
它解决了问题,但不是最好的解决方案。所以我想问是否有人注意到这一点并知道它是否真的是一个 liquibase 错误。
提前致谢。