如何根据部署配置文件运行不同的前后 SSDT 发布脚本 [英] How to run different pre and post SSDT pubish scripts depending on the deploy profile

查看:43
本文介绍了如何根据部署配置文件运行不同的前后 SSDT 发布脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Visual Studio 2013 中使用 SSDT.

我为开发服务器创建了一些发布前和发布后脚本.预部署脚本从表中清空数据并重新设置所有自动标识字段.部署后脚本使用静态数据和示例数据填充表.

现在我需要将数据库发布到我们的临时数据库服务器和实时数据库服务器.我为这些服务器创建了新的publish.xml"配置文件.但显然我不希望运行相同的前后脚本.

如何根据发布配置文件指定不同的脚本,或者让脚本了解目标并执行不同的操作.

我最担心的是发布到实时服务器并意外破坏数据.

提前致谢.

道格

解决方案

您有几个选择:

1 - 将您的数据更改包装在对@servername 或环境独有的调用中,这样您就可以得到如下内容:

if @@servername = 'dev_server'开始删除废话插入废话结尾

2 - 您也可以使用 sqlcmd 变量实现类似的功能,传入一个名为/v:DestoryData=true"的变量,然后您可以在脚本中引用它.

3 - 不要使用部署前/部署后脚本,而是使用自己的机制来运行它们,即使用批处理文件来部署 dacpacs 并在之前和之后添加对 sqlcmd 的调用 - 这样做的缺点是在部署时,对表的更改会导致任何外键在部署前被禁用,并在部署后重新启用.

4 - 编辑 dacpac,部署前/部署后脚本只是 dacpac 内的文本文件,它本质上是一个遵循 microsoft 打包格式的 zip 文件,并且有一个 .net 打包 api 可让您修改它.

我想大概就这些了,有什么不清楚的请追问:)

编辑

I am using SSDT in Visual Studio 2013.

I have created some pre and post publish scripts for the development server. The pre-deployment scripts empty data from tables and re-set all the auto identity fields. The post-deployment scripts populate the tables with static and sample data.

Now I need to publish the database to our staging and live database servers. I have created new "publish.xml" profiles for these servers. But obviously I don't want the same pre and post scripts to run.

How can I either specify different scripts depending on the publish profile, or make the scripts aware of the target and perform different actions.

My biggest concern is publishing to the live server and accidentally destroying data.

Thanks in advance.

Doug

解决方案

You have a few options:

1 - Wrap your data changes in calls to @servername or something unique to the environment so you would have something like:

if @@servername = 'dev_server'
begin
     delete blah
     insert blah

end

2 - You can also achieve something similar using sqlcmd variables, pass in a variable called "/v:DestoryData=true" or something and then you can reference that in your script.

3 - Don't use pre/post deploy scripts but have your own mechanism for running them i.e. use a batch file to deploy your dacpacs and add a call to sqlcmd before and after - the downside to this is that when deploying, changes to a table result in any foreign keys being disabled before the pre-deploy and re-enabled after the post-deploy.

4 - Edit the dacpacs, the pre/post deploy scripts are just text files inside the dacpac which is essentially a zip file that follows the microsoft packaging format and there is a .net packaging api to let you modify it.

I think that is about it, please ask if anything is unclear :)

ed

这篇关于如何根据部署配置文件运行不同的前后 SSDT 发布脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