pgsql: Move namespace to its module
This commit is contained in:
parent
98e81ff5f1
commit
176c69e59d
@ -12,13 +12,15 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
package pgsql
|
package namespace
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
"github.com/coreos/clair/database"
|
"github.com/coreos/clair/database"
|
||||||
|
"github.com/coreos/clair/database/pgsql/util"
|
||||||
"github.com/coreos/clair/pkg/commonerr"
|
"github.com/coreos/clair/pkg/commonerr"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -26,8 +28,24 @@ const (
|
|||||||
searchNamespaceID = `SELECT id FROM Namespace WHERE name = $1 AND version_format = $2`
|
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.
|
// 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 {
|
if len(namespaces) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -49,12 +67,12 @@ func (tx *pgSession) PersistNamespaces(namespaces []database.Namespace) error {
|
|||||||
|
|
||||||
_, err := tx.Exec(queryPersistNamespace(len(namespaces)), keys...)
|
_, err := tx.Exec(queryPersistNamespace(len(namespaces)), keys...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return handleError("queryPersistNamespace", err)
|
return util.HandleError("queryPersistNamespace", err)
|
||||||
}
|
}
|
||||||
return nil
|
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 {
|
if len(namespaces) == 0 {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
@ -69,7 +87,7 @@ func (tx *pgSession) findNamespaceIDs(namespaces []database.Namespace) ([]sql.Nu
|
|||||||
|
|
||||||
rows, err := tx.Query(querySearchNamespace(len(namespaces)), keys...)
|
rows, err := tx.Query(querySearchNamespace(len(namespaces)), keys...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, handleError("searchNamespace", err)
|
return nil, util.HandleError("searchNamespace", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
@ -81,7 +99,7 @@ func (tx *pgSession) findNamespaceIDs(namespaces []database.Namespace) ([]sql.Nu
|
|||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
err := rows.Scan(&id, &ns.Name, &ns.VersionFormat)
|
err := rows.Scan(&id, &ns.Name, &ns.VersionFormat)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, handleError("searchNamespace", err)
|
return nil, util.HandleError("searchNamespace", err)
|
||||||
}
|
}
|
||||||
nsMap[ns] = id
|
nsMap[ns] = id
|
||||||
}
|
}
|
@ -12,7 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
package pgsql
|
package namespace
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
@ -20,25 +20,26 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/coreos/clair/database"
|
"github.com/coreos/clair/database"
|
||||||
|
"github.com/coreos/clair/database/pgsql/testutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPersistNamespaces(t *testing.T) {
|
func TestPersistNamespaces(t *testing.T) {
|
||||||
datastore, tx := openSessionForTest(t, "PersistNamespaces", false)
|
tx, cleanup := testutil.CreateTestTx(t, "PersistNamespaces")
|
||||||
defer closeTest(t, datastore, tx)
|
defer cleanup()
|
||||||
|
|
||||||
ns1 := database.Namespace{}
|
ns1 := database.Namespace{}
|
||||||
ns2 := database.Namespace{Name: "t", VersionFormat: "b"}
|
ns2 := database.Namespace{Name: "t", VersionFormat: "b"}
|
||||||
|
|
||||||
// Empty Case
|
// Empty Case
|
||||||
assert.Nil(t, tx.PersistNamespaces([]database.Namespace{}))
|
assert.Nil(t, PersistNamespaces(tx, []database.Namespace{}))
|
||||||
// Invalid Case
|
// Invalid Case
|
||||||
assert.NotNil(t, tx.PersistNamespaces([]database.Namespace{ns1}))
|
assert.NotNil(t, PersistNamespaces(tx, []database.Namespace{ns1}))
|
||||||
// Duplicated Case
|
// Duplicated Case
|
||||||
assert.Nil(t, tx.PersistNamespaces([]database.Namespace{ns2, ns2}))
|
assert.Nil(t, PersistNamespaces(tx, []database.Namespace{ns2, ns2}))
|
||||||
// Existing Case
|
// 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.Len(t, nsList, 1)
|
||||||
assert.Equal(t, ns2, nsList[0])
|
assert.Equal(t, ns2, nsList[0])
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user