1

I'm working on integrating SQL Server databases into our in-house version control/deployment utility, which is built with powershell,and uses Github as a repository.

Using the excellent sqlpackage.exe utility, I have been able to add a process whereby a developer can extract their current changes into a dacpac and store it in Github, then do the opposite in reverse when they want to get the latest version. However, because the .dacpac is a binary file, it's not possible to see differences in git. I have mitigated this somewhat by unzipping the dacpac before storing in in source control, so contained xml files are added instead. However, even though these files are text-based, they are still not easy to look through and find differences.

What I would like to do, is convert the dacpac into a folder structure similar to what would be seen in SSMS (with all the database objects such as triggers, sprocs etc in their respective folders), store that in Github, and then convert it back into a dacpac when a client checks out the code. However, there doesn't seem to be any function in sqlpackage.exe for this, and I can't find any documentation. Is there any command line tool I can use to this through Powershell?

4
  • Off topic but why use a home built system when you could just use sqlpackage and the dacpac deployment system?
    – Ed Elliott
    Mar 2, 2018 at 19:08
  • Sorry, Ed I should clarify -- I am using sqlpackage and the dacpac deployment system. My only issue is dacpac is a binary file, but I want to store my schema in git as a collection of database objects (tables, sprocs) similar to the folder structure for databases in SSMS. I'm just looking for a way to convert the extracted dacpac into this structure.
    – Daryl1976
    Mar 2, 2018 at 20:54
  • 2
    Is there a reason not to just use the SQLProj in SSDT that does this for you?
    – nschonni
    Mar 4, 2018 at 7:02
  • Our team uses and is comfortable using SSMS for database development, rather than VS, so we're looking for a command line implementation that would allow us to continue with our current workflow
    – Daryl1976
    Mar 5, 2018 at 22:28

2 Answers 2

2

Using the public APIs for DacFx you can load the dacpac, iterate over all objects, and script each one out. If you're willing to write your own code you could write each one to its own file based on the object type. The basic process is covered in the model filtering samples in the DacExtensions Github project. Specifically you'll want to do something like the ModelFilterer code that loads a dacpac, queries all objects, scripts them out - see the CreateFilteredModel method. I've put a sample that should mostly work below. Once you have this, you can easily do compare on a per-object basis.

using (TSqlModel model = new TSqlModel(dacpacPath))
{
    IEnumerable<TSqlObject> allObjects = model.GetObjects(QueryScopes);
    foreach (TSqlObject tsqlObject allObjects)
    {
        string script;
        if (tsqlObject.TryGetScript(out script))
        {
            // Some objects such as the DatabaseOptions can't be scripted out.

            // Write to disk by object type
            string objectTypeName = tsqlObject.ObjectType.Name;
            // pseudo-code as I didn't bother writing.
            // basically just create the folder and write a file
            this.MkdirIfNotExists(objectTypeName);
            this.WriteToFile(objectTypeName, tsqlObject.Name + '.sql', script);
        }
    }
}

This can be converted into a powershell cmdlet fairly easily. The dacfx libraries are on nuget at https://www.nuget.org/packages/Microsoft.SqlServer.DacFx.x64/ so you should be able to install them in PS and then use the code without too much trouble.

1
  • 4
    This method works -- I have created cmdlets for converting a dacpac into a sql file structure, and then converting them back into a dacpac, and have put the code in the following github repo github.com/DarylSmith/DacpacToSqlCmdlet
    – Daryl1976
    Mar 5, 2018 at 22:25
0

Based on the other post I was able to get a script working. Caveat is you'll have to try the types till you get what you want... The way it is no it trys to put the full http or https value for some of the objects.

param($dacpacPath = 'c:\somepath' Debug', $dacpac = 'your.dacpac')
Add-Type -Path 'C:\Program Files (x86)\Microsoft SQL Server\120\DAC\bin\Microsoft.SqlServer.Dac.dll'
add-type -path 'C:\Program Files (x86)\Microsoft SQL Server\120\DAC\bin\Microsoft.SqlServer.Dac.Extensions.dll'
cd $dacpacPath
$dacPack = [Microsoft.SqlServer.Dac.DacPackage]::Load(((get-item ".\$dacpac").fullname))
$model =[Microsoft.SqlServer.Dac.Model.TSqlModel]::new(((get-item ".\$dacpac").fullname))
$queryScopes = [Microsoft.SqlServer.Dac.Model.DacQueryScopes]::All
$return = [System.Collections.Generic.IEnumerable[string]]
$returnObjects = $model.GetObjects([Microsoft.SqlServer.Dac.Model.DacQueryScopes]::All)
$s = ''
foreach($r in $returnObjects)
{
   if ($r.TryGetScript([ref]$s))
   {
    $objectTypeName = $r.ObjectType.Name;
    $d="c:\temp\db\$objectTypeName"
    if(!(test-path $d ))
    {
        new-item $d -ItemType Directory
    }
    $filename = "$d\$($r.Name.Parts).sql"

    if(! (test-path $filename))
    {new-item $filename -ItemType File}
    $s | out-file  $filename -Force
    write-output $filename
   }

}
6
  • Thanks Thom, but this just expands the dacpac, and extracts the xml files (like model.xml), which is what I am currently doing and adding to github. What I am looking for is not just extracting the files in the dacpac, but actually converting them into individual sql data objects (triggers, sprocs,tables) in a file structure
    – Daryl1976
    Mar 2, 2018 at 20:52
  • The best thing I can think of is to Automate the SLN Creation you can do in Visual Studio. To do what you are looking for.. You can create a solution and import your bacpac. It then creates sql files for the changes in the dacpac. Mar 2, 2018 at 21:32
  • This does extract the file, but it basically builds a deploy script, rather than a collection of sql objects
    – Daryl1976
    Mar 5, 2018 at 22:26
  • You've inspired me to blog about this @Daryl1976 so posted an article on my site: powershellposse.com/2018/03/06/… Mar 6, 2018 at 18:43
  • Yah thanks for the challenge this to is something I've always wanted as well. If you find something wrong in my code or something that you've found to work better please do share. Mar 8, 2018 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.