Mark Ku's Blog

The Problem

We have one system cloned into many separate sites, each with its own independent database. In the past, deploying database scripts meant manually running them against each database one by one. This post walks through how to use PowerShell to deploy a release's SQL scripts to all those databases in one shot.

Approach

PowerShell reads a database config file (site.json) and a SQL script manifest (sql.json), then uses sqlcmd to execute the scripts against each database.

Install the sqlcmd Utility (SQL Server command-line tool)

  1. Download from the official documentation: https://docs.microsoft.com/zh-tw/sql/tools/sqlcmd-utility?view=sql-server-ver15

  2. Open PowerShell and test that sqlcmd works:

& sqlcmd -S "(local)\instance1" -U a -P a -i "c:\temp\sql.sql"

# To call a Win32 executable you want to use the call operator & like this:

Create Config Files

  1. Create a database config file (site.json)
{
	"servers": [
		{
			"name": "site1",
			"server": "192.168.1.32",
			"db": "CMS",
			"user": "mark",
			"password": "123",
			"switch": "on"
		},
		{
			"name": "site2",
			"server": "192.168.1.31",
			"db": "CMS2",
			"user": "mark",
			"password": "123",
			"switch": "on"
		}
	]
}
  1. Create a SQL script manifest (sql.json)
{
	"20210605": [
		{
			"filename": "1.sql",
			"desc": "create customer table"
		},
		{
			"filename": "2.sql",
			"desc": "create customer2 table"
		}
	],
	"20210608": [
		{
			"filename": "3.sql",
			"desc": "create customer3 table"
		},
		{
			"filename": "4.sql",
			"desc": "create customer4 table"
		}
	]	
}

Write the PowerShell Script

$sitejson = Get-Content './site.json' | Out-String | ConvertFrom-Json
$sqljson = Get-Content './sql.json' | Out-String | ConvertFrom-Json

$releaseNo = Read-Host 'Please Enter ReleaseNo'
Write-Host $sqljson."$releaseNo"

foreach ($site in $sitejson.servers)
{    
	$n = $site.name
	$s = $site.server
    $d = $site.db
    $u = $site.user
    $p = $site.password
	$switch = $site.switch
	# Write-Host "$s,$d,$u,$p "
		
	if($switch -eq 'on')
	{	
	 Write-Host "Start DB deploy - $n($s)"	 
	foreach ($sql in $sqljson."$releaseNo")
	{    
		$filename=$sql.filename;
		Write-Host "execute $filename"	 
		
		& sqlcmd -S "$s" -d "$d" -U $u -P $p -f 950 -i  "./$filename" 	 	 
	 
	}	
	Write-Host "Finish DB deploy - $n($s)"	
	Write-Host ""
	}
}

pause

How to Use

  1. Right-click RunSql.ps1 and select "Run with PowerShell". File Explorer context menu highlighting Run with PowerShell option
  2. Enter the Release No you want to deploy, as defined in sql.json. PowerShell window prompting for ReleaseNo 20210605
  3. Successful SQL script execution. PowerShell console output showing successful SQL script deployment
    Note: Failed SQL script execution looks like this: PowerShell console showing SQL script execution failed due to existing objects

Author

Mark Ku

擁有 10+ 年經驗的資深軟體工程師,現為 AI 應用 Builder,專注於大型平台架構與簡化複雜系統設計,從電商系統到訂閱與收費平台,結合 AI Agent、AI 整合與自動化開發,打造高效率且可持續演進的產品技術基礎。Read More

Found this useful?

The author's free tools, daily podcasts and newsletter are all here.

Mark Ku · This article is licensed under CC BY 4.0. Credit the author and link back to the original when reusing it.

Comments

Subscribe to Newsletter

Subscribe to get new posts delivered instantly — never miss a tech share.

By submitting, you agree to receive emails. You can anytime.

Popular Posts

View all
Mark Ku
··602

Oracle Cloud Always Free Tier: Linux Host and Static IP for a $0 Cloud Solution

Oracle Cloud Always Free Tier: Linux Host and Static IP for a $0 Cloud Solution
Mark Ku
··490

Say Goodbye to Postman's Fee Trap! A Hands-on Guide to Bruno, the Open-Source Git-Native API Testing Powerhouse.

Say Goodbye to Postman's Fee Trap! A Hands-on Guide to Bruno, the Open-Source Git-Native API Testing Powerhouse.
Mark Ku
··333

A Free, Open-Source, Notion-like Knowledge Base — A Complete Guide to Deploying and Backing Up Outline Wiki

A Free, Open-Source, Notion-like Knowledge Base — A Complete Guide to Deploying and Backing Up Outline Wiki
Mark Ku
··264

Training Your Own AI Voice: Hardware Requirements, Open-Source Model Comparison, and LoRA Fine-Tuning

Training Your Own AI Voice: Hardware Requirements, Open-Source Model Comparison, and LoRA Fine-Tuning
Mark Ku
··221

Building an Efficient API Management Platform: Deploying Kong Gateway from Scratch - Part 1

Building an Efficient API Management Platform: Deploying Kong Gateway from Scratch - Part 1
Mark Ku
··215

Setting Up Samba on Ubuntu to Share Folders with Windows 11

Setting Up Samba on Ubuntu to Share Folders with Windows 11