fix(queries): actions prepare funcs (#2689)

* chore(queries): test suite for prepare stmt funcs

* test(queries): prepare project funcs

* refactor: add comments

* test: simlify expected sql, added possibility to add args to expected queries

* test(queries): prepare funcs in org

* chore(backend): correct modules

* test(queries): org domain prepare funcs

* test: correct name

* refactor: file name

* refactor: add table to login policy columns

* chore(prepare_test): only add row to result if columns

* test(queries): login policy prepare funcs

* chore: add comments for configs

* test(queries): prepare idp funcs

* fix(queries): add table to password complexity policy cols

* test(queries): password complexity policy prepare funcs

* fix(queries): add table to password age policy cols

* test(queries): password age policy prepare func

* fix(queries): set cols on lockout policy

* test(queries): lockout policy prepare funs

* fix(queries): set table on privacy policy cols

* test(queries): privacy policy prepare funcs

* fix(queries): set table on org iam policy cols

* fix(queries): correct table in org iam policy cols

* test(queries): org iam policy prepare funcs

* test(queries): prepare project grant funcs

* refactor(queries): prepareProjectRoleQuery as func

* test(queries): prepare project role funcs

* test(queries): project grant check for nulls in joins

* fix(queries): allow null values in project grant

* refactor(queries): make toQuery private

* test(queries): action prepare funcs

* refactor: rename prepareFlowQuery to prepareFlowsQuery

* test: generic count only if count in cols

* refactor: remove param in prepareFlowQuery

* fix(queries): correct left joins in action flows

* test(queries): action flow prepare funcs
This commit is contained in:
Silvan
2021-11-16 14:04:22 +01:00
committed by GitHub
parent 9ddbc80e81
commit 6d94975a85
12 changed files with 955 additions and 64 deletions

View File

@@ -6,6 +6,7 @@ import (
"time"
sq "github.com/Masterminds/squirrel"
"github.com/lib/pq"
"github.com/caos/zitadel/internal/domain"
"github.com/caos/zitadel/internal/errors"
@@ -39,18 +40,17 @@ var (
)
type Flow struct {
ID string
CreationDate time.Time
ChangeDate time.Time
ResourceOwner string
Sequence uint64
CreationDate time.Time //TODO: add in projection
ChangeDate time.Time //TODO: add in projection
ResourceOwner string //TODO: add in projection
Sequence uint64 //TODO: add in projection
Type domain.FlowType
TriggerActions map[domain.TriggerType][]*Action
}
func (q *Queries) GetFlow(ctx context.Context, flowType domain.FlowType, orgID string) (*Flow, error) {
query, scan := q.prepareFlowQuery(flowType)
query, scan := prepareFlowQuery()
stmt, args, err := query.Where(
sq.Eq{
FlowsTriggersColumnFlowType.identifier(): flowType,
@@ -68,7 +68,7 @@ func (q *Queries) GetFlow(ctx context.Context, flowType domain.FlowType, orgID s
}
func (q *Queries) GetActionsByFlowAndTriggerType(ctx context.Context, flowType domain.FlowType, triggerType domain.TriggerType, orgID string) ([]*Action, error) {
stmt, scan := q.prepareTriggerActionsQuery()
stmt, scan := prepareTriggerActionsQuery()
query, args, err := stmt.Where(
sq.Eq{
FlowsTriggersColumnFlowType.identifier(): flowType,
@@ -88,42 +88,48 @@ func (q *Queries) GetActionsByFlowAndTriggerType(ctx context.Context, flowType d
}
func (q *Queries) GetFlowTypesOfActionID(ctx context.Context, actionID string) ([]domain.FlowType, error) {
stmt, args, err := sq.StatementBuilder.
Select(FlowsTriggersColumnFlowType.identifier()).
From(flowsTriggersTable.identifier()).
Where(sq.Eq{
stmt, scan := prepareFlowTypesQuery()
query, args, err := stmt.Where(
sq.Eq{
FlowsTriggersColumnActionID.identifier(): actionID,
}).
PlaceholderFormat(sq.Dollar).
ToSql()
},
).ToSql()
if err != nil {
return nil, errors.ThrowInvalidArgument(err, "QUERY-Dh311", "Errors.Query.InvalidRequest")
}
rows, err := q.client.QueryContext(ctx, stmt, args...)
rows, err := q.client.QueryContext(ctx, query, args...)
if err != nil {
return nil, errors.ThrowInternal(err, "QUERY-Bhj4w", "Errors.Internal")
}
flowTypes := make([]domain.FlowType, 0)
for rows.Next() {
var flowType domain.FlowType
err := rows.Scan(
&flowType,
)
if err != nil {
return nil, err
}
flowTypes = append(flowTypes, flowType)
}
if err := rows.Close(); err != nil {
return nil, errors.ThrowInternal(err, "QUERY-Fbgnh", "Errors.Query.CloseRows")
}
return flowTypes, nil
return scan(rows)
}
func (q *Queries) prepareTriggerActionsQuery() (sq.SelectBuilder, func(*sql.Rows) ([]*Action, error)) {
func prepareFlowTypesQuery() (sq.SelectBuilder, func(*sql.Rows) ([]domain.FlowType, error)) {
return sq.Select(
FlowsTriggersColumnFlowType.identifier(),
).
From(flowsTriggersTable.identifier()).
PlaceholderFormat(sq.Dollar),
func(rows *sql.Rows) ([]domain.FlowType, error) {
types := []domain.FlowType{}
for rows.Next() {
var flowType domain.FlowType
err := rows.Scan(
&flowType,
)
if err != nil {
return nil, err
}
types = append(types, flowType)
}
return types, nil
}
}
func prepareTriggerActionsQuery() (sq.SelectBuilder, func(*sql.Rows) ([]*Action, error)) {
return sq.Select(
ActionColumnID.identifier(),
ActionColumnCreationDate.identifier(),
@@ -133,8 +139,6 @@ func (q *Queries) prepareTriggerActionsQuery() (sq.SelectBuilder, func(*sql.Rows
ActionColumnSequence.identifier(),
ActionColumnName.identifier(),
ActionColumnScript.identifier(),
FlowsTriggersColumnTriggerType.identifier(),
FlowsTriggersColumnTriggerSequence.identifier(),
).
From(flowsTriggersTable.name).
LeftJoin(join(ActionColumnID, FlowsTriggersColumnActionID)).
@@ -143,8 +147,6 @@ func (q *Queries) prepareTriggerActionsQuery() (sq.SelectBuilder, func(*sql.Rows
actions := make([]*Action, 0)
for rows.Next() {
action := new(Action)
var triggerType domain.TriggerType
var triggerSequence int
err := rows.Scan(
&action.ID,
&action.CreationDate,
@@ -154,8 +156,6 @@ func (q *Queries) prepareTriggerActionsQuery() (sq.SelectBuilder, func(*sql.Rows
&action.Sequence,
&action.Name,
&action.Script,
&triggerType,
&triggerSequence,
)
if err != nil {
return nil, err
@@ -171,7 +171,7 @@ func (q *Queries) prepareTriggerActionsQuery() (sq.SelectBuilder, func(*sql.Rows
}
}
func (q *Queries) prepareFlowQuery(flowType domain.FlowType) (sq.SelectBuilder, func(*sql.Rows) (*Flow, error)) {
func prepareFlowQuery() (sq.SelectBuilder, func(*sql.Rows) (*Flow, error)) {
return sq.Select(
ActionColumnID.identifier(),
ActionColumnCreationDate.identifier(),
@@ -183,35 +183,59 @@ func (q *Queries) prepareFlowQuery(flowType domain.FlowType) (sq.SelectBuilder,
ActionColumnScript.identifier(),
FlowsTriggersColumnTriggerType.identifier(),
FlowsTriggersColumnTriggerSequence.identifier(),
FlowsTriggersColumnFlowType.identifier(),
).
From(flowsTriggersTable.name).
LeftJoin(join(ActionColumnID, FlowsTriggersColumnActionID)).
PlaceholderFormat(sq.Dollar),
func(rows *sql.Rows) (*Flow, error) {
flow := &Flow{
Type: flowType,
TriggerActions: make(map[domain.TriggerType][]*Action),
}
for rows.Next() {
action := new(Action)
var triggerType domain.TriggerType
var triggerSequence int
// action := new(Action)
var (
actionID sql.NullString
actionCreationDate pq.NullTime
actionChangeDate pq.NullTime
actionResourceOwner sql.NullString
actionSequence sql.NullInt64
actionName sql.NullString
actionScript sql.NullString
triggerType domain.TriggerType
triggerSequence int
flowType domain.FlowType
)
err := rows.Scan(
&action.ID,
&action.CreationDate,
&action.ChangeDate,
&action.ResourceOwner,
&actionID,
&actionCreationDate,
&actionChangeDate,
&actionResourceOwner,
//&action.State, //TODO: state in next release
&action.Sequence,
&action.Name,
&action.Script,
&actionSequence,
&actionName,
&actionScript,
&triggerType,
&triggerSequence,
&flowType,
)
if err != nil {
return nil, err
}
flow.TriggerActions[triggerType] = append(flow.TriggerActions[triggerType], action)
flow.Type = flowType
if !actionID.Valid {
continue
}
flow.TriggerActions[triggerType] = append(flow.TriggerActions[triggerType], &Action{
ID: actionID.String,
CreationDate: actionCreationDate.Time,
ChangeDate: actionChangeDate.Time,
ResourceOwner: actionResourceOwner.String,
Sequence: uint64(actionSequence.Int64),
Name: actionName.String,
Script: actionScript.String,
})
}
if err := rows.Close(); err != nil {