从迁移中创建存储过程。

huangapple go评论83阅读模式
英文:

Create stored procedure from migration

问题

我有一个使用GO语言的项目,它在应用程序启动时检查是否有新的迁移需要应用(我正在使用https://github.com/mattes/migrate库)。

现在的问题是,我有一个存储过程需要在其中一个迁移中创建(因为稍后会调用它,如果不在迁移中创建,测试套件当然会失败)。

一个示例可以是这样的:

DELIMITER @@
CREATE PROCEDURE get_value(my_id BIGINT(20), OUT out_value DOUBLE)
   BEGIN
     SELECT CASE
		WHEN o.financial_status = "test" THEN 0
		ELSE 1
	    END
	    INTO out_value
	  FROM `order` o
	  LEFT JOIN `my_table_2` io ON io.field_2 = o.id
	  LEFT JOIN `my_table_3` ip ON io.field_3 = ip.id
	  WHERE o.id = my_id;
END @@
DELIMITER ;

据我所读,这可能是不可能的,因为GO的SQL驱动程序不支持多语句(我们正在使用mysql 5.6)。

还有其他方法可以做到吗?谢谢!

英文:

I've a project in GO that check if there are new migrations to apply when the app starts (I'm using library https://github.com/mattes/migrate).

Now the problem is that I have a stored procedure to create in one of this migration (because this will be called later, and I need to create it in a migration otherwise the test suite will fail of course).

An example can be this one:

<!-- language: sql -->

DELIMITER @@
	CREATE PROCEDURE get_value(my_id BIGINT(20), OUT out_value DOUBLE)
	   BEGIN
	     SELECT CASE
			WHEN o.financial_status = &quot;test&quot; THEN 0
			ELSE 1
		    END
		    INTO out_value
		  FROM `order` o
		  LEFT JOIN `my_table_2` io ON io.field_2 = o.id
		  LEFT JOIN `my_table_3` ip ON io.field_3 = ip.id
		  WHERE o.id = my_id;
END @@
DELIMITER ;

As far as I read, this might not be possible because sql driver for go does not support multi statement (we're using mysql 5.6).

There are any other way to do that? Thanks!

答案1

得分: 1

我遇到了同样的情况。我运行的方法是删除了所有的分隔符声明,然后运行它。由于在迁移中不需要分隔符,它会自动终止(或实际上是命名),然后迁移应该会运行。

英文:

I ran into the same situation, actually. The way I ran it is I removed all delimiter declarations and ran it. Since you don't need a delimiter within the migration, it will auto terminate (or realistically denominate) and the migration should run.

huangapple
  • 本文由 发表于 2016年9月8日 18:24:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/39388467.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定