Octopus Deploy – SQL – Execute Scripts Ordered step giving Exception

huangapple go评论79阅读模式
英文:

Octopus Deploy - SQL - Execute Scripts Ordered step giving Exception

问题

在Octopus Deploy中,我已经添加了一个步骤来运行存储过程,使用库脚本“SQL - 执行脚本有序步骤”。

当我提供要执行存储过程的脚本时,它会引发以下异常:

> 调用“ReadAllText”时出现异常,带有“1”个参数: “指定的路径、文件名或两者都太长。完全限定的文件名必须少于260个字符,目录名必须少于248个字符。”
关闭连接

我认为这是因为我提供的文本脚本作为“SQL脚本文件”字段中要执行的脚本太长。

正如示例中所示,我可以直接运行脚本。因此,我将存储过程执行脚本提供给库的PowerShell脚本 -

$content = [IO.File]::ReadAllText($OctopusParameters[‘SqlScriptFile’])

ReadAllText期望的内容要少于260个字符。

我能想到的一个解决方案是将执行脚本作为包内的文件提供。但这将是最后的选择。

如何从流程步骤中直接运行存储过程?

英文:

In Octopus deploy I have added a step in process to run the stored procedure with library script “SQL - Execute Scripts Ordered step”.

When I’m providing the script to execute the stored procedure it is throwing the below Exception:

> Exception calling “ReadAllText” with “1” argument(s): “The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.”
Closing connection

I believe this is because of the large script as text I've provided to execute in field “SQL Script File”.
As shown in examples I can run script directly. So I’m providing the stored procedure execution script but in library's PowerShell scipt -

$content = [IO.File]::ReadAllText($OctopusParameters[‘SqlScriptFile’])

ReadAllText is expecting something less than 260 characters.

One solution I can think of is to provide the execution script as a file within package itself. But this will be the last resort.

How can I run the stored procedure directly from the step in process?

答案1

得分: 1

The SQL - Execute Scripts Ordered template is meant to be run against a folder of SQL script files.

The SQL - Execute Script template may be a better match for your scenario.

英文:

The SQL - Execute Scripts Ordered template is meant to be run against a folder of SQL script files.

The SQL - Execute Script template may be a better match for your scenario.

答案2

得分: 0

显然,[IO.File]::ReadAllText($OctopusParameters[‘SqlScriptFile’]) 期望 SqlScriptFile 作为文件路径。我已经更新了库的 PowerShell 脚本,将完整的 SQL 脚本从字段 "SQL Script File" 作为参数传递给了函数。

$content = $OctopusParameters['SqlScriptFile']
Execute-SqlQuery -query $content

以下是供参考的完整 PowerShell 脚本:

$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $OctopusParameters['ConnectionString']
Register-ObjectEvent -inputobject $connection -eventname InfoMessage -action {
    write-host $event.SourceEventArgs
} | Out-Null

function Execute-SqlQuery($query) {
    $queries = [System.Text.RegularExpressions.Regex]::Split($query, "^\s*GO\s*$", [System.Text.RegularExpressions.RegexOptions]::IgnoreCase -bor [System.Text.RegularExpressions.RegexOptions]::Multiline)

    $queries | ForEach-Object {
        $q = $_
        if ((-not [String]::IsNullOrWhiteSpace($q)) -and ($q.Trim().ToLowerInvariant() -ne "go")) {            
            $command = $connection.CreateCommand()
            $command.CommandText = $q
            $command.CommandTimeout = $OctopusParameters['CommandTimeout']
            $command.ExecuteNonQuery() | Out-Null
        }
    }
}

Write-Host "Connecting"
try {
    $connection.Open()
    Write-Host "Executing script in" $OctopusParameters['SqlScriptFile']
    $content = $OctopusParameters['SqlScriptFile']
    Execute-SqlQuery -query $content
}
catch {
    if ($OctopusParameters['ContinueOnError']) {
        Write-Host $_.Exception.Message
    }
    else {
        throw
    }
}
finally {
    Write-Host "Closing connection"
    $connection.Dispose()
}

希望这对你有所帮助。

英文:

Apparantly [IO.File]::ReadAllText($OctopusParameters[‘SqlScriptFile’]) is expecting file path as SqlScriptFile. I updated the library's powershell script to take the full sql script from field "SQL Script File" as parameter and passed it directly to the function.

$content= $OctopusParameters['SqlScriptFile']
        Execute-SqlQuery -query $content

providing below the full powershell script for reference:

$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $OctopusParameters['ConnectionString']
Register-ObjectEvent -inputobject $connection -eventname InfoMessage -action {
    write-host $event.SourceEventArgs
} | Out-Null

function Execute-SqlQuery($query) {
    $queries = [System.Text.RegularExpressions.Regex]::Split($query, "^\s*GO\s*`$", [System.Text.RegularExpressions.RegexOptions]::IgnoreCase -bor [System.Text.RegularExpressions.RegexOptions]::Multiline)

    $queries | ForEach-Object {
        $q = $_
        if ((-not [String]::IsNullOrWhiteSpace($q)) -and ($q.Trim().ToLowerInvariant() -ne "go")) {            
            $command = $connection.CreateCommand()
            $command.CommandText = $q
            $command.CommandTimeout = $OctopusParameters['CommandTimeout']
            $command.ExecuteNonQuery() | Out-Null
        }
    }
}

Write-Host "Connecting"
try {
    $connection.Open()
    Write-Host "Executing script in" $OctopusParameters['SqlScriptFile']
    # $content = [IO.File]::ReadAllText($OctopusParameters['SqlScriptFile'])
	$content= $OctopusParameters['SqlScriptFile']
    Execute-SqlQuery -query $content
}
catch {
	if ($OctopusParameters['ContinueOnError']) {
		Write-Host $_.Exception.Message
	}
	else {
		throw
	}
}
finally {
    Write-Host "Closing connection"
    $connection.Dispose()
}

huangapple
  • 本文由 发表于 2023年2月8日 17:53:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383994.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定