<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>DacFX on gavincampbell.dev</title>
		<link>https://gavincampbell.dev/tags/dacfx/</link>
		<description>Recent content in DacFX on gavincampbell.dev</description>
		<generator>Hugo 0.121.2</generator>
		<language>en-gb</language>
		<managingEditor>gavin@gavincampbell.dev (Gavin Campbell)</managingEditor>
		<webMaster>gavin@gavincampbell.dev (Gavin Campbell)</webMaster>
		<copyright>
Except as otherwise noted, all blog posts, photos, graphics, presentations and other media on &lt;a property=&#34;dct:title&#34; rel=&#34;cc:attributionURL&#34; href=&#34;https://gavincampbell.dev&#34;&gt;gavincampbell.dev&lt;/a&gt; by &lt;a rel=&#34;cc:attributionURL dct:creator&#34; property=&#34;cc:attributionName&#34; href=&#34;https://gavincampbell.dev&#34;&gt;Gavin Campbell&lt;/a&gt; are licensed under &lt;a href=&#34;http://creativecommons.org/licenses/by/4.0/?ref=chooser-v1&#34; target=&#34;_blank&#34; rel=&#34;license noopener noreferrer&#34; style=&#34;display:inline-block;&#34;&gt;Attribution 4.0 International&lt;/a&gt;. Except as otherwise noted, all code snippets on &lt;a property=&#34;dct:title&#34; rel=&#34;cc:attributionURL&#34; href=&#34;https://gavincampbell.dev&#34;&gt;gavincampbell.dev&lt;/a&gt; by &lt;a rel=&#34;cc:attributionURL dct:creator&#34; property=&#34;cc:attributionName&#34; href=&#34;https://gavincampbell.dev&#34;&gt;Gavin Campbell&lt;/a&gt; are licensed under &lt;a href=&#34;http://creativecommons.org/publicdomain/zero/1.0?ref=chooser-v1&#34; target=&#34;_blank&#34; rel=&#34;license noopener noreferrer&#34; style=&#34;display:inline-block;&#34;&gt;CC0 1.0 Universal&lt;/a&gt;. Built with the &lt;a href=&#34;https://github.com/spookey/slick&#34;&gt;Slick&lt;/a&gt; theme for &lt;a href=&#34;https://gohugo.io/&#34;&gt;Hugo&lt;/a&gt;, hosted in the UK by &lt;a href=&#34;https://www.mythic-beasts.com//&#34;&gt;Mythic Beasts&lt;/a&gt;</copyright>
		<lastBuildDate>Mon, 17 Dec 2018 00:00:00 +0000</lastBuildDate>
		<atom:link href="https://gavincampbell.dev/tags/dacfx/index.xml" rel="self" type="application/rss+xml"/>
		<item>
			<title>Filtering Dacpac deployments with DacFX and Powershell</title>
			<link>https://gavincampbell.dev/post/filtering-dacpac-deployments-dacfx/</link>
			<pubDate>Mon, 17 Dec 2018 00:00:00 +0000</pubDate>
			<author>gavin@gavincampbell.dev (Gavin Campbell)</author>
			<guid isPermaLink="true">https://gavincampbell.dev/post/filtering-dacpac-deployments-dacfx/</guid>
			<description>&lt;p&gt;Prompted by some discussion on the &lt;a href=&#34;https://sqlps.io/slack/&#34;&gt;SQL Community Slack&lt;/a&gt;, I thought I&amp;rsquo;d revisit &lt;a href=&#34;https://blogs.msdn.microsoft.com/ssdt/2016/09/20/schema-compare-in-sqlpackage-and-the-data-tier-application-framework-dacfx/&#34;&gt;this old post&lt;/a&gt; on the SSDT Team Blog which outlines how to filter specific objects from a dacpac deployment using the &lt;a href=&#34;https://docs.microsoft.com/en-us/dotnet/api/microsoft.sqlserver.dac.compare?view=sql-dacfx-140.3881.1&#34;&gt;Schema Compare API&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In the past, I&amp;rsquo;ve used &lt;a href=&#34;https://the.agilesql.club/&#34;&gt;Ed Elliott&amp;rsquo;s&lt;/a&gt; &lt;a href=&#34;https://github.com/GoEddie/DeploymentContributorFilterer&#34;&gt;filtering deployment contributor&lt;/a&gt; for this kind of thing, but in the interest of experimentation I thought I&amp;rsquo;d have a look at what comes &amp;ldquo;in the box&amp;rdquo;, not least because deployment contributors can, ironically, be a bit of a pain to &lt;em&gt;deploy&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ve created a &lt;a href=&#34;https://github.com/gavincampbell-dev-example-repos/FilteringDemo/tree/master/FilteringDemo&#34;&gt;simple project&lt;/a&gt; you can use to play along at home, with two schemas, named &lt;code&gt;Production&lt;/code&gt; and &lt;code&gt;UnProduction&lt;/code&gt;, each containing a single table. I&amp;rsquo;ve also included a publish profile that can be used to publish the project to a local database called &amp;ldquo;Unfiltered&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;In this example, we&amp;rsquo;ll use the schema compare API to allow only the &lt;code&gt;Production&lt;/code&gt; schema and the table it contains to deployed.&lt;/p&gt;
