【问题标题】:Create and run stored procedure using Grails database migration plugin使用 Grails 数据库迁移插件创建和运行存储过程
【发布时间】:2014-04-11 18:08:14
【问题描述】:

我正在尝试进行稍微尴尬的数据库迁移,并且我正在使用存储过程来更新我的一个应用程序表中的一些行。如果我使用 mysql 命令行工具或在 Sequel Pro 中执行存储过程,它运行正常,但是如果我尝试使用数据库迁移插件运行它,它不会运行。

貌似liquibase支持存储过程,但是数据库迁移插件好像炸了。有谁知道这是否应该工作?

错误(和完整的存储过程)如下所示:

    2014-03-08 09:05:21,693 [localhost-startStop-1] INFO  liquibase  - ChangeSet changelog_fb_contactForm_2_add_new_enquiry_roles.groovy::1393960870500-1::rcgeorge23 ran successfully in 6671ms
    | Error 2014-03-08 09:05:21,704 [localhost-startStop-1] ERROR liquibase  - Change Set changelog_fb_contactForm_3_add_enquiry_flexible_model_definition_to_existing_organisations.groovy::1393960870500-1::rcgeorge23
     failed.  Error: Error executing SQL DELIMITER // 
                            CREATE PROCEDURE ABC() 
                            BEGIN 
                                    REPEAT 
                                            set @organisationConfigurationId = (select id from organisation_configuration oc where oc.enquiry_flexible_model_definition_id is null limit 1); 
                                            insert into flexible_model_definition (`version`, `name`) values ('0', 'enquiry.flexible.model'); 
                                            set @newlyInsertedEnquiryFlexibleModelDefinitionId = LAST_INSERT_ID(); 
                                            update organisation_configuration oc set oc.enquiry_flexible_model_definition_id = @newlyInsertedEnquiryFlexibleModelDefinitionId where oc.id = @organisationConfigurationId
    ; 
                                    UNTIL 0 = (select count(*) from organisation_configuration oc where oc.enquiry_flexible_model_definition_id is null) 
                            END REPEAT; 
                            END //: 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 PROCEDURE ABC() 
                            BEGIN 
                                    REPEAT 
                                            set @organis' at line 1
    Message: Error executing SQL DELIMITER // 
                            CREATE PROCEDURE ABC() 
                            BEGIN 
                                    REPEAT 
                                            set @organisationConfigurationId = (select id from organisation_configuration oc where oc.enquiry_flexible_model_definition_id is null limit 1); 
                                            insert into flexible_model_definition (`version`, `name`) values ('0', 'enquiry.flexible.model'); 
                                            set @newlyInsertedEnquiryFlexibleModelDefinitionId = LAST_INSERT_ID(); 
                                            update organisation_configuration oc set oc.enquiry_flexible_model_definition_id = @newlyInsertedEnquiryFlexibleModelDefinitionId where oc.id = @organisationConfigurationId
    ; 
                                    UNTIL 0 = (select count(*) from organisation_configuration oc where oc.enquiry_flexible_model_definition_id is null) 
                            END REPEAT; 
                            END //: 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 PROCEDURE ABC() 
                            BEGIN 
                                    REPEAT 
                                            set @organis' at line 1
        Line | Method
    ->>   62 | execute                        in liquibase.executor.jvm.JdbcExecutor
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    |    104 | execute                        in     ''
    |   1091 | execute . . . . . . . . . . .  in liquibase.database.AbstractDatabase
    |   1075 | executeStatements              in     ''
    |    317 | execute . . . . . . . . . . .  in liquibase.changelog.ChangeSet
    |     27 | visit                          in liquibase.changelog.visitor.UpdateVisitor
    |     58 | run . . . . . . . . . . . . .  in liquibase.changelog.ChangeLogIterator
    ....

【问题讨论】:

    标签: mysql grails database-migration


    【解决方案1】:

    在定义存储过程时尝试使用splitStatements,例如:

    sqlFile path:'my_stored_procedure.sql', splitStatements: false

    否则,数据库迁移插件将尝试变得聪明并拆分您的语句,这会破坏您的定义。

    【讨论】:

    • 谢谢你——我最后没有使用存储过程,但如果我以后需要的话,我会试试这个。
    【解决方案2】:

    runOnChange='true' 配置强制插件检查更改集是否已更改,而不是仅检查它之前是否运行过一次以便可以跳过。

    默认情况下,文件中的 sql 语句在 ;'s 上拆分,以避免这种 splitStatements = 'false' 配置用于确保完整的创建过程脚本被读取为一个而不是拆分它在;里面的每个单独的 sql 语句。

    示例:

    changeSet(author: "Rahul", id: "1435600003872-128", runOnChange: "true") {
        sql("DROP PROCEDURE IF EXISTS BestCustomers;")
        sqlFile(path: "../sql/BestCustomers.sql", splitStatements: false)
    }
    

    BestCustomers.sql 的代码:

    CREATE PROCEDURE `BestCustomers`(
         //Input params here
       )
    BEGIN
        //Sql scripts here ;
    END
    

    可以在here找到分步指南。

    赞美归于拉胡尔巴布。

    【讨论】:

    • 我没有投反对票,我只是在评论管道上查看了答案
    猜你喜欢
    • 1970-01-01
    • 2011-01-07
    • 2018-05-04
    • 2019-03-02
    • 2012-05-20
    • 1970-01-01
    • 2016-02-17
    • 2019-08-31
    相关资源
    最近更新 更多