Liquibase生成的更改日志引发SQL语法异常



我已经使用Liquibase的generateChangeLog命令为现有数据库生成了一个更改日志。但是,当我尝试运行此程序时,会抛出一个SQLSyntaxErrorException。

Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'databaseChangeLog:
- changeSet:
id: 1595846089000-1
author: A (gen' at line 1
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) ~[mysql-connector-java-8.0.21.jar:8.0.21]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.21.jar:8.0.21]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.21.jar:8.0.21]
at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:764) ~[mysql-connector-java-8.0.21.jar:8.0.21]
at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:648) ~[mysql-connector-java-8.0.21.jar:8.0.21]
at com.zaxxer.hikari.pool.ProxyStatement.execute(ProxyStatement.java:95) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.HikariProxyStatement.execute(HikariProxyStatement.java) ~[HikariCP-3.4.5.jar:na]
at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:352) ~[liquibase-core-3.6.3.jar:na]
... 31 common frames omitted

更改日志

databaseChangeLog:
- changeSet:
id: 1595846089000-1
author: A123456 (generated)
changes:
- createTable:
columns:
- column:
constraints:
primaryKey: true
name: id
type: VARCHAR(36)
- column:
name: message
type: LONGTEXT
- column:
name: output
type: LONGTEXT
- column:
name: result
type: VARCHAR(16)
tableName: task

我尝试过用不同的格式(xml、yaml、sql(生成文件,但即使语法有效,它们都会以相同的错误失败。

我认为生成的日志字符集可能是个问题,所以我尝试将以下内容添加到连接URL中。

&useJvmCharsetConverters=true

在看到这个问题后,我还确保了文件是UTF-8(没有bom(。

运行Liquibase 3.6.3

如有任何帮助,我们将不胜感激!

编辑以添加主更改日志:

databaseChangeLog:
- changeSet:
id: 1
author: A123456
dbms: mysql
labels: initial-migration
preConditions:
- onFail: MARK_RAN
- onError: MARK_RAN
- not:
tableExists:
schemaName: pd
tableName: task
changes:
- sqlFile:
path: initial-migration.yaml
relativeToChangelogFile: true
Caused by: liquibase.exception.MigrationFailedException: Migration failed for change set classpath:/db/changelog/db.changelog-master.yaml::1::A123456:
Reason: liquibase.exception.DatabaseException: You have an error in your SQL syntax;

尝试使用"updateSQL"命令从更改日志生成sql,并检查生成的脚本是否存在语法错误。

updateSQL命令的doc链接https://docs.liquibase.com/commands/community/updatesql.html

所以最终我解决了这个问题。我在主日志中声明了- sqlFile:(第一个变更集(,它期望的是纯SQL(而不是变更集(。

在Liquibase生成SQL文件的地方,它用双连字符声明更改集参数(名称等(,并且没有空格。这不是有效的SQL注释,但它是有效的Liquibase格式。因此,Liquibase生成的SQL失败了,yaml/xml格式总是会失败,因为它们不是SQL。

解决方案是用Liquibase的include:标记声明文件,这样可以正确读取生成的SQL。链接

相关内容

最新更新