Flatly-stacked
Flatly-stacked

Reputation: 649

VSTS Database Deployment to Azure - Loop a Task Group (or something like that)

I am trying to setup continuous deployment for a group of Azure databases that all share the same schema. In my situation, there are a number of dynamic databases that get created via copying and renaming a standard template. The software will make a copy of the CompanyTemplate database and rename it to Company_XXXX.

I would like to create a Task Group and/or a script in VSTS (hosted) that can query the master database, get a list of the company database names and then loop said Task Group in order to deploy the same schema and scripts to each of the Company databases that get created.

I have been Googling and testing odds and ends for days but I cannot find anything pertaining to how this can be done. Any thoughts? Is this possible?

Upvotes: 2

Views: 1763

Answers (2)

jessehouwing
jessehouwing

Reputation: 114621

There is no loop concept in the VSTS Build/Release environment.

There are a few workarounds that sprint to mind:

  • Run a powershell script and implement the logic there. Using the loop constructs in Powershell.
  • Run a powershell to trigger as many builds as you want using the REST API.

Upvotes: 2

Steve Brouillard
Steve Brouillard

Reputation: 3266

To begin with, I want to acknowledge that reading the answer from @jessehouwing triggered a few thoughts on my end.

As he mentions in his answer, there isn't anything that would directly do what you're asking. However, some techniques do come to mind, depending on how you want to deploy the databases.

  1. ARM Templates -
    1. Setup an ARM template that uses Resource Iteration to deploy multiple Azure SQL Databases. (See MS DOCS on how to do that). Configure the template to copy the schema of an existing DB to the new ones. You'll need that template DB deployed to Azure to act as the schema source. To configure the ARM template to create the new databases as a copy of the template, look at the createMode property of the SQL Database ARM template (SQL ARM Template documentation).
    2. Run a Powershell script that queries the master DB to get the list of companies (Query DB from Powershell).
    3. Output the results of the DB query to a VSTS variable and pass that variable into the ARM template to produce the databases.
  2. DACPAC -
    1. Create a DACPAC from a SQL DB Project in Visual Studio.
    2. You can either create a DACPAC that defines just the DB schema and use the ARM template technique above to run the DACPAC for each database you need in something of a hybrid technique - or
    3. You can create a dacpac that queries your main DB for the list of companies and creates a database for each one based on the defined schema. This options encapsulates the process of creating the schema and querying the main DB for the ones to create all into a single deployment artifact

Each option has its Pros and Cons. The ARM Template option is going to give you the most flexibility, but requires that you have a template DB in place to copy from.

The DACPAC option requires familiarity with using that technique for deploying databases and may still require an ARM template to make the process as flexible as possible. It does offer the potential to encapsulate all the DB deployment parts into a single step.

There are a fair number of variables here, but I think this should give you some options to consider that will take you in a workable direction.

Upvotes: 1

Related Questions