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,18 +13,51 @@ import (
"github.com/caos/zitadel/internal/repository/policy"
)
const (
PasswordComplexityTable = "projections.password_complexity_policies"
ComplexityPolicyIDCol = "id"
ComplexityPolicyCreationDateCol = "creation_date"
ComplexityPolicyChangeDateCol = "change_date"
ComplexityPolicySequenceCol = "sequence"
ComplexityPolicyStateCol = "state"
ComplexityPolicyIsDefaultCol = "is_default"
ComplexityPolicyResourceOwnerCol = "resource_owner"
ComplexityPolicyInstanceIDCol = "instance_id"
ComplexityPolicyMinLengthCol = "min_length"
ComplexityPolicyHasLowercaseCol = "has_lowercase"
ComplexityPolicyHasUppercaseCol = "has_uppercase"
ComplexityPolicyHasSymbolCol = "has_symbol"
ComplexityPolicyHasNumberCol = "has_number"
)
type PasswordComplexityProjection struct {
crdb.StatementHandler
}
const (
PasswordComplexityTable = "zitadel.projections.password_complexity_policies"
)
func NewPasswordComplexityProjection(ctx context.Context, config crdb.StatementHandlerConfig) *PasswordComplexityProjection {
p := new(PasswordComplexityProjection)
config.ProjectionName = PasswordComplexityTable
config.Reducers = p.reducers()
config.InitCheck = crdb.NewTableCheck(
crdb.NewTable([]*crdb.Column{
crdb.NewColumn(ComplexityPolicyIDCol, crdb.ColumnTypeText),
crdb.NewColumn(ComplexityPolicyCreationDateCol, crdb.ColumnTypeTimestamp),
crdb.NewColumn(ComplexityPolicyChangeDateCol, crdb.ColumnTypeTimestamp),
crdb.NewColumn(ComplexityPolicySequenceCol, crdb.ColumnTypeInt64),
crdb.NewColumn(ComplexityPolicyStateCol, crdb.ColumnTypeEnum),
crdb.NewColumn(ComplexityPolicyIsDefaultCol, crdb.ColumnTypeBool, crdb.Default(false)),
crdb.NewColumn(ComplexityPolicyResourceOwnerCol, crdb.ColumnTypeText),
crdb.NewColumn(ComplexityPolicyInstanceIDCol, crdb.ColumnTypeText),
crdb.NewColumn(ComplexityPolicyMinLengthCol, crdb.ColumnTypeInt64),
crdb.NewColumn(ComplexityPolicyHasLowercaseCol, crdb.ColumnTypeBool),
crdb.NewColumn(ComplexityPolicyHasUppercaseCol, crdb.ColumnTypeBool),
crdb.NewColumn(ComplexityPolicyHasSymbolCol, crdb.ColumnTypeBool),
crdb.NewColumn(ComplexityPolicyHasNumberCol, crdb.ColumnTypeBool),
},
crdb.NewPrimaryKey(ComplexityPolicyInstanceIDCol, ComplexityPolicyIDCol),
),
)
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
return p
}
@@ -76,8 +108,7 @@ func (p *PasswordComplexityProjection) reduceAdded(event eventstore.Event) (*han
policyEvent = e.PasswordComplexityPolicyAddedEvent
isDefault = true
default:
logging.LogWithFields("PROJE-mP8AR", "seq", event.Sequence(), "expectedTypes", []eventstore.EventType{org.PasswordComplexityPolicyAddedEventType, iam.PasswordComplexityPolicyAddedEventType}).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "PROJE-KTHmJ", "reduce.wrong.event.type")
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-KTHmJ", "reduce.wrong.event.type %v", []eventstore.EventType{org.PasswordComplexityPolicyAddedEventType, iam.PasswordComplexityPolicyAddedEventType})
}
return crdb.NewCreateStatement(
&policyEvent,
@@ -93,6 +124,7 @@ func (p *PasswordComplexityProjection) reduceAdded(event eventstore.Event) (*han
handler.NewCol(ComplexityPolicyHasSymbolCol, policyEvent.HasSymbol),
handler.NewCol(ComplexityPolicyHasNumberCol, policyEvent.HasNumber),
handler.NewCol(ComplexityPolicyResourceOwnerCol, policyEvent.Aggregate().ResourceOwner),
handler.NewCol(ComplexityPolicyInstanceIDCol, policyEvent.Aggregate().InstanceID),
handler.NewCol(ComplexityPolicyIsDefaultCol, isDefault),
}), nil
}
@@ -105,8 +137,7 @@ func (p *PasswordComplexityProjection) reduceChanged(event eventstore.Event) (*h
case *iam.PasswordComplexityPolicyChangedEvent:
policyEvent = e.PasswordComplexityPolicyChangedEvent
default:
logging.LogWithFields("PROJE-L4UHn", "seq", event.Sequence(), "expectedTypes", []eventstore.EventType{org.PasswordComplexityPolicyChangedEventType, iam.PasswordComplexityPolicyChangedEventType}).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "PROJE-cf3Xb", "reduce.wrong.event.type")
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-cf3Xb", "reduce.wrong.event.type %v", []eventstore.EventType{org.PasswordComplexityPolicyChangedEventType, iam.PasswordComplexityPolicyChangedEventType})
}
cols := []handler.Column{
handler.NewCol(ComplexityPolicyChangeDateCol, policyEvent.CreationDate()),
@@ -138,8 +169,7 @@ func (p *PasswordComplexityProjection) reduceChanged(event eventstore.Event) (*h
func (p *PasswordComplexityProjection) reduceRemoved(event eventstore.Event) (*handler.Statement, error) {
policyEvent, ok := event.(*org.PasswordComplexityPolicyRemovedEvent)
if !ok {
logging.LogWithFields("PROJE-ibd0c", "seq", event.Sequence(), "expectedType", org.PasswordComplexityPolicyRemovedEventType).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "PROJE-wttCd", "reduce.wrong.event.type")
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-wttCd", "reduce.wrong.event.type %s", org.PasswordComplexityPolicyRemovedEventType)
}
return crdb.NewDeleteStatement(
policyEvent,
@@ -147,18 +177,3 @@ func (p *PasswordComplexityProjection) reduceRemoved(event eventstore.Event) (*h
handler.NewCond(ComplexityPolicyIDCol, policyEvent.Aggregate().ID),
}), nil
}
const (
ComplexityPolicyCreationDateCol = "creation_date"
ComplexityPolicyChangeDateCol = "change_date"
ComplexityPolicySequenceCol = "sequence"
ComplexityPolicyIDCol = "id"
ComplexityPolicyStateCol = "state"
ComplexityPolicyMinLengthCol = "min_length"
ComplexityPolicyHasLowercaseCol = "has_lowercase"
ComplexityPolicyHasUppercaseCol = "has_uppercase"
ComplexityPolicyHasSymbolCol = "has_symbol"
ComplexityPolicyHasNumberCol = "has_number"
ComplexityPolicyIsDefaultCol = "is_default"
ComplexityPolicyResourceOwnerCol = "resource_owner"
)