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/errors"
"github.com/caos/zitadel/internal/eventstore"
"github.com/caos/zitadel/internal/eventstore/handler"
@@ -12,18 +11,51 @@ import (
"github.com/caos/zitadel/internal/repository/project"
)
const (
SecretGeneratorProjectionTable = "projections.secret_generators"
SecretGeneratorColumnGeneratorType = "generator_type"
SecretGeneratorColumnAggregateID = "aggregate_id"
SecretGeneratorColumnCreationDate = "creation_date"
SecretGeneratorColumnChangeDate = "change_date"
SecretGeneratorColumnSequence = "sequence"
SecretGeneratorColumnResourceOwner = "resource_owner"
SecretGeneratorColumnInstanceID = "instance_id"
SecretGeneratorColumnLength = "length"
SecretGeneratorColumnExpiry = "expiry"
SecretGeneratorColumnIncludeLowerLetters = "include_lower_letters"
SecretGeneratorColumnIncludeUpperLetters = "include_upper_letters"
SecretGeneratorColumnIncludeDigits = "include_digits"
SecretGeneratorColumnIncludeSymbols = "include_symbols"
)
type SecretGeneratorProjection struct {
crdb.StatementHandler
}
const (
SecretGeneratorProjectionTable = "zitadel.projections.secret_generators"
)
func NewSecretGeneratorProjection(ctx context.Context, config crdb.StatementHandlerConfig) *SecretGeneratorProjection {
p := new(SecretGeneratorProjection)
config.ProjectionName = SecretGeneratorProjectionTable
config.Reducers = p.reducers()
config.InitCheck = crdb.NewTableCheck(
crdb.NewTable([]*crdb.Column{
crdb.NewColumn(SecretGeneratorColumnGeneratorType, crdb.ColumnTypeText),
crdb.NewColumn(SecretGeneratorColumnAggregateID, crdb.ColumnTypeText),
crdb.NewColumn(SecretGeneratorColumnCreationDate, crdb.ColumnTypeTimestamp),
crdb.NewColumn(SecretGeneratorColumnChangeDate, crdb.ColumnTypeTimestamp),
crdb.NewColumn(SecretGeneratorColumnSequence, crdb.ColumnTypeInt64),
crdb.NewColumn(SecretGeneratorColumnResourceOwner, crdb.ColumnTypeText),
crdb.NewColumn(SecretGeneratorColumnInstanceID, crdb.ColumnTypeText),
crdb.NewColumn(SecretGeneratorColumnLength, crdb.ColumnTypeInt64),
crdb.NewColumn(SecretGeneratorColumnExpiry, crdb.ColumnTypeInt64),
crdb.NewColumn(SecretGeneratorColumnIncludeLowerLetters, crdb.ColumnTypeBool),
crdb.NewColumn(SecretGeneratorColumnIncludeUpperLetters, crdb.ColumnTypeBool),
crdb.NewColumn(SecretGeneratorColumnIncludeDigits, crdb.ColumnTypeBool),
crdb.NewColumn(SecretGeneratorColumnIncludeSymbols, crdb.ColumnTypeBool),
},
crdb.NewPrimaryKey(SecretGeneratorColumnInstanceID, SecretGeneratorColumnGeneratorType, SecretGeneratorColumnAggregateID),
),
)
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
return p
}
@@ -50,26 +82,10 @@ func (p *SecretGeneratorProjection) reducers() []handler.AggregateReducer {
}
}
const (
SecretGeneratorColumnGeneratorType = "generator_type"
SecretGeneratorColumnAggregateID = "aggregate_id"
SecretGeneratorColumnCreationDate = "creation_date"
SecretGeneratorColumnChangeDate = "change_date"
SecretGeneratorColumnResourceOwner = "resource_owner"
SecretGeneratorColumnSequence = "sequence"
SecretGeneratorColumnLength = "length"
SecretGeneratorColumnExpiry = "expiry"
SecretGeneratorColumnIncludeLowerLetters = "include_lower_letters"
SecretGeneratorColumnIncludeUpperLetters = "include_upper_letters"
SecretGeneratorColumnIncludeDigits = "include_digits"
SecretGeneratorColumnIncludeSymbols = "include_symbols"
)
func (p *SecretGeneratorProjection) reduceSecretGeneratorAdded(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*iam.SecretGeneratorAddedEvent)
if !ok {
logging.LogWithFields("HANDL-nf9sl", "seq", event.Sequence(), "expectedType", iam.SecretGeneratorAddedEventType).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "HANDL-sk99F", "reduce.wrong.event.type")
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-sk99F", "reduce.wrong.event.type %s", iam.SecretGeneratorAddedEventType)
}
return crdb.NewCreateStatement(
e,
@@ -79,6 +95,7 @@ func (p *SecretGeneratorProjection) reduceSecretGeneratorAdded(event eventstore.
handler.NewCol(SecretGeneratorColumnCreationDate, e.CreationDate()),
handler.NewCol(SecretGeneratorColumnChangeDate, e.CreationDate()),
handler.NewCol(SecretGeneratorColumnResourceOwner, e.Aggregate().ResourceOwner),
handler.NewCol(SecretGeneratorColumnInstanceID, e.Aggregate().InstanceID),
handler.NewCol(SecretGeneratorColumnSequence, e.Sequence()),
handler.NewCol(SecretGeneratorColumnLength, e.Length),
handler.NewCol(SecretGeneratorColumnExpiry, e.Expiry),
@@ -93,8 +110,7 @@ func (p *SecretGeneratorProjection) reduceSecretGeneratorAdded(event eventstore.
func (p *SecretGeneratorProjection) reduceSecretGeneratorChanged(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*iam.SecretGeneratorChangedEvent)
if !ok {
logging.LogWithFields("HANDL-sn9jd", "seq", event.Sequence(), "expected", iam.SecretGeneratorChangedEventType).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "HANDL-s00Fs", "reduce.wrong.event.type")
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-s00Fs", "reduce.wrong.event.type %s", iam.SecretGeneratorChangedEventType)
}
columns := make([]handler.Column, 0, 7)
@@ -131,8 +147,7 @@ func (p *SecretGeneratorProjection) reduceSecretGeneratorChanged(event eventstor
func (p *SecretGeneratorProjection) reduceSecretGeneratorRemoved(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*iam.SecretGeneratorRemovedEvent)
if !ok {
logging.LogWithFields("HANDL-30oEF", "seq", event.Sequence(), "expectedType", iam.SecretGeneratorRemovedEventType).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "HANDL-fmiIf", "reduce.wrong.event.type")
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-fmiIf", "reduce.wrong.event.type %s", iam.SecretGeneratorRemovedEventType)
}
return crdb.NewDeleteStatement(
e,