&lt;p&gt;The action takes place in the powershell script &lt;a href=&#34;https://github.com/gavincampbell-dev-example-repos/FilteringDemo/blob/master/FilteringDemo/FilteredDeploy.ps1&#34;&gt;Filtered Deploy.ps1&lt;/a&gt;, which I&amp;rsquo;ve reproduced in its entirety here:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-powershell&#34; data-lang=&#34;powershell&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$serverName = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;localhost&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$databaseName = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Filtered&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$schemaToInclude = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Production&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$dacFxDll=&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;C:\Program Files (x86)\Microsoft SQL Server\140\DAC\bin\Microsoft.SqlServer.Dac.Extensions.dll&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$dacpacPath = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;$PSScriptRoot&lt;span style=&#34;color:#e6db74&#34;&gt;\bin\Debug\FilteringDemo.dacpac&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Add-Type -Path $dacFxDll
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$sourceDacpac = New-Object Microsoft.SqlServer.Dac.Compare.SchemaCompareDacpacEndpoint($dacpacPath);
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$targetDatabase = New-Object Microsoft.SqlServer.Dac.Compare.SchemaCompareDatabaseEndpoint(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Data Source=&lt;/span&gt;$serverName&lt;span style=&#34;color:#e6db74&#34;&gt;;Initial Catalog=&lt;/span&gt;$databaseName&lt;span style=&#34;color:#e6db74&#34;&gt;;Integrated Security=True;&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$comparison = New-Object Microsoft.SqlServer.Dac.Compare.SchemaComparison($sourceDacpac, $targetDatabase)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$comparisonResult = $comparison.Compare()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$comparisonResult.Differences | %{
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;	&lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt;( $_.SourceObject.name.parts[&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;] &lt;span style=&#34;color:#f92672&#34;&gt;-ne&lt;/span&gt; $schemaToInclude){
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;		Write-Output &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Excluding Object &lt;/span&gt;$($_.SourceObject.name)&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;		$comparisonResult.Exclude($_) | Out-Null
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;	}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$publishResult =  $comparisonResult.PublishChangesToTarget();
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; ($publishResult.Success){
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;	Write-Output &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Worky&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;else&lt;/span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;	Write-Output &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;NoWorky&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Briefly, we load the dll that contains the classes we need to use, and set up a schema comparison between a source dacpac and a target database. There are two key pre-requisites for this, which are&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the dacpac must exist, meaning that the project must have been built&lt;/li&gt;
&lt;li&gt;the target database must exist, meaning that this script can&amp;rsquo;t be used for &amp;ldquo;first-time&amp;rdquo; deployment. &lt;em&gt;This is a departure from the &amp;ldquo;usual way&amp;rdquo; of working with SSDT, and IMV the biggest limitation of this approach.&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Moving on, we execute the comparison using &lt;code&gt;SchemaComparison.Compare()&lt;/code&gt;, then iterate the list of differences looking for source objects that match - or in this case don&amp;rsquo;t match - our filter.  This kind of thing is useful for excluding objects that only need to exist in dev and test environments.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s perfectly valid to do this comparison the other way around, examining objects in the target database - indeed this is what the C# snippet in the original blog post does. This would be useful if there were objects in your target database created by a third party outside of your deployment process that needed to be specifically excluded from consideration.&lt;/p&gt;
&lt;p&gt;Having excluded the relevant objects from the comparison results, we go ahead and deploy the changes with &lt;code&gt;PublishChangesToTarget()&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-powershell&#34; data-lang=&#34;powershell&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;~\source\repos\FilteringDemo\FilteringDemo [master &lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;≡&lt;/span&gt;]&amp;gt; .\FilteredDeploy.ps1
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Excluding Object [&lt;span style=&#34;color:#66d9ef&#34;&gt;UnProduction].[UnimportantStuff&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Excluding Object [&lt;span style=&#34;color:#66d9ef&#34;&gt;UnProduction&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Worky
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;~\source\repos\FilteringDemo\FilteringDemo [master &lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;≡&lt;/span&gt;]&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;figure&gt;&lt;img src=&#34;dbs.png&#34;
         alt=&#34;picture of the two databases&#34;/&gt;&lt;figcaption&gt;
            &lt;p&gt;picture of the two databases&lt;/p&gt;
        &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;This is a pretty simplistic example, it relies on the fact that both tables and schemas have the schema name in the first element of the &lt;code&gt;SourceObject.name.Parts[]&lt;/code&gt; collection, but more complex filtering criteria can be implemented with a bit of API speleology.&lt;/p&gt;
</description>
		</item>
	</channel>
</rss>
