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/repository/settings"
@@ -14,18 +13,43 @@ import (
"github.com/caos/zitadel/internal/repository/iam"
)
const (
DebugNotificationProviderTable = "projections.notification_providers"
DebugNotificationProviderAggIDCol = "aggregate_id"
DebugNotificationProviderCreationDateCol = "creation_date"
DebugNotificationProviderChangeDateCol = "change_date"
DebugNotificationProviderSequenceCol = "sequence"
DebugNotificationProviderResourceOwnerCol = "resource_owner"
DebugNotificationProviderInstanceIDCol = "instance_id"
DebugNotificationProviderStateCol = "state"
DebugNotificationProviderTypeCol = "provider_type"
DebugNotificationProviderCompactCol = "compact"
)
type DebugNotificationProviderProjection struct {
crdb.StatementHandler
}
const (
DebugNotificationProviderTable = "zitadel.projections.notification_providers"
)
func NewDebugNotificationProviderProjection(ctx context.Context, config crdb.StatementHandlerConfig) *DebugNotificationProviderProjection {
p := &DebugNotificationProviderProjection{}
config.ProjectionName = DebugNotificationProviderTable
config.Reducers = p.reducers()
config.InitCheck = crdb.NewTableCheck(
crdb.NewTable([]*crdb.Column{
crdb.NewColumn(DebugNotificationProviderAggIDCol, crdb.ColumnTypeText),
crdb.NewColumn(DebugNotificationProviderCreationDateCol, crdb.ColumnTypeTimestamp),
crdb.NewColumn(DebugNotificationProviderChangeDateCol, crdb.ColumnTypeTimestamp),
crdb.NewColumn(DebugNotificationProviderSequenceCol, crdb.ColumnTypeInt64),
crdb.NewColumn(DebugNotificationProviderResourceOwnerCol, crdb.ColumnTypeText),
crdb.NewColumn(DebugNotificationProviderInstanceIDCol, crdb.ColumnTypeText),
crdb.NewColumn(DebugNotificationProviderStateCol, crdb.ColumnTypeEnum),
crdb.NewColumn(DebugNotificationProviderTypeCol, crdb.ColumnTypeEnum),
crdb.NewColumn(DebugNotificationProviderCompactCol, crdb.ColumnTypeBool),
},
crdb.NewPrimaryKey(DebugNotificationProviderInstanceIDCol, DebugNotificationProviderAggIDCol, DebugNotificationProviderTypeCol),
),
)
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
return p
}
@@ -64,17 +88,6 @@ func (p *DebugNotificationProviderProjection) reducers() []handler.AggregateRedu
}
}
const (
DebugNotificationProviderAggIDCol = "aggregate_id"
DebugNotificationProviderCreationDateCol = "creation_date"
DebugNotificationProviderChangeDateCol = "change_date"
DebugNotificationProviderSequenceCol = "sequence"
DebugNotificationProviderResourceOwnerCol = "resource_owner"
DebugNotificationProviderStateCol = "state"
DebugNotificationProviderTypeCol = "provider_type"
DebugNotificationProviderCompactCol = "compact"
)
func (p *DebugNotificationProviderProjection) reduceDebugNotificationProviderAdded(event eventstore.Event) (*handler.Statement, error) {
var providerEvent settings.DebugNotificationProviderAddedEvent
var providerType domain.NotificationProviderType
@@ -86,8 +99,7 @@ func (p *DebugNotificationProviderProjection) reduceDebugNotificationProviderAdd
providerEvent = e.DebugNotificationProviderAddedEvent
providerType = domain.NotificationProviderTypeLog
default:
logging.WithFields("seq", event.Sequence(), "expectedTypes", []eventstore.EventType{iam.DebugNotificationProviderFileAddedEventType, iam.DebugNotificationProviderLogAddedEventType}).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "HANDL-pYPxS", "reduce.wrong.event.type")
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-pYPxS", "reduce.wrong.event.type %v", []eventstore.EventType{iam.DebugNotificationProviderFileAddedEventType, iam.DebugNotificationProviderLogAddedEventType})
}
return crdb.NewCreateStatement(&providerEvent, []handler.Column{
@@ -96,6 +108,7 @@ func (p *DebugNotificationProviderProjection) reduceDebugNotificationProviderAdd
handler.NewCol(DebugNotificationProviderChangeDateCol, providerEvent.CreationDate()),
handler.NewCol(DebugNotificationProviderSequenceCol, providerEvent.Sequence()),
handler.NewCol(DebugNotificationProviderResourceOwnerCol, providerEvent.Aggregate().ResourceOwner),
handler.NewCol(DebugNotificationProviderInstanceIDCol, providerEvent.Aggregate().InstanceID),
handler.NewCol(DebugNotificationProviderStateCol, domain.NotificationProviderStateActive),
handler.NewCol(DebugNotificationProviderTypeCol, providerType),
handler.NewCol(DebugNotificationProviderCompactCol, providerEvent.Compact),
@@ -113,8 +126,7 @@ func (p *DebugNotificationProviderProjection) reduceDebugNotificationProviderCha
providerEvent = e.DebugNotificationProviderChangedEvent
providerType = domain.NotificationProviderTypeLog
default:
logging.WithFields("seq", event.Sequence(), "expectedTypes", []eventstore.EventType{iam.DebugNotificationProviderFileChangedEventType, iam.DebugNotificationProviderLogChangedEventType}).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "HANDL-pYPxS", "reduce.wrong.event.type")
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-pYPxS", "reduce.wrong.event.type %v", []eventstore.EventType{iam.DebugNotificationProviderFileChangedEventType, iam.DebugNotificationProviderLogChangedEventType})
}
cols := []handler.Column{
@@ -146,8 +158,7 @@ func (p *DebugNotificationProviderProjection) reduceDebugNotificationProviderRem
providerEvent = e.DebugNotificationProviderRemovedEvent
providerType = domain.NotificationProviderTypeLog
default:
logging.WithFields("seq", event.Sequence(), "expectedTypes", []eventstore.EventType{iam.DebugNotificationProviderFileRemovedEventType, iam.DebugNotificationProviderLogRemovedEventType}).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "HANDL-dow9f", "reduce.wrong.event.type")
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-dow9f", "reduce.wrong.event.type %v", []eventstore.EventType{iam.DebugNotificationProviderFileRemovedEventType, iam.DebugNotificationProviderLogRemovedEventType})
}
return crdb.NewDeleteStatement(