Skip to content

Commit 1662d73

Browse files
New-DbaDatabase - Support Azure Blob Storage paths for data and log files (#10315)
1 parent b2f217f commit 1662d73

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

public/New-DbaDatabase.ps1

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,19 @@ function New-DbaDatabase {
252252
$LogFilePath = (Get-DbaDefaultPath -SqlInstance $server).Log
253253
}
254254

255-
if (-not (Test-DbaPath -SqlInstance $server -Path $LogFilePath)) {
255+
# Detect Azure Blob Storage URLs to skip filesystem directory operations
256+
$dataPathIsAzure = $DataFilePath -like "https://*"
257+
$logPathIsAzure = $LogFilePath -like "https://*"
258+
259+
# Trim trailing separators to avoid double-separators when concatenating file names
260+
$DataFilePath = $DataFilePath.TrimEnd('\', '/')
261+
$LogFilePath = $LogFilePath.TrimEnd('\', '/')
262+
263+
# Choose the path separator based on whether the path is an Azure Blob Storage URL
264+
$dataPathSeparator = if ($dataPathIsAzure) { "/" } else { "\" }
265+
$logPathSeparator = if ($logPathIsAzure) { "/" } else { "\" }
266+
267+
if (-not $logPathIsAzure -and -not (Test-DbaPath -SqlInstance $server -Path $LogFilePath)) {
256268
try {
257269
Write-Message -Message "Creating directory $LogFilePath" -Level Verbose
258270
$null = New-DbaDirectory -SqlInstance $server -Path $LogFilePath -EnableException
@@ -261,7 +273,7 @@ function New-DbaDatabase {
261273
}
262274
}
263275

264-
if (-not (Test-DbaPath -SqlInstance $server -Path $DataFilePath)) {
276+
if (-not $dataPathIsAzure -and -not (Test-DbaPath -SqlInstance $server -Path $DataFilePath)) {
265277
try {
266278
Write-Message -Message "Creating directory $DataFilePath" -Level Verbose
267279
$null = New-DbaDirectory -SqlInstance $server -Path $DataFilePath -EnableException
@@ -320,7 +332,7 @@ function New-DbaDatabase {
320332

321333
#create the primary file
322334
$primaryfile = New-Object Microsoft.SqlServer.Management.Smo.DataFile($primaryfg, $primaryfilename)
323-
$primaryfile.FileName = $DataFilePath + "\" + $primaryfilename + ".mdf"
335+
$primaryfile.FileName = $DataFilePath + $dataPathSeparator + $primaryfilename + ".mdf"
324336
$primaryfile.IsPrimaryFile = $true
325337

326338
if (Test-Bound -ParameterName PrimaryFilesize) {
@@ -362,7 +374,7 @@ function New-DbaDatabase {
362374
}
363375

364376
$tlog = New-Object Microsoft.SqlServer.Management.Smo.LogFile($newdb, $logname)
365-
$tlog.FileName = $LogFilePath + "\" + $logname + ".ldf"
377+
$tlog.FileName = $LogFilePath + $logPathSeparator + $logname + ".ldf"
366378

367379
if (Test-Bound -ParameterName LogSize) {
368380
$tlog.Size = ($LogSize * 1024)
@@ -420,7 +432,7 @@ function New-DbaDatabase {
420432
$secondaryfilename = "$($secondaryfilegroupname)_$($secondaryfgcount)"
421433
Write-Message -Message "Creating file name $secondaryfilename in filegroup $secondaryfilegroupname" -Level Verbose
422434
$secondaryfile = New-Object Microsoft.SQLServer.Management.Smo.Datafile($secondaryfg, $secondaryfilename)
423-
$secondaryfile.FileName = $DataFilePath + "\" + $secondaryfilename + ".ndf"
435+
$secondaryfile.FileName = $DataFilePath + $dataPathSeparator + $secondaryfilename + ".ndf"
424436

425437
if (Test-Bound -ParameterName SecondaryFilesize) {
426438
$secondaryfile.Size = ($SecondaryFilesize * 1024)

0 commit comments

Comments
 (0)