Skip to content

Commit 41c71c2

Browse files
authored
on error ignore precedence over -b (#181)
* on error ignore precedence over -b * adding UTs * lint errors
1 parent c211944 commit 41c71c2

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

pkg/sqlcmd/commands.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ func newCommands() Commands {
103103
isSystem: true,
104104
},
105105
"ONERROR": {
106-
regex: regexp.MustCompile(`(?im)^[\t ]*?:?ONERROR(?:[ \t]+(.*$)|$)`),
107-
action: onerrorCommand,
108-
name: "ONERROR",
106+
regex: regexp.MustCompile(`(?im)^[\t ]*?:?ONERROR(?:[ \t]+(.*$)|$)`),
107+
action: onerrorCommand,
108+
name: "ONERROR",
109109
},
110110
}
111111
}
@@ -474,6 +474,7 @@ func onerrorCommand(s *Sqlcmd, args []string, line uint) error {
474474
s.Connect.ExitOnError = true
475475
} else if strings.EqualFold(strings.ToLower(params), "ignore") {
476476
s.Connect.IgnoreError = true
477+
s.Connect.ExitOnError = false
477478
} else {
478479
return InvalidCommandError("ON ERROR", line)
479480
}

pkg/sqlcmd/commands_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,16 @@ func TestOnErrorCommand(t *testing.T) {
240240
err = runSqlCmd(t, s, []string{":ONERROR exit", "printtgit N'message'", "SELECT @@versionn", "GO"})
241241
assert.NoError(t, err, "runSqlCmd")
242242
assert.Equal(t, 1, s.Exitcode, "ExitCode")
243+
// -b sets ExitOnError true
244+
s.Connect.ExitOnError = true
245+
err = runSqlCmd(t, s, []string{":ONERROR ignore", "printtgit N'message'", "SELECT @@versionn", "GO"})
246+
// when ignore is set along with -b command , ignore takes precedence and resets ExitOnError
247+
assert.Equal(t, false, s.Connect.ExitOnError, "ExitOnError")
248+
assert.NoError(t, err, "runSqlCmd")
249+
// checking ExitonError with Exit option
250+
err = runSqlCmd(t, s, []string{":ONERROR exit", "printtgit N'message'", "SELECT @@versionn", "GO"})
251+
assert.Equal(t, true, s.Connect.ExitOnError, "ExitOnError")
252+
assert.NoError(t, err, "runSqlCmd")
243253
}
244254
func TestResolveArgumentVariables(t *testing.T) {
245255
type argTest struct {

0 commit comments

Comments
 (0)