Fix database init when locale isn't english (#736)

* use IsMissingTableError(error)

* update changelog
This commit is contained in:
Jim authored and GitHub committed 2020-10-20 21:13:01 -04:00
1 parent 3b347d24d6
commit 547ae7c802
4 files changed
+70 -1

No files matched your search

+3
View File
@@ -14,6 +14,9 @@ Canonical reference for changes, improvements, and bugfixes for Boundary.
### Bug Fixes
* cli: Fix database init when locale isn't english
([Issue](https://github.com/hashicorp/boundary/issues/729))
([PR](https://github.com/hashicorp/boundary/pull/736))
* cli: Fix hyphenation in help output for resources with compound names
([Issue](https://github.com/hashicorp/boundary/issues/686))
([PR](https://github.com/hashicorp/boundary/pull/689))
+1 -1
View File
@@ -254,7 +254,7 @@ func (c *InitCommand) Run(args []string) (retCode int) {
c.UI.Info("Database already initialized.")
return 0
}
case strings.Contains(err.Error(), "does not exist"):
case db.IsMissingTableError(err):
// Doesn't exist so we continue on
default:
c.UI.Error(fmt.Errorf("Error querying database for init status: %w", err).Error())
+12
View File
@@ -89,3 +89,15 @@ func IsNotNullError(err error) bool {
return false
}
// IsMissingTableError returns a boolean indicating whether the error is known
// to report a undefined/missing table violation.
func IsMissingTableError(err error) bool {
var pqError *pq.Error
if errors.As(err, &pqError) {
if pqError.Code == "42P01" {
return true
}
}
return false
}
+54
View File
@@ -1,10 +1,12 @@
package db
import (
"context"
"testing"
"github.com/lib/pq"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestError_IsUnique(t *testing.T) {
@@ -124,3 +126,55 @@ func TestError_IsNotNullError(t *testing.T) {
})
}
}
func TestError_IsMissingTableError(t *testing.T) {
var tests = []struct {
name string
in error
want bool
}{
{
name: "nil-error",
in: nil,
want: false,
},
{
name: "postgres-is-unique-not-not-null",
in: &pq.Error{
Code: pq.ErrorCode("23505"),
},
want: false,
},
{
name: "postgres-is-check-constraint-not-not-null",
in: &pq.Error{
Code: pq.ErrorCode("23514"),
},
want: false,
},
{
name: "postgres-is-missing-table",
in: &pq.Error{
Code: pq.ErrorCode("42P01"),
},
want: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
err := tt.in
got := IsMissingTableError(err)
assert.Equal(tt.want, got)
})
}
t.Run("query-missing-table", func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
conn, _ := TestSetup(t, "postgres")
rw := New(conn)
_, err := rw.Query(context.Background(), "select * from non_existent_table", nil)
require.Error(err)
assert.True(IsMissingTableError(err))
})
}