r/SQLServer • • Mar 17 '20

Automating Complex SQL Server Infrastructures

On the last project I was involved with we had a pretty complex SQL Server setup. 5 clusters. Over 40 individual databases. Each with multiple Linked Servers to each other (old various legacy names too). Transactional replication going all over the place. Databases replicated across servers with different names on the remote side. Stored Procedures that did cross database and cross linked server joins. A real mess. A 25 year old gigantic app that had grown out of control with no planning. Duplicated in 5 different environments (prod and pre-prod environments).

As part of my efforts to get this mess under control, I developed a utility that would enable us to reflect this mess in our CI/CD pipeline, and our Unit Tests. So we could replicate the mess, make changes to it, deploy those changes, and begin fixing and solving issues. Also, so we could develop unit tests that executed stored procedures. Basically, code to deploy a cluster of servers, configure them, and then deploy the databases, which we got under source control in SSDT (DACPACs).

Somebody on Reddit two weeks ago reminded me about this.

So I reimplemented a bunch of it.

https://github.com/wasabii/Cogito.SqlServer.Deployment

The basic idea here is that SqlDeployment takes an XML manifest file that describes your instances, their configuration, replication settings, loads it, then executes it. If you're pointing to (local)\DBINSTANCE, it will install it from setup.exe. If it's configured to point to (localdb)\INSTANCENAME it will bring LocalDB instances online. If pointing to a remote instance, it will simply connect to it. It will deploy DACPACs as specified. It will work in parallel, deploying multiple databases at the same time, depending on declared dependencies between them. And it's resumable, so you can cancel progress, and then restart it.

This makes it usable from within a CI/CD pipeline to bring an existing SQL Server infrastructure up to date. It makes it usable on developer machines to install the SQL Server infrastructure locally. It makes it useful to bring online new environments. And with it's LocalDB support, it makes it useful to use inside Unit Tests (provide SqlDeploymentPlan from the container, execute targets as configuration for EF core is acquired, etc).

You store the manifest in the source code. You execute it as part of your CI/CD pipeline. Or you execute it in your unit test setup. It's like a build script for SQL. You can build a C# project that contains and wraps the SqlDeployment instance. I also support ProjectReferences to SSDT projects from C# projects; importing the generated DACPACs into the project output. So you can build a single executable that deploys your infrastructure.

This goes way beyond DACPACs alone.

There is also a .NET Core Tool that you can install: Cogito.SqlServer.Deployment.Tool; this provides a sqldeploy command that can publish a manifest.

Here's an example of what a complex deployment might look like: https://github.com/wasabii/Cogito.SqlServer.Deployment/blob/develop/docs/examples/4_servers_with_replication.txt

28 Upvotes

0 comments sorted by