If you write code to SQL Server then you might be interested in this: at the end of 2017 I wrote a tSQLt tdd training course which has helped over 300 people learn both tSQLt and how to apply TDD practices to their SQL Server T-SQL development, you can join the course at https://courses.agilesql.club. The course is free if you are happy to wait 10 weeks to complete it, with 1 lesson being made available per week - if you are in more of a hurry or you would like to help support the project you can purchase the course which makes it instantly available to you.
Deploying a dacpac from powershell should be pretty easy, there is a .net api which you can use to open a dacpac, compare to a database and either create a script or get the database to look the same as the dacpac but there are a couple of problems with the approach.
Problem 1 - Is the DacFx installed?
The first problem is whether the DacFx is installed, the chances are if Visual Studio or SQL Server have been installed then it will be there.
Problem 2 - Where is the DacFx installed?
Once you know that the DacFx is available, where is it? Depending how you installed it, whether when you installed Visual Studio or via an MSI it will be installed in a different location. Further if you get a recent build it will be in the 130 folder, older builds the 120 or 110 folder. Which one do you have??
So what I used to do to combat these first two issues is to check into my test projects the DAC folder which includes sqlpackage and just shell out to that to do the install, this works but updates to SSDT come between every 1 and every 3 months, the projects I have done this on are all on old builds of the DacFx and probably will be until something goes wrong and someone updates it. That sucks :(
It was with great excitement, and I don’t say that lightly, that in July the SSDT team announced that they would be maintaining a nuget package of the DacFx. This is really exciting because it means that problem 1 and 2 no longer exist, we can simply reference the nuget package and keeping up to date is pretty simple. While you recover from the excitement that is the DacFx in a nuget package I have something else to get you really excited…
This means no install to Program Files
I know right, exciting! What this means is that even on hosted build servers (vsts for example) where we don’t have admin rights we can still keep up to date with recent copies of the DacFx without having to commit the source control sin of checking in libraries.
Problem 3 :( Deployment Contributors
If we no longer need admin rights to use the DacFx it means we no longer can rely on admin rights to deploy dacpacs - this means that deployment contributors cannot be copied to program files which is where the DacFx loads contributors from. Remember also that contributors are specifically loaded from the program files (or x86) version of SQL Sever or Visual Studio or whatever version of SSDT you have so it could be from any one of the version DAC folders i.e. 110, 120, 130, 1xx etc.
There is a solution? Well yes of course otherwise I wouldn’t have been writing this! DacFxed is a powershell module which:
- 1. References the DacFx nuget package so updating to the latest version is simple
- 2. Implements a hack (ooh) to allow contributors to be loaded from anywhere
- 3. Is published to the powershell gallery so to use it you just do “Install-Module -Name DacFxed -Scope User -Force”
- 4. Has a Publish-Database, New-PublishProfile and Get-DatabaseChanges CmdLets
Cool right, now a couple of things to mention. Firstly this is of course open source and available: https://github.com/GoEddie/DacFxed
Secondly, what is up with the cool name? Well I did’t want to call the module DacFx as I was publishing it to the powershell gallery and hope that one day maybe the SSDT team will want to create a supported powershell module that publishes dacpac’s and didn’t want to steal the name. DacFxed is just DacFx with my name appended, what could be cooler than that?
In the list I mention that it uses a hack to allow contributors to be loaded, lets talk some more about this.
Currently to use a deployment contributor you either need to copy it into the Program Files directory or use sqlpackage and put it in a sub-folder called Extensions - neither of these two options are particularly exciting. I needed a better way to be able to include a deployment contributor in some user writable folder and then load the dll’s from there. I hope (there is a connect somewhere) that one day the SSDT team will give us an option when using the DacFx to say where to load contributors from - when that happens I commit here to modify this package to support their method so if you do use this then fear not, I will make sure it stops using a hack as soon as possible.
What is this so called hack?
When the DacFx tries to load deployment contributors it does a search of a number of well known directories to find the dll’s, it also has a fairly unique way to determine which folder it should use when being called from visual studio - what happens is that it checks whether two folders above the folder the dll is in, there is a file called “Microsoft.VisualStudio.Data.Tools.Package.dll” - if this file exists then it searches in the folder the dll is in to find out if there are any deployment contributors to load. The interesting thing about this is that it doesn’t actually load the file, just checks the existence of it - if it exists it searches itself for extensions. So if we have this structure:
Folder 1\Microsoft.VisualStudio.Data.Tools.Package.dll (this can be an empty text file)
Folder 1\Folder 2\folder 3\DacFx Dll’s
When you load the DacFx dll’s from this folder (to be specific “Microsoft.Data.Tools.Schema.Sql.dll”) we get the ability to load contributors from user writable folders (which is the end goal for this).
Well no, it would be if it wasn’t for the way .net resolved assemblies and powershell CmdLets. If our powershell module is structured like this:
WindowsPowershell\Modules\DacFxed\Microsoft.VisualStudio.Data.Tools.Package.dll (remember this is an empty text file)
What would happen is that out Cmdlet.dll would try to resolve the DacFx and it would not find it as .net doesn’t search every sub-folder of the current directory to find dll’s to load. If .net can’t find the dll locally it will search horrible things like the GAC and if it finds the dll there, load it. This means our sneaky trick to trick the DacFx to load our extensions doesn’t work.
What I came up with is a DacFxLoadProxy and DaxFxProxy, the CmdLet.dll has a reference to DacFxLoadProxy. DacFxLoadProxy does a Assembly.Load on each of the DacFx dll’s in the bin\dll folder and then overrides Assemnbly.Resolve and tries to load the DacFxProxy.dll. Loading the DacFxProxy causes the DacFx dll’s to be loaded and the DacFxLoadProxy passes the dll’s that it has already loaded from the correct place.
Phew, I said hack :)
This sounds dangerous
Well yes and no, yes it is a little exotic but no in that if you tell the DacFx to load a contributor if this process doesn’t work for some reason the worst thing that will happen is you get a “Deployment contributor could not be loaded” error - you won’t deploy to a database without contributor you weren’t expecting.
So no not really dangerous, just annoying if it doesn’t work. I have tests setup and a release pipeline for this that I will cover in another post that make it easy for me to ensure each update to the DacFx can be taken while this still works. If the SSDT team break this behaviour then I won’t deploy and then anyone using it can update in their own time.
How does it work?
You need a dacpac and a publish profile, if you have a dacpac and no publish profile then New-PublishProfile will create a template you can use to get started with.
Publish-Database -DacpacPath “path\to\dacpac” -PublishProfilePath “path\to\publish-profile”
Publish-Database -DacpacPath “path\to\dacpac” -PublishProfilePath “path\to\publish-profile” -verbose
Publish-Database -DacpacPath “path\to\dacpac” -PublishProfilePath “path\to\publish-profile” -verbose -DacFxExtensionsPath “c:\path\one;c:\path-two”
August 3, 2016 - 14:13
Deployment Contributor Locations
Could deployment contributors also be added to the GAC? Wouldn’t that be a somewhat less ugly kludge?
p.s. – I use “kludge” instead of “hack” because I prefer it’s origins, not to cast any disparagement :-).
August 3, 2016 - 21:29
Hi Andy - no I am afraid not
Hi Andy - no I am afraid not (I am about 99.1% sure but I haven’t tried it) the DacFx is explicit about where it loads extensions from or where it thinks it loads them from :)
August 4, 2016 - 19:03
Awesome as always
I really appreciate your ongoing work with things like this, Ed. These areas are somewhat foreign to me, but I know our ops team will appreciate things like this. I know that a lot of our current releases have been moving towards Nuget files and this should help quite a bit.
August 4, 2016 - 21:20
Thanks Peter that means a lot
Thanks Peter that means a lot :)