feat: projections auto create their tables (#3324)

* begin init checks for projections

* first projection checks

* debug notification providers with query fixes

* more projections and first index

* more projections

* more projections

* finish projections

* fix tests (remove db name)

* create tables in setup

* fix logging / error handling

* add tenant to views

* rename tenant to instance_id

* add instance_id to all projections

* add instance_id to all queries

* correct instance_id on projections

* add instance_id to failed_events

* use separate context for instance

* implement features projection

* implement features projection

* remove unique constraint from setup when migration failed

* add error to failed setup event

* add instance_id to primary keys

* fix IAM projection

* remove old migrations folder

* fix keysFromYAML test
This commit is contained in:
Livio Amstutz
2022-03-23 09:02:39 +01:00
committed by GitHub
parent 9e13b70a3d
commit 56b916a2b0
400 changed files with 6508 additions and 8890 deletions

View File

@@ -8,6 +8,8 @@ import (
sq "github.com/Masterminds/squirrel"
"github.com/lib/pq"
"github.com/caos/zitadel/internal/api/authz"
"github.com/caos/zitadel/internal/domain"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/query/projection"
@@ -21,6 +23,14 @@ var (
name: projection.FlowTypeCol,
table: flowsTriggersTable,
}
FlowsTriggersColumnChangeDate = Column{
name: projection.FlowChangeDateCol,
table: flowsTriggersTable,
}
FlowsTriggersColumnSequence = Column{
name: projection.FlowSequenceCol,
table: flowsTriggersTable,
}
FlowsTriggersColumnTriggerType = Column{
name: projection.FlowTriggerTypeCol,
table: flowsTriggersTable,
@@ -29,6 +39,10 @@ var (
name: projection.FlowResourceOwnerCol,
table: flowsTriggersTable,
}
FlowsTriggersColumnInstanceID = Column{
name: projection.FlowInstanceIDCol,
table: flowsTriggersTable,
}
FlowsTriggersColumnTriggerSequence = Column{
name: projection.FlowActionTriggerSequenceCol,
table: flowsTriggersTable,
@@ -40,10 +54,9 @@ var (
)
type Flow struct {
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
ChangeDate time.Time
ResourceOwner string
Sequence uint64
Type domain.FlowType
TriggerActions map[domain.TriggerType][]*Action
@@ -55,6 +68,7 @@ func (q *Queries) GetFlow(ctx context.Context, flowType domain.FlowType, orgID s
sq.Eq{
FlowsTriggersColumnFlowType.identifier(): flowType,
FlowsTriggersColumnResourceOwner.identifier(): orgID,
FlowsTriggersColumnInstanceID.identifier(): authz.GetInstance(ctx).ID,
}).ToSql()
if err != nil {
return nil, errors.ThrowInvalidArgument(err, "QUERY-HBRh3", "Errors.Query.InvalidRequest")
@@ -74,6 +88,7 @@ func (q *Queries) GetActiveActionsByFlowAndTriggerType(ctx context.Context, flow
FlowsTriggersColumnFlowType.identifier(): flowType,
FlowsTriggersColumnTriggerType.identifier(): triggerType,
FlowsTriggersColumnResourceOwner.identifier(): orgID,
FlowsTriggersColumnInstanceID.identifier(): authz.GetInstance(ctx).ID,
ActionColumnState.identifier(): domain.ActionStateActive,
},
).ToSql()
@@ -92,7 +107,8 @@ func (q *Queries) GetFlowTypesOfActionID(ctx context.Context, actionID string) (
stmt, scan := prepareFlowTypesQuery()
query, args, err := stmt.Where(
sq.Eq{
FlowsTriggersColumnActionID.identifier(): actionID,
FlowsTriggersColumnActionID.identifier(): actionID,
FlowsTriggersColumnInstanceID.identifier(): authz.GetInstance(ctx).ID,
},
).ToSql()
if err != nil {
@@ -185,6 +201,9 @@ func prepareFlowQuery() (sq.SelectBuilder, func(*sql.Rows) (*Flow, error)) {
FlowsTriggersColumnTriggerType.identifier(),
FlowsTriggersColumnTriggerSequence.identifier(),
FlowsTriggersColumnFlowType.identifier(),
FlowsTriggersColumnChangeDate.identifier(),
FlowsTriggersColumnSequence.identifier(),
FlowsTriggersColumnResourceOwner.identifier(),
).
From(flowsTriggersTable.name).
LeftJoin(join(ActionColumnID, FlowsTriggersColumnActionID)).
@@ -194,7 +213,6 @@ func prepareFlowQuery() (sq.SelectBuilder, func(*sql.Rows) (*Flow, error)) {
TriggerActions: make(map[domain.TriggerType][]*Action),
}
for rows.Next() {
// action := new(Action)
var (
actionID sql.NullString
actionCreationDate pq.NullTime
@@ -207,7 +225,6 @@ func prepareFlowQuery() (sq.SelectBuilder, func(*sql.Rows) (*Flow, error)) {
triggerType domain.TriggerType
triggerSequence int
flowType domain.FlowType
)
err := rows.Scan(
&actionID,
@@ -220,12 +237,14 @@ func prepareFlowQuery() (sq.SelectBuilder, func(*sql.Rows) (*Flow, error)) {
&actionScript,
&triggerType,
&triggerSequence,
&flowType,
&flow.Type,
&flow.ChangeDate,
&flow.Sequence,
&flow.ResourceOwner,
)
if err != nil {
return nil, err
}
flow.Type = flowType
if !actionID.Valid {
continue
}