BuildSQL.ps1 3.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
################################################################################
#  BuildSQL.ps1
#  
#  This PoSH script will merge all .sql files in the folder that this .ps1 file is found, and all
#  subdirectories.  Specifically it runs the FILES first, then the FOLDERS in the given directory, 
#  alphabetically.  

#  Any folder prefaced with "_" (underscore) is skipped (including sub folders and files)
#
#  To Execute:
#    cd to the directory with BuildSQL.ps1
#    powershell .\BuildSQL.ps1 
#
#  Output:
#    PerformanceCollectorInstaller.sql.  This file will contain all objects for PerformanceCollector
# 
#  System Requirements: 
#    As with any PoSH script, you may need to run powershell Set-ExecutionPolicy Unrestricted first
#    MD3 requires sqlcmd to be in PATH, so you'll need to run this from a machine that has SQL installed on it.  
#
################################################################################

cls

# Always fail the PoSH script on ANY error
$script:ErrorActionPreference = "Stop"

$workingDirectory = (Split-Path $MyInvocation.MyCommand.Path)
$CurrentSQLScriptDirectory = $workingDirectory
$ScriptFile = "PerformanceCollectorInstaller.sql"
$ScriptFilePath = (Join-Path $workingDirectory $ScriptFile)

################################################################################
#     Helper Functions
################################################################################

function Recurse-Folders {
	param (
		[Parameter(Position=0, Mandatory=$true)] [string]$folderspec
	)
	
	## new method
	[string]$MergedOutput = "`n"
	
	# within a folder, process FILES first, then subfolders
	foreach ($sqlScript in Get-ChildItem -Path $folderspec -Filter "*.sql" | Sort-Object Name) {

		$sqlScript = $sqlScript.FullName
		$MergedOutput += "`n GO `n"
		$MergedOutput += "`n GO `n"
		$MergedOutput += [System.IO.File]::ReadAllText("$sqlScript")
		$MergedOutput += "`n GO `n"
		
	}
	
	# Append this code to existing ScriptFile
	$MergedOutput | Out-File -Encoding "Unicode" -FilePath $ScriptFilePath -Append
	
	# now, starting with folderspec, traverse the directory tree , skip all folders that start with _ (underscore)
	foreach ($subfolder in (Get-ChildItem $folderspec | Where-Object {$_.PSIsContainer -and $_.Name -notlike "_*"}) | Sort-Object Name){
		$subfolder = $subfolder.FullName

		Recurse-Folders $subfolder
	
	}
	
}

################################################################################
#     Main
################################################################################
Try
{
	[int]$exitCode = 0

	# Run the "master only" script 
	# RunCreateDbScript
	
	# Prepare the ScriptFile
	"`n" | Out-File -Encoding "Unicode" -FilePath $ScriptFilePath -Force  
	
	
	# Merge all .sql files, then subfolders, alphabetically
	Recurse-Folders $CurrentSQLScriptDirectory
	
	$exitCode = 0
}
Catch
{
	Write-Host "Error running script: $sqlScript" -ForegroundColor Red

	# Loop through all errors
	$currentException = $_.Exception

	While ($currentException) {
		Write-Host $currentException.Message
		$lastException = $currentException
		if ($currentException.GetType().Name -eq "SqlException") {break}
		$currentException = $currentException.InnerException
	}
	
	Write-Host "Error:  deployment FAILED" -ForegroundColor Red
	$exitCode = 1
}
Finally {
	$script:ErrorActionPreference = "SilentlyContinue"
	foreach ($err in $Error) {Throw $err}
	Exit $exitCode
}