Skip to content

Commit

Permalink
Fix database data directory cleanup (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-garcia-garcia committed Aug 4, 2024
1 parent 748daa1 commit 63770cd
Showing 1 changed file with 3 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ if ($restored -eq $false -and $Env:MSSQL_LIFECYCLE -eq 'ATTACH') {
}

if (($false -eq $restored) -and ($Env:MSSQL_LIFECYCLE -ne 'ATTACH') -and ($Env:MSSQL_LIFECYCLE -ne 'PERSISTENT')) {
$hasData = (Get-ChildItem $dataPath -File | Measure-Object).Count -gt 0 -or (Get-ChildItem $logPath -File | Measure-Object).Count -gt 0;
$hasData = (Get-ChildItem $dataPath -File -Recurse | Measure-Object).Count -gt 0 -or (Get-ChildItem $logPath -File -Recurse | Measure-Object).Count -gt 0;
if ($hasData -eq $true) {
$clearDataPaths = SbsGetEnvBool -Name "MSSQL_CLEARDATAPATHS" -DefaultValue $false;
if ($clearDataPaths -and $Env:MSSQL_LIFECYCLE -eq "BACKUP") {
Expand All @@ -74,8 +74,8 @@ if (($false -eq $restored) -and ($Env:MSSQL_LIFECYCLE -ne 'ATTACH') -and ($Env:M
SbsWriteWarning "# MSSQL_LIFECYCLE=BACKUP and MSSQL_CLEARDATAPATHS=True";
SbsWriteWarning "######################################";
Get-DbaDatabase -SqlInstance $sqlInstance -ExcludeSystem | Remove-DbaDatabase -Verbose -Confirm:$false;
Get-ChildItem -Path $dataPath | Remove-Item -Recurse -Force;
Get-ChildItem -Path $logPath | Remove-Item -Recurse -Force;
Get-ChildItem -Path $dataPath -File -Recurse | Remove-Item -Force;
Get-ChildItem -Path $logPath -File -Recurse | Remove-Item -Force;
}
else {
Write-Error "No structure.json file was found to attach database, yet there are files in the data and log directories. Please clear them. You can use the MSSQL_CLEARDATAPATHS=True in combination with MSSQL_LIFECYCLE=BACKUP to clear the paths automatically.";
Expand Down

0 comments on commit 63770cd

Please sign in to comment.