-
Notifications
You must be signed in to change notification settings - Fork 1
/
DB_CI_ExecuteMigrationScripts.ps1
executable file
·39 lines (33 loc) · 1.32 KB
/
DB_CI_ExecuteMigrationScripts.ps1
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
<#
# Author - Dane Butler
# Purpose - Execute migration scripts (lookup table inserts) for a given DB
All scripts should be written in an idempotent manner
# Called By - Jenkins upon DB code check in
#>
Param( $gitRevision, $workingDirectory, $sqlServerInstance )
# Write output to host so it will be available in the "Console Output" in Jenkins
Write-Host "Git Revision - $gitRevision, Working Directory - $workingDirectory";
# Get DB version directory.. Pattern matching used because all DB's will not have the same version number
$versionDirectory = Get-ChildItem -Path $workingDirectory -Directory | Where-Object {$_.Name -match "v[1-9].[0-9]"};
# Set Migration Script directory
$migrationScriptDirectory = "$workingDirectory\$versionDirectory\MigrationScripts";
Write-Host "Migration Script directory - $migrationScriptDirectory";
if(Test-Path -Path $migrationScriptDirectory)
{
$migrationScriptFiles = Get-ChildItem -Path $migrationScriptDirectory | where {$_.extension -eq ".sql"} | % {$_.FullName };
if($file)
{
foreach($file in $migrationScriptFiles)
{
Write-Host "Executing Migration Script file - $file";
try
{
Invoke-SqlCmd -ServerInstance $sqlServerInstance -Database "master" -InputFile $file;
}
catch
{
Write-Host $_.Exception.Message;
}
}
}
}