Skip to content

Commit

Permalink
pgsql: Move namespace to its module
Browse files Browse the repository at this point in the history
  • Loading branch information
KeyboardNerd committed Mar 13, 2019
1 parent 98e81ff commit 176c69e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,40 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package pgsql
package namespace

import (
"database/sql"
"fmt"
"sort"

"github.com/coreos/clair/database"
"github.com/coreos/clair/database/pgsql/util"
"github.com/coreos/clair/pkg/commonerr"
)

const (
searchNamespaceID = `SELECT id FROM Namespace WHERE name = $1 AND version_format = $2`
)

func queryPersistNamespace(count int) string {
return util.QueryPersist(count,
"namespace",
"namespace_name_version_format_key",
"name",
"version_format")
}

func querySearchNamespace(nsCount int) string {
return fmt.Sprintf(
`SELECT id, name, version_format
FROM namespace WHERE (name, version_format) IN (%s)`,
util.QueryString(2, nsCount),
)
}

// PersistNamespaces soi namespaces into database.
func (tx *pgSession) PersistNamespaces(namespaces []database.Namespace) error {
func PersistNamespaces(tx *sql.Tx, namespaces []database.Namespace) error {
if len(namespaces) == 0 {
return nil
}
Expand All @@ -49,12 +67,12 @@ func (tx *pgSession) PersistNamespaces(namespaces []database.Namespace) error {

_, err := tx.Exec(queryPersistNamespace(len(namespaces)), keys...)
if err != nil {
return handleError("queryPersistNamespace", err)
return util.HandleError("queryPersistNamespace", err)
}
return nil
}

func (tx *pgSession) findNamespaceIDs(namespaces []database.Namespace) ([]sql.NullInt64, error) {
func FindNamespaceIDs(tx *sql.Tx, namespaces []database.Namespace) ([]sql.NullInt64, error) {
if len(namespaces) == 0 {
return nil, nil
}
Expand All @@ -69,7 +87,7 @@ func (tx *pgSession) findNamespaceIDs(namespaces []database.Namespace) ([]sql.Nu

rows, err := tx.Query(querySearchNamespace(len(namespaces)), keys...)
if err != nil {
return nil, handleError("searchNamespace", err)
return nil, util.HandleError("searchNamespace", err)
}

defer rows.Close()
Expand All @@ -81,7 +99,7 @@ func (tx *pgSession) findNamespaceIDs(namespaces []database.Namespace) ([]sql.Nu
for rows.Next() {
err := rows.Scan(&id, &ns.Name, &ns.VersionFormat)
if err != nil {
return nil, handleError("searchNamespace", err)
return nil, util.HandleError("searchNamespace", err)
}
nsMap[ns] = id
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,34 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package pgsql
package namespace

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/coreos/clair/database"
"github.com/coreos/clair/database/pgsql/testutil"
)

func TestPersistNamespaces(t *testing.T) {
datastore, tx := openSessionForTest(t, "PersistNamespaces", false)
defer closeTest(t, datastore, tx)
tx, cleanup := testutil.CreateTestTx(t, "PersistNamespaces")
defer cleanup()

ns1 := database.Namespace{}
ns2 := database.Namespace{Name: "t", VersionFormat: "b"}

// Empty Case
assert.Nil(t, tx.PersistNamespaces([]database.Namespace{}))
assert.Nil(t, PersistNamespaces(tx, []database.Namespace{}))
// Invalid Case
assert.NotNil(t, tx.PersistNamespaces([]database.Namespace{ns1}))
assert.NotNil(t, PersistNamespaces(tx, []database.Namespace{ns1}))
// Duplicated Case
assert.Nil(t, tx.PersistNamespaces([]database.Namespace{ns2, ns2}))
assert.Nil(t, PersistNamespaces(tx, []database.Namespace{ns2, ns2}))
// Existing Case
assert.Nil(t, tx.PersistNamespaces([]database.Namespace{ns2}))
assert.Nil(t, PersistNamespaces(tx, []database.Namespace{ns2}))

nsList := listNamespaces(t, tx)
nsList := testutil.ListNamespaces(t, tx)
assert.Len(t, nsList, 1)
assert.Equal(t, ns2, nsList[0])
}

0 comments on commit 176c69e

Please sign in to comment.