Merged in feature/update (pull request #27)
Query Update * baselineupdate * baseupdateplusmodelupdates * passtests * somemoresubmittesting * testinnerfunctions * readmeandinstall * cleanerstartup * readmeplusdeps * tidyatrighttime * validatetests * normalizedontvalidate * abitofzenormalizationcleanup * addunitstestforhelperfuns * normalizeactiveversiontestas
This commit is contained in:
@@ -8,33 +8,87 @@ import (
|
||||
)
|
||||
|
||||
func MustToDBUUIDArray(ids []uuid.UUID) []pgtype.UUID {
|
||||
dbIDs := make([]pgtype.UUID, len(ids))
|
||||
for index, id := range ids {
|
||||
dbIDs[index] = MustToDBUUID(id)
|
||||
}
|
||||
|
||||
return dbIDs
|
||||
}
|
||||
|
||||
func MustToDBUUID(id uuid.UUID) pgtype.UUID {
|
||||
var dbID pgtype.UUID
|
||||
err := dbID.Scan(id.String())
|
||||
dbid, err := ToDBUUIDArray(ids)
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
||||
return dbID
|
||||
return dbid
|
||||
}
|
||||
|
||||
func MustToUUID(id pgtype.UUID) uuid.UUID {
|
||||
return uuid.Must(uuid.FromBytes(id.Bytes[:]))
|
||||
func ToDBUUIDArray(ids []uuid.UUID) ([]pgtype.UUID, error) {
|
||||
dbIDs := []pgtype.UUID{}
|
||||
for _, id := range ids {
|
||||
uid, err := ToDBUUID(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if uid.Valid {
|
||||
dbIDs = append(dbIDs, uid)
|
||||
}
|
||||
}
|
||||
|
||||
return dbIDs, nil
|
||||
}
|
||||
|
||||
func MustToUUIDArray(dbIDs []pgtype.UUID) []uuid.UUID {
|
||||
ids := make([]uuid.UUID, len(dbIDs))
|
||||
for index, id := range dbIDs {
|
||||
ids[index] = MustToUUID(id)
|
||||
func MustToDBUUID(id uuid.UUID) pgtype.UUID {
|
||||
dbid, err := ToDBUUID(id)
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
||||
return dbid
|
||||
}
|
||||
|
||||
func ToDBUUID(id uuid.UUID) (pgtype.UUID, error) {
|
||||
var dbID pgtype.UUID
|
||||
err := dbID.Scan(id.String())
|
||||
if err != nil {
|
||||
return dbID, err
|
||||
}
|
||||
|
||||
if id == uuid.Nil {
|
||||
dbID.Valid = false
|
||||
}
|
||||
|
||||
return dbID, nil
|
||||
}
|
||||
|
||||
func MustToUUID(dbid pgtype.UUID) uuid.UUID {
|
||||
id, err := ToUUID(dbid)
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
||||
return id
|
||||
}
|
||||
|
||||
func ToUUID(id pgtype.UUID) (uuid.UUID, error) {
|
||||
return uuid.FromBytes(id.Bytes[:])
|
||||
}
|
||||
|
||||
func MustToUUIDArray(dbids []pgtype.UUID) []uuid.UUID {
|
||||
ids, err := ToUUIDArray(dbids)
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
||||
return ids
|
||||
}
|
||||
|
||||
func ToUUIDArray(dbIDs []pgtype.UUID) ([]uuid.UUID, error) {
|
||||
ids := []uuid.UUID{}
|
||||
for _, id := range dbIDs {
|
||||
uid, err := ToUUID(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if uid != uuid.Nil {
|
||||
ids = append(ids, uid)
|
||||
}
|
||||
}
|
||||
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
@@ -9,41 +9,53 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMustToDBUUID(t *testing.T) {
|
||||
func TestToDBUUID(t *testing.T) {
|
||||
id := uuid.New()
|
||||
|
||||
dbID := database.MustToDBUUID(id)
|
||||
dbID, err := database.ToDBUUID(id)
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Equal(t, true, dbID.Valid)
|
||||
assert.True(t, dbID.Valid)
|
||||
assert.Equal(t, id.String(), uuid.UUID(dbID.Bytes).String())
|
||||
}
|
||||
|
||||
func TestMustToDBUUIDArray(t *testing.T) {
|
||||
ids := []uuid.UUID{uuid.New(), uuid.New()}
|
||||
func TestToDBUUIDNil(t *testing.T) {
|
||||
id := uuid.Nil
|
||||
|
||||
dbIDs := database.MustToDBUUIDArray(ids)
|
||||
dbID, err := database.ToDBUUID(id)
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Len(t, dbIDs, len(ids))
|
||||
for index, id := range dbIDs {
|
||||
assert.Equal(t, database.MustToDBUUID(ids[index]), id)
|
||||
}
|
||||
assert.False(t, dbID.Valid)
|
||||
assert.Equal(t, id.String(), uuid.UUID(dbID.Bytes).String())
|
||||
}
|
||||
|
||||
func TestMustToUUID(t *testing.T) {
|
||||
dbID := database.MustToDBUUID(uuid.New())
|
||||
func TestToDBUUIDArray(t *testing.T) {
|
||||
ids := []uuid.UUID{uuid.Nil, uuid.New()}
|
||||
|
||||
id := database.MustToUUID(dbID)
|
||||
dbIDs, err := database.ToDBUUIDArray(ids)
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Len(t, dbIDs, 1)
|
||||
assert.ElementsMatch(t, []pgtype.UUID{database.MustToDBUUID(ids[1])}, dbIDs)
|
||||
}
|
||||
|
||||
func TestToUUID(t *testing.T) {
|
||||
dbID, err := database.ToDBUUID(uuid.New())
|
||||
assert.Nil(t, err)
|
||||
|
||||
id, err := database.ToUUID(dbID)
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Equal(t, id.String(), uuid.UUID(dbID.Bytes).String())
|
||||
}
|
||||
|
||||
func TestMustToUUIDArray(t *testing.T) {
|
||||
dbIDs := []pgtype.UUID{database.MustToDBUUID(uuid.New()), database.MustToDBUUID(uuid.New())}
|
||||
func TestToUUIDArray(t *testing.T) {
|
||||
ogIDs := []uuid.UUID{uuid.Nil, uuid.New()}
|
||||
dbIDs := database.MustToDBUUIDArray(ogIDs)
|
||||
|
||||
ids := database.MustToUUIDArray(dbIDs)
|
||||
ids, err := database.ToUUIDArray(dbIDs)
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Len(t, ids, len(dbIDs))
|
||||
for index, id := range dbIDs {
|
||||
assert.Equal(t, database.MustToDBUUID(ids[index]), id)
|
||||
}
|
||||
assert.Len(t, ids, 1)
|
||||
assert.ElementsMatch(t, []uuid.UUID{ogIDs[1]}, ids)
|
||||
}
|
||||
|
||||
@@ -11,6 +11,60 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const addQueryConfig = `-- name: AddQueryConfig :exec
|
||||
INSERT INTO queryConfigs (queryId, config, addedVersion) VALUES ($1, $2, $3)
|
||||
`
|
||||
|
||||
type AddQueryConfigParams struct {
|
||||
Queryid pgtype.UUID `db:"queryid"`
|
||||
Config []byte `db:"config"`
|
||||
Addedversion int32 `db:"addedversion"`
|
||||
}
|
||||
|
||||
// AddQueryConfig
|
||||
//
|
||||
// INSERT INTO queryConfigs (queryId, config, addedVersion) VALUES ($1, $2, $3)
|
||||
func (q *Queries) AddQueryConfig(ctx context.Context, arg *AddQueryConfigParams) error {
|
||||
_, err := q.db.Exec(ctx, addQueryConfig, arg.Queryid, arg.Config, arg.Addedversion)
|
||||
return err
|
||||
}
|
||||
|
||||
const addRequiredQuery = `-- name: AddRequiredQuery :exec
|
||||
INSERT INTO requiredQueries (queryId, requiredQueryId, addedVersion) VALUES ($1, $2, $3)
|
||||
`
|
||||
|
||||
type AddRequiredQueryParams struct {
|
||||
Queryid pgtype.UUID `db:"queryid"`
|
||||
Requiredqueryid pgtype.UUID `db:"requiredqueryid"`
|
||||
Addedversion int32 `db:"addedversion"`
|
||||
}
|
||||
|
||||
// AddRequiredQuery
|
||||
//
|
||||
// INSERT INTO requiredQueries (queryId, requiredQueryId, addedVersion) VALUES ($1, $2, $3)
|
||||
func (q *Queries) AddRequiredQuery(ctx context.Context, arg *AddRequiredQueryParams) error {
|
||||
_, err := q.db.Exec(ctx, addRequiredQuery, arg.Queryid, arg.Requiredqueryid, arg.Addedversion)
|
||||
return err
|
||||
}
|
||||
|
||||
const allQueriesExist = `-- name: AllQueriesExist :one
|
||||
SELECT COUNT(*) = COUNT(DISTINCT id) AS all_exist
|
||||
FROM unnest($1::uuid[]) AS input_id
|
||||
LEFT JOIN queries ON input_id = queries.id
|
||||
`
|
||||
|
||||
// AllQueriesExist
|
||||
//
|
||||
// SELECT COUNT(*) = COUNT(DISTINCT id) AS all_exist
|
||||
// FROM unnest($1::uuid[]) AS input_id
|
||||
// LEFT JOIN queries ON input_id = queries.id
|
||||
func (q *Queries) AllQueriesExist(ctx context.Context, dollar_1 []pgtype.UUID) (bool, error) {
|
||||
row := q.db.QueryRow(ctx, allQueriesExist, dollar_1)
|
||||
var all_exist bool
|
||||
err := row.Scan(&all_exist)
|
||||
return all_exist, err
|
||||
}
|
||||
|
||||
const createQuery = `-- name: CreateQuery :one
|
||||
INSERT INTO queries (type) VALUES ($1) RETURNING id
|
||||
`
|
||||
@@ -25,42 +79,6 @@ func (q *Queries) CreateQuery(ctx context.Context, type_ Querytype) (pgtype.UUID
|
||||
return id, err
|
||||
}
|
||||
|
||||
const createQueryConfig = `-- name: CreateQueryConfig :exec
|
||||
INSERT INTO queryConfigs (queryId, config, addedVersion) VALUES ($1, $2, $3)
|
||||
`
|
||||
|
||||
type CreateQueryConfigParams struct {
|
||||
Queryid pgtype.UUID `db:"queryid"`
|
||||
Config []byte `db:"config"`
|
||||
Addedversion int32 `db:"addedversion"`
|
||||
}
|
||||
|
||||
// CreateQueryConfig
|
||||
//
|
||||
// INSERT INTO queryConfigs (queryId, config, addedVersion) VALUES ($1, $2, $3)
|
||||
func (q *Queries) CreateQueryConfig(ctx context.Context, arg *CreateQueryConfigParams) error {
|
||||
_, err := q.db.Exec(ctx, createQueryConfig, arg.Queryid, arg.Config, arg.Addedversion)
|
||||
return err
|
||||
}
|
||||
|
||||
const createRequiredQuery = `-- name: CreateRequiredQuery :exec
|
||||
INSERT INTO requiredQueries (queryId, requiredQueryId, addedVersion) VALUES ($1, $2, $3)
|
||||
`
|
||||
|
||||
type CreateRequiredQueryParams struct {
|
||||
Queryid pgtype.UUID `db:"queryid"`
|
||||
Requiredqueryid pgtype.UUID `db:"requiredqueryid"`
|
||||
Addedversion int32 `db:"addedversion"`
|
||||
}
|
||||
|
||||
// CreateRequiredQuery
|
||||
//
|
||||
// INSERT INTO requiredQueries (queryId, requiredQueryId, addedVersion) VALUES ($1, $2, $3)
|
||||
func (q *Queries) CreateRequiredQuery(ctx context.Context, arg *CreateRequiredQueryParams) error {
|
||||
_, err := q.db.Exec(ctx, createRequiredQuery, arg.Queryid, arg.Requiredqueryid, arg.Addedversion)
|
||||
return err
|
||||
}
|
||||
|
||||
const deprecateQuery = `-- name: DeprecateQuery :exec
|
||||
INSERT INTO queryDeprecations (queryId) VALUES ($1)
|
||||
`
|
||||
@@ -169,3 +187,56 @@ func (q *Queries) ListQueries(ctx context.Context) ([]*Fullactivequery, error) {
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const removeQueryConfig = `-- name: RemoveQueryConfig :exec
|
||||
UPDATE queryConfigs SET removedVersion = $1 WHERE queryId = $2 and removedVersion is null
|
||||
`
|
||||
|
||||
type RemoveQueryConfigParams struct {
|
||||
Removedversion *int32 `db:"removedversion"`
|
||||
Queryid pgtype.UUID `db:"queryid"`
|
||||
}
|
||||
|
||||
// RemoveQueryConfig
|
||||
//
|
||||
// UPDATE queryConfigs SET removedVersion = $1 WHERE queryId = $2 and removedVersion is null
|
||||
func (q *Queries) RemoveQueryConfig(ctx context.Context, arg *RemoveQueryConfigParams) error {
|
||||
_, err := q.db.Exec(ctx, removeQueryConfig, arg.Removedversion, arg.Queryid)
|
||||
return err
|
||||
}
|
||||
|
||||
const removeRequiredQuery = `-- name: RemoveRequiredQuery :exec
|
||||
UPDATE requiredQueries SET removedVersion = $1 WHERE requiredQueryId = $2 and queryId = $3 and removedVersion is null
|
||||
`
|
||||
|
||||
type RemoveRequiredQueryParams struct {
|
||||
Removedversion *int32 `db:"removedversion"`
|
||||
Requiredqueryid pgtype.UUID `db:"requiredqueryid"`
|
||||
Queryid pgtype.UUID `db:"queryid"`
|
||||
}
|
||||
|
||||
// RemoveRequiredQuery
|
||||
//
|
||||
// UPDATE requiredQueries SET removedVersion = $1 WHERE requiredQueryId = $2 and queryId = $3 and removedVersion is null
|
||||
func (q *Queries) RemoveRequiredQuery(ctx context.Context, arg *RemoveRequiredQueryParams) error {
|
||||
_, err := q.db.Exec(ctx, removeRequiredQuery, arg.Removedversion, arg.Requiredqueryid, arg.Queryid)
|
||||
return err
|
||||
}
|
||||
|
||||
const updateQuery = `-- name: UpdateQuery :exec
|
||||
UPDATE queries SET activeVersion = $1, latestVersion = $2 WHERE id = $3
|
||||
`
|
||||
|
||||
type UpdateQueryParams struct {
|
||||
Activeversion int32 `db:"activeversion"`
|
||||
Latestversion int32 `db:"latestversion"`
|
||||
ID pgtype.UUID `db:"id"`
|
||||
}
|
||||
|
||||
// UpdateQuery
|
||||
//
|
||||
// UPDATE queries SET activeVersion = $1, latestVersion = $2 WHERE id = $3
|
||||
func (q *Queries) UpdateQuery(ctx context.Context, arg *UpdateQueryParams) error {
|
||||
_, err := q.db.Exec(ctx, updateQuery, arg.Activeversion, arg.Latestversion, arg.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"queryorchestration/internal/test"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -32,22 +33,6 @@ func TestQueries(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, jsonQueryID.Valid)
|
||||
|
||||
jsonConfig := []byte("{\"path\": \"example_path\"}")
|
||||
|
||||
err = queries.CreateRequiredQuery(ctx, &repository.CreateRequiredQueryParams{
|
||||
Queryid: jsonQueryID,
|
||||
Requiredqueryid: contextQueryID,
|
||||
Addedversion: 1,
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
|
||||
err = queries.CreateQueryConfig(ctx, &repository.CreateQueryConfigParams{
|
||||
Queryid: jsonQueryID,
|
||||
Config: jsonConfig,
|
||||
Addedversion: 1,
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
|
||||
jsonQuery, err := queries.GetQuery(ctx, jsonQueryID)
|
||||
assert.Nil(t, err)
|
||||
assert.EqualExportedValues(t, repository.Fullactivequery{
|
||||
@@ -55,10 +40,41 @@ func TestQueries(t *testing.T) {
|
||||
Type: repository.QuerytypeJsonExtractor,
|
||||
Activeversion: 1,
|
||||
Latestversion: 1,
|
||||
Config: jsonConfig,
|
||||
Requiredids: []pgtype.UUID{contextQueryID},
|
||||
Config: nil,
|
||||
Requiredids: []pgtype.UUID{database.MustToDBUUID(uuid.Nil)},
|
||||
}, *jsonQuery)
|
||||
|
||||
err = queries.UpdateQuery(ctx, &repository.UpdateQueryParams{
|
||||
Latestversion: 2,
|
||||
Activeversion: 1,
|
||||
ID: jsonQueryID,
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
|
||||
jsonConfig := []byte("{\"path\": \"example_path\"}")
|
||||
|
||||
err = queries.AddRequiredQuery(ctx, &repository.AddRequiredQueryParams{
|
||||
Queryid: jsonQueryID,
|
||||
Requiredqueryid: contextQueryID,
|
||||
Addedversion: 1,
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
|
||||
removeV := int32(2)
|
||||
err = queries.RemoveRequiredQuery(ctx, &repository.RemoveRequiredQueryParams{
|
||||
Queryid: jsonQueryID,
|
||||
Requiredqueryid: contextQueryID,
|
||||
Removedversion: &removeV,
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
|
||||
err = queries.AddQueryConfig(ctx, &repository.AddQueryConfigParams{
|
||||
Queryid: jsonQueryID,
|
||||
Config: jsonConfig,
|
||||
Addedversion: 1,
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
|
||||
jsonQueryConfig, err := queries.GetQueryConfig(ctx, &repository.GetQueryConfigParams{
|
||||
Queryid: jsonQueryID,
|
||||
Addedversion: jsonQuery.Activeversion,
|
||||
@@ -66,6 +82,50 @@ func TestQueries(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, jsonConfig, jsonQueryConfig.Config)
|
||||
|
||||
removeV = 2
|
||||
err = queries.RemoveQueryConfig(ctx, &repository.RemoveQueryConfigParams{
|
||||
Queryid: jsonQueryID,
|
||||
Removedversion: &removeV,
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
|
||||
_, err = queries.GetQueryConfig(ctx, &repository.GetQueryConfigParams{
|
||||
Queryid: jsonQueryID,
|
||||
Addedversion: jsonQuery.Activeversion,
|
||||
})
|
||||
assert.EqualError(t, err, "no rows in result set")
|
||||
|
||||
jsonQuery, err = queries.GetQuery(ctx, jsonQueryID)
|
||||
assert.Nil(t, err)
|
||||
assert.EqualExportedValues(t, repository.Fullactivequery{
|
||||
ID: jsonQueryID,
|
||||
Type: repository.QuerytypeJsonExtractor,
|
||||
Activeversion: 1,
|
||||
Latestversion: 2,
|
||||
Config: nil,
|
||||
Requiredids: []pgtype.UUID{database.MustToDBUUID(uuid.Nil)},
|
||||
}, *jsonQuery)
|
||||
|
||||
all_exist, err := queries.AllQueriesExist(ctx, []pgtype.UUID{})
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, all_exist)
|
||||
|
||||
all_exist, err = queries.AllQueriesExist(ctx, []pgtype.UUID{database.MustToDBUUID(uuid.New())})
|
||||
assert.Nil(t, err)
|
||||
assert.False(t, all_exist)
|
||||
|
||||
all_exist, err = queries.AllQueriesExist(ctx, []pgtype.UUID{jsonQueryID})
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, all_exist)
|
||||
|
||||
all_exist, err = queries.AllQueriesExist(ctx, []pgtype.UUID{jsonQueryID, contextQueryID})
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, all_exist)
|
||||
|
||||
all_exist, err = queries.AllQueriesExist(ctx, []pgtype.UUID{jsonQueryID, database.MustToDBUUID(uuid.New())})
|
||||
assert.Nil(t, err)
|
||||
assert.False(t, all_exist)
|
||||
|
||||
isDeprecated, err := queries.IsQueryDeprecated(ctx, jsonQueryID)
|
||||
assert.Nil(t, err)
|
||||
assert.False(t, isDeprecated)
|
||||
|
||||
Reference in New Issue
Block a user