-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-db-from-dacpac.ps1
More file actions
86 lines (71 loc) · 2.98 KB
/
create-db-from-dacpac.ps1
File metadata and controls
86 lines (71 loc) · 2.98 KB
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
param(
[parameter(Mandatory = $true, Position = 0)]
[string]$sqlUser,
[parameter(Mandatory = $true, Position = 1)]
[string]$sqlPassword,
[parameter(Mandatory = $true, Position = 2)]
[string]$serverName,
[parameter(Mandatory = $true, Position = 3)]
$dacPacPath,
[parameter(Mandatory = $true, Position = 4)]
[string]$dbLocation,
[parameter(Mandatory = $false, Position = 5)]
[string]$sqlInstallationBinFolder = "C:\Program Files (x86)\Microsoft SQL Server\140\DAC\bin",
[parameter(Mandatory = $false, Position = 6)]
[string]$tempDbNamePrefix = "PoweShellTransformation."
)
function Test-SQLConnection {
[OutputType([bool])]
Param
(
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 0)]
$ConnectionString
)
try {
$sqlConnection = New-Object System.Data.SqlClient.SqlConnection $ConnectionString;
$sqlConnection.Open();
$sqlConnection.Close();
return $true;
}
catch {
return $false;
}
}
if (-not (Test-Path $sqlInstallationBinFolder)) {
Write-Host "Incorrect sqlInstallationBinFolder: $sqlInstallationBinFolder" -ForegroundColor Red
exit
}
if (-not (Test-Path "$sqlInstallationBinFolder\SqlPackage.exe")) {
Write-Host "Could not find SqlPackage.exe: $sqlInstallationBinFolder\SqlPackage.exe" -ForegroundColor Red
exit
}
if (-not (Test-SQLConnection "Data Source=$serverName;database=master;User ID=$sqlUser;Password=$sqlPassword;")) {
Write-Host "Could not connect to a database. Check credentials" -ForegroundColor Red
exit
}
if (-not(Test-Path "$dbLocation")) {
New-Item -ItemType directory -Path "$dbLocation"
}
if (-not(Test-Path $dacPacPath)) {
Write-Error "Cannot find file"
}
$dacPac = Get-Item -Path $dacPacPath
$name = $tempDbNamePrefix + $dacPac.Name.Replace(".dacpac", "")
$srcPath = $dacPac.FullName
# publish db from script
Write-Host "Processing $($_.Name)" -ForegroundColor Green
& "$sqlInstallationBinFolder\SqlPackage.exe" /action:Publish /SourceFile:$srcPath /TargetServerName:$serverName /TargetDatabaseName:$name /TargetUser:$sqlUser /TargetPassword:$sqlPassword /p:AllowIncompatiblePlatform=true
# get database files paths
$query = "select name, physical_name from sys.master_files where name like '$name%' and physical_name like '%$name%'"
$paths = Invoke-Sqlcmd -ServerInstance $serverName -Database "master" -Query $query -Username $sqlUser -Password $sqlPassword | % { $_.physical_name }
$paths | % { Write-Host $_ -ForegroundColor Yellow }
# detach database
$query = "sp_detach_db '$name', 'true';"
Invoke-Sqlcmd -ServerInstance $serverName -Database "master" -Query $query -Username $sqlUser -Password $sqlPassword
# move mdf and ldf files
$paths | % { Move-Item -Path $_ -Destination $dbLocation -Force }
# set file names
Get-ChildItem -Path "$dbLocation" | % {
$newName = $_.Name.Replace("_Primary", "").Replace($tempDbNamePrefix, [string]::Empty)
Rename-Item -Path $_.FullName -NewName $newName
}