Enhance Windows MSI build script with improved error handling and database modes
Some checks failed
farmcontrol/farmcontrol-server/pipeline/head There was a failure building this commit

- Added constants for Windows Installer OpenDatabase modes to clarify database access options.
- Improved error handling in the Set-InstallerBinaryStream function to gracefully handle cases where the Binary table is empty.
- Refactored the New-WrapperMsiDatabase function to ensure proper path handling and database creation, enhancing the robustness of the MSI generation process.
This commit is contained in:
Tom Butcher 2026-08-01 22:52:07 +01:00
parent 8783caa93c
commit a4d8a501d1

View File

@ -15,6 +15,11 @@ param(
$ErrorActionPreference = "Stop"
# Windows Installer OpenDatabase modes (winuser.h / MSI API).
$msiOpenDatabaseModeReadOnly = 0
$msiOpenDatabaseModeTransact = 1
$msiOpenDatabaseModeCreate = 3
function Get-MsiVersion {
param([string]$InputVersion)
@ -99,7 +104,11 @@ function Set-InstallerBinaryStream {
)
$deleteSql = "DELETE FROM `Binary` WHERE `Name` = '$Name'"
try {
Invoke-InstallerExecute -Database $Database -Sql $deleteSql
} catch {
# Ignore when the Binary table is empty on first insert.
}
$insertSql = "INSERT INTO `Binary` (`Name`, `Data`) VALUES (?, ?)"
$view = $Database.GetType().InvokeMember(
@ -147,23 +156,19 @@ function New-WrapperMsiDatabase {
[string]$UpgradeCodeValue
)
if (Test-Path $OutputPath) {
Remove-Item -LiteralPath $OutputPath -Force
}
$outputDir = Split-Path $OutputPath -Parent
$outputPath = [System.IO.Path]::GetFullPath($OutputPath)
$outputDir = Split-Path $outputPath -Parent
if ($outputDir -and -not (Test-Path $outputDir)) {
New-Item -ItemType Directory -Path $outputDir -Force | Out-Null
}
if (Test-Path $outputPath) {
Remove-Item -LiteralPath $outputPath -Force
}
$installer = New-Object -ComObject WindowsInstaller.Installer
$database = $installer.GetType().InvokeMember(
"OpenDatabase",
"InvokeMethod",
$null,
$installer,
@($OutputPath, 1)
)
# Mode 3 (create) is required for new databases; mode 1 (transact) only opens existing MSIs.
$database = $installer.OpenDatabase($outputPath, $msiOpenDatabaseModeCreate)
Invoke-InstallerExecute -Database $database -Sql @"
CREATE TABLE `Property` (