通过Liquibase从SQL脚本创建函数

时间:2016-01-11 00:13:04

标签: sql function liquibase

项目配置:

  • 数据库 - MySQL 5.7
  • orm - Hibernate 4.3.11.Final / JPA 1.3.1.RELEASE
  • Liquibase 3.4.2

当我仅从Liquibase运行来自workBeanch的脚本时,我的问题不存在。

<changeSet author="newbie" id="function_rad2deg" dbms="mysql,h2">
    <sqlFile encoding="utf8" path="sql/function_rad2deg.sql" relativeToChangelogFile="true"  splitStatements="false" stripComments="false"/>
</changeSet>

我的sql脚本如下所示:

DROP FUNCTION IF EXISTS rad2deg;

DELIMITER //

CREATE FUNCTION rad2deg(rad DOUBLE)
    RETURNS DOUBLE
    BEGIN
        RETURN (rad * 180 / PI());
    END

//

DELIMITER ;

好的并记录:

liquibase.exception.DatabaseException: 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 'DELIMITER //

CREATE FUNCTION rad2deg(rad DOUBLE)
    RETURNS DOUBLE
    BEGIN
 ' at line 3 [Failed SQL: DROP FUNCTION IF EXISTS rad2deg;

DELIMITER //

CREATE FUNCTION rad2deg(rad DOUBLE)
    RETURNS DOUBLE
    BEGIN
        RETURN (rad * 180 / PI());
    END

//

DELIMITER ;]
    at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:301)
    at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:55)
    at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:107)
    at liquibase.database.AbstractJdbcDatabase.execute(AbstractJdbcDatabase.java:1251)
    at liquibase.database.AbstractJdbcDatabase.executeStatements(AbstractJdbcDatabase.java:1234)
    at liquibase.changelog.ChangeSet.execute(ChangeSet.java:554)
    at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:51)
    at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:73)
    at liquibase.Liquibase.update(Liquibase.java:212)
    at liquibase.Liquibase.update(Liquibase.java:192)
    at liquibase.Liquibase.update(Liquibase.java:327)
    at org.liquibase.maven.plugins.LiquibaseUpdate.doUpdate(LiquibaseUpdate.java:33)
    at org.liquibase.maven.plugins.AbstractLiquibaseUpdateMojo.performLiquibaseTask(AbstractLiquibaseUpdateMojo.java:30)
    at org.liquibase.maven.plugins.AbstractLiquibaseMojo.execute(AbstractLiquibaseMojo.java:394)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:862)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:286)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:197)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
    at org.codehaus.classworlds.Launcher.main(Launcher.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'DELIMITER //

CREATE FUNCTION rad2deg(rad DOUBLE)
    RETURNS DOUBLE
    BEGIN
 ' at line 3
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at com.mysql.jdbc.Util.getInstance(Util.java:387)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:939)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2547)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2505)
    at com.mysql.jdbc.StatementImpl.executeInternal(StatementImpl.java:840)
    at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:740)
    at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:299)
    ... 41 more

3 个答案:

答案 0 :(得分:7)

我认为您需要根据找到的文档here

在Liquibase标记中添加endDelimiter子句

以下是一个例子

<changeSet author="newbie" id="function_rad2deg" dbms="mysql,h2">
    <sqlFile encoding="utf8" 
             path="sql/function_rad2deg.sql" 
             relativeToChangelogFile="true"  
             splitStatements="false" 
             stripComments="false"
             endDelimiter="\nGO"
    />
</changeSet>

具有上述分隔符的SQL文件看起来像

DROP FUNCTION IF EXISTS rad2deg;
GO

CREATE FUNCTION rad2deg(rad DOUBLE)
RETURNS DOUBLE
BEGIN
    RETURN (rad * 180 / PI());
END
GO

希望这有帮助

答案 1 :(得分:1)

如果您使用的是yaml文件,则配置如下

changeSet:
  id: sqlFile-function
  author: sandeep
  logicalFilePath: baseFunctionScript
  changes:
  - sqlFile:
      dbms: mysql
      encoding: utf8
      endDelimiter: \nGO
      path: my/path/baseScripts.sql
      splitStatements: true
      stripComments: false

确保stripComments应该为false,否则将引发错误。因此,我花了4个小时。

答案 2 :(得分:0)

在上面的示例中,有两个问题,一个是在h2上不起作用,第二个是splitStatements应该为真:

<changeSet author="me" id="01_functions_mysql" dbms="mysql">
    <sqlFile encoding="utf8" path="sql/01_functions.mysql.sql" 
    relativeToChangelogFile="true" 
    splitStatements="true" 
    stripComments="false" 
    endDelimiter="\nGO" />
</changeSet>

还有类似这样的东西:

DROP FUNCTION IF EXISTS FIRST_DAY_THIS_MONTH;

GO

CREATE FUNCTION FIRST_DAY_THIS_MONTH (day date) 
RETURNS date
DETERMINISTIC
BEGIN 
  RETURN STR_TO_DATE  ( DATE_FORMAT    ( day,'%Y%m01' ),'%Y%m%d');
END

GO