0

我使用 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;
4

2 回答 2

0

所以最终我发现了问题所在。我- sqlFile:在主日志(第一个更改集)中声明的地方是纯 SQL(不是更改集)。

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

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

于 2020-08-16T20:34:36.287 回答
0

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

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

于 2020-08-16T07:43:38.057 回答