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

@@ -3,7 +3,6 @@ package projection
import (
"context"
"github.com/caos/logging"
"github.com/caos/zitadel/internal/domain"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore"
@@ -14,28 +13,45 @@ import (
"github.com/caos/zitadel/internal/repository/policy"
)
type LockoutPolicyProjection struct {
crdb.StatementHandler
}
const (
LockoutPolicyTable = "zitadel.projections.lockout_policies"
LockoutPolicyTable = "projections.lockout_policies"
LockoutPolicyIDCol = "id"
LockoutPolicyCreationDateCol = "creation_date"
LockoutPolicyChangeDateCol = "change_date"
LockoutPolicySequenceCol = "sequence"
LockoutPolicyIDCol = "id"
LockoutPolicyStateCol = "state"
LockoutPolicyMaxPasswordAttemptsCol = "max_password_attempts"
LockoutPolicyShowLockOutFailuresCol = "show_failure"
LockoutPolicyIsDefaultCol = "is_default"
LockoutPolicyResourceOwnerCol = "resource_owner"
LockoutPolicyInstanceIDCol = "instance_id"
LockoutPolicyMaxPasswordAttemptsCol = "max_password_attempts"
LockoutPolicyShowLockOutFailuresCol = "show_failure"
)
type LockoutPolicyProjection struct {
crdb.StatementHandler
}
func NewLockoutPolicyProjection(ctx context.Context, config crdb.StatementHandlerConfig) *LockoutPolicyProjection {
p := new(LockoutPolicyProjection)
config.ProjectionName = LockoutPolicyTable
config.Reducers = p.reducers()
config.InitCheck = crdb.NewTableCheck(
crdb.NewTable([]*crdb.Column{
crdb.NewColumn(LockoutPolicyIDCol, crdb.ColumnTypeText),
crdb.NewColumn(LockoutPolicyCreationDateCol, crdb.ColumnTypeTimestamp),
crdb.NewColumn(LockoutPolicyChangeDateCol, crdb.ColumnTypeTimestamp),
crdb.NewColumn(LockoutPolicySequenceCol, crdb.ColumnTypeInt64),
crdb.NewColumn(LockoutPolicyStateCol, crdb.ColumnTypeEnum),
crdb.NewColumn(LockoutPolicyIsDefaultCol, crdb.ColumnTypeBool, crdb.Default(false)),
crdb.NewColumn(LockoutPolicyResourceOwnerCol, crdb.ColumnTypeText),
crdb.NewColumn(LockoutPolicyInstanceIDCol, crdb.ColumnTypeText),
crdb.NewColumn(LockoutPolicyMaxPasswordAttemptsCol, crdb.ColumnTypeInt64),
crdb.NewColumn(LockoutPolicyShowLockOutFailuresCol, crdb.ColumnTypeBool),
},
crdb.NewPrimaryKey(LockoutPolicyInstanceIDCol, LockoutPolicyIDCol),
),
)
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
return p
}
@@ -86,8 +102,7 @@ func (p *LockoutPolicyProjection) reduceAdded(event eventstore.Event) (*handler.
policyEvent = e.LockoutPolicyAddedEvent
isDefault = true
default:
logging.LogWithFields("PROJE-uFqFM", "seq", event.Sequence(), "expectedTypes", []eventstore.EventType{org.LockoutPolicyAddedEventType, iam.LockoutPolicyAddedEventType}).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "PROJE-d8mZO", "reduce.wrong.event.type")
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-d8mZO", "reduce.wrong.event.type, %v", []eventstore.EventType{org.LockoutPolicyAddedEventType, iam.LockoutPolicyAddedEventType})
}
return crdb.NewCreateStatement(
&policyEvent,
@@ -101,6 +116,7 @@ func (p *LockoutPolicyProjection) reduceAdded(event eventstore.Event) (*handler.
handler.NewCol(LockoutPolicyShowLockOutFailuresCol, policyEvent.ShowLockOutFailures),
handler.NewCol(LockoutPolicyIsDefaultCol, isDefault),
handler.NewCol(LockoutPolicyResourceOwnerCol, policyEvent.Aggregate().ResourceOwner),
handler.NewCol(LockoutPolicyInstanceIDCol, policyEvent.Aggregate().InstanceID),
}), nil
}
@@ -112,8 +128,7 @@ func (p *LockoutPolicyProjection) reduceChanged(event eventstore.Event) (*handle
case *iam.LockoutPolicyChangedEvent:
policyEvent = e.LockoutPolicyChangedEvent
default:
logging.LogWithFields("PROJE-iIkej", "seq", event.Sequence(), "expectedTypes", []eventstore.EventType{org.LockoutPolicyChangedEventType, iam.LockoutPolicyChangedEventType}).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "PROJE-pT3mQ", "reduce.wrong.event.type")
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-pT3mQ", "reduce.wrong.event.type, %v", []eventstore.EventType{org.LockoutPolicyChangedEventType, iam.LockoutPolicyChangedEventType})
}
cols := []handler.Column{
handler.NewCol(LockoutPolicyChangeDateCol, policyEvent.CreationDate()),
@@ -136,8 +151,7 @@ func (p *LockoutPolicyProjection) reduceChanged(event eventstore.Event) (*handle
func (p *LockoutPolicyProjection) reduceRemoved(event eventstore.Event) (*handler.Statement, error) {
policyEvent, ok := event.(*org.LockoutPolicyRemovedEvent)
if !ok {
logging.LogWithFields("PROJE-U5cys", "seq", event.Sequence(), "expectedType", org.LockoutPolicyRemovedEventType).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "PROJE-Bqut9", "reduce.wrong.event.type")
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-Bqut9", "reduce.wrong.event.type %s", org.LockoutPolicyRemovedEventType)
}
return crdb.NewDeleteStatement(
policyEvent,