如何禁用DacServices.Deploy()中的部署前部署和后部署脚本 [英] How to disable predeployment and postdeployment scripts in DacServices.Deploy()

查看:203
本文介绍了如何禁用DacServices.Deploy()中的部署前部署和后部署脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一些自动化的dacpac部署代码,可以使用 Microsoft.SqlServer.Dac


正确处理CreateNewDatabase和简单的Update数据库方案

现在,在CreateNewDatabase的情况下,我们希望能够运行 DacServices.Deploy(),同时禁用了Pre和Post部署脚本。即在这种情况下,不应该执行它们。



我尝试在 DacDeployOptions DacServices 对象,但找不到任何会这样做的东西。我真的



问题1 我想要一些类似 DacDeployOptions.IgnorePreDeploymentScript = true 有什么办法可以在运行时实现这一点吗?


另外,前一段时间我还记得看到示例代码,它显示了如何遍历一个dacpac,并在运行时创建一个新的dacpac。我认为这种方法将允许我简单地创建一个新的dacpac,我可以传递给部署,并排除前后部署脚本。我不喜欢这个解决方案,但这样可以让我实现我所需要的。



问题2 :有一些例子吗?



我的代码:

  var dacService = new DacServices(ConstDefaultConnectionString); 
使用(var dacPackage = DacPackage.Load(dacPacFilePath))
{
var deployOptions = new DacDeployOptions
{
CreateNewDatabase = true,
IncludeTransactionalScripts = false
};
dacService.Deploy(dacPackage,TestDatabaseName,true,deployOptions);
}

问题与以下内容有关:从Visual Studio SQL项目创建LocalDB以进行测试

解决方案

有一些方法可以采取这个,这是一个大脑转储(呃昨天晚上回来了,我甚至不确定当前时间):



1)创建一个空项目,使用相同的数据库引用引用您的主项目 - 当部署时,没有脚本使用IncludeCompositeObjects部署空的项目 - 前/后部署脚本只能从您部署的dacpac运行,而不是从任何引用的dacpacs运行,但显然代码和方案已部署。这描述了:



https://the.agilesql.club/blog/Ed-Elliott/2016-03-03/Post-Deploy-Scripts-In-Composite -Dacpac不部署



2)使用SQLCMD变量包装数据设置,并将值传递给部署。



3)使您的脚本检查是否应该设置数据,如果表rowcount为零,则插入参考数据

4)使用合并脚本 - 我不清楚这是为了参考数据还是设置测试数据



5)使用.net打包api删除前/从dacpac部署脚本,这将显示如何编写脚本,以便您能够执行GetPart而不是WritePart:



https:// github.com/GoEddie/Dir2Dac/blob/master/src/Dir2Dac/DacCreator.cs



总的来说,我猜这可能是一个更简单的解决方案 - 如果这是用于测试,那么可能使数据设置成为测试设置的一部分?如果您是单元测试,tSQLt可以通过使用FakeTable来帮助您避免所有这些。



希望它有助于:)



Ed


We have some automated dacpac deployment code which correctly handles both a CreateNewDatabase and straightforward Update database scenarios in C# using Microsoft.SqlServer.Dac

Now in the CreateNewDatabase case we want to be able to run the DacServices.Deploy() with both the Pre and Post Deployment scripts disabled. I.e. they should not be executed in this scenario.

I have tried to find a suitable place in the DacDeployOptions and DacServices objects but cannot find anything that will do this.Ideally

Question 1: I would like something like DacDeployOptions.IgnorePreDeploymentScript = true Is there any means by which I could achieve this at runtime?

As an alternative, some time ago I remember seeing example code which showed how to traverse a dacpac and create a new dacpac in run time. I think this approach would allow me to simply create a new dacpac which I could pass to the Deploy and which would exclude the Pre and Post Deployment scripts. I don't like this solution but it would allow me to achieve what I need.

Question 2: Can anyone point me to some examples for this please?

My code:

var dacService = new DacServices(ConstDefaultConnectionString);
using (var dacPackage = DacPackage.Load(dacPacFilePath))
{
    var deployOptions = new DacDeployOptions 
    { 
       CreateNewDatabase = true, 
       IncludeTransactionalScripts = false
    };
    dacService.Deploy(dacPackage, TestDatabaseName, true, deployOptions);
}

The question is related to: Create LocalDB for testing from Visual Studio SQL project

解决方案

There are a number of approaches you can take for this, this is a bit of a brain dump (hey the clocks went back last night and I'm not even sure if the current time):

1) create an empty project that references your main project using a same database reference - when you deploy without the scripts deploy the empty one using IncludeCompositeObjects - pre/post deploy scripts are only run from the dacpac you deploy not from any referenced dacpacs but obviously the code and scheme are deployed. This describes it:

https://the.agilesql.club/blog/Ed-Elliott/2016-03-03/Post-Deploy-Scripts-In-Composite-Dacpac-not-deploying

2) use SQLCMD variables to wrap the data setups and pass in the value to the deploy.

3) make your scripts check for whether they should setup data like only insert if the table rowcount is zero

4) for reference data use merge scripts - I'm not clear if the point of this is for reference data or setting up test data

5) Use .net packaging api to remove the pre/post deploy scripts from the dacpac, this shows you how to write the scripts so you should be able to do a GetPart rather than WritePart:

https://github.com/GoEddie/Dir2Dac/blob/master/src/Dir2Dac/DacCreator.cs

On the whole I would guess that there is probably a simpler solution- if this is for testing then maybe make the data setup part of the test setup? If you are unit testing tSQLt helps you avoid all this by using FakeTable.

Hope it helps :)

Ed

这篇关于如何禁用DacServices.Deploy()中的部署前部署和后部署脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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