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,8 +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,11 +12,12 @@ import (
)
const (
ActionTable = "zitadel.projections.actions"
ActionTable = "projections.actions"
ActionIDCol = "id"
ActionCreationDateCol = "creation_date"
ActionChangeDateCol = "change_date"
ActionResourceOwnerCol = "resource_owner"
ActionInstanceIDCol = "instance_id"
ActionStateCol = "action_state"
ActionSequenceCol = "sequence"
ActionNameCol = "name"
@@ -35,6 +34,24 @@ func NewActionProjection(ctx context.Context, config crdb.StatementHandlerConfig
p := new(ActionProjection)
config.ProjectionName = ActionTable
config.Reducers = p.reducers()
config.InitCheck = crdb.NewTableCheck(
crdb.NewTable([]*crdb.Column{
crdb.NewColumn(ActionIDCol, crdb.ColumnTypeText),
crdb.NewColumn(ActionCreationDateCol, crdb.ColumnTypeTimestamp),
crdb.NewColumn(ActionChangeDateCol, crdb.ColumnTypeTimestamp),
crdb.NewColumn(ActionResourceOwnerCol, crdb.ColumnTypeText),
crdb.NewColumn(ActionInstanceIDCol, crdb.ColumnTypeText),
crdb.NewColumn(ActionStateCol, crdb.ColumnTypeEnum),
crdb.NewColumn(ActionSequenceCol, crdb.ColumnTypeInt64),
crdb.NewColumn(ActionNameCol, crdb.ColumnTypeText),
crdb.NewColumn(ActionScriptCol, crdb.ColumnTypeText, crdb.Default("")),
crdb.NewColumn(ActionTimeoutCol, crdb.ColumnTypeInt64, crdb.Default(0)),
crdb.NewColumn(ActionAllowedToFailCol, crdb.ColumnTypeBool, crdb.Default(false)),
},
crdb.NewPrimaryKey(ActionInstanceIDCol, ActionIDCol),
crdb.NewIndex("ro_idx", []string{ActionResourceOwnerCol}),
),
)
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
return p
}
@@ -72,8 +89,7 @@ func (p *ActionProjection) reducers() []handler.AggregateReducer {
func (p *ActionProjection) reduceActionAdded(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*action.AddedEvent)
if !ok {
logging.LogWithFields("HANDL-Sgg31", "seq", event.Sequence, "expectedType", action.AddedEventType).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "HANDL-Dff21", "reduce.wrong.event.type")
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-Dff21", "reduce.wrong.event.type% s", action.AddedEventType)
}
return crdb.NewCreateStatement(
e,
@@ -82,6 +98,7 @@ func (p *ActionProjection) reduceActionAdded(event eventstore.Event) (*handler.S
handler.NewCol(ActionCreationDateCol, e.CreationDate()),
handler.NewCol(ActionChangeDateCol, e.CreationDate()),
handler.NewCol(ActionResourceOwnerCol, e.Aggregate().ResourceOwner),
handler.NewCol(ActionInstanceIDCol, e.Aggregate().InstanceID),
handler.NewCol(ActionSequenceCol, e.Sequence()),
handler.NewCol(ActionNameCol, e.Name),
handler.NewCol(ActionScriptCol, e.Script),
@@ -95,8 +112,7 @@ func (p *ActionProjection) reduceActionAdded(event eventstore.Event) (*handler.S
func (p *ActionProjection) reduceActionChanged(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*action.ChangedEvent)
if !ok {
logging.LogWithFields("HANDL-Dg2th", "seq", event.Sequence, "expected", action.ChangedEventType).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "HANDL-Gg43d", "reduce.wrong.event.type")
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-Gg43d", "reduce.wrong.event.type %s", action.ChangedEventType)
}
values := []handler.Column{
handler.NewCol(ActionChangeDateCol, e.CreationDate()),
@@ -126,8 +142,7 @@ func (p *ActionProjection) reduceActionChanged(event eventstore.Event) (*handler
func (p *ActionProjection) reduceActionDeactivated(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*action.DeactivatedEvent)
if !ok {
logging.LogWithFields("HANDL-Fhhjd", "seq", event.Sequence, "expectedType", action.DeactivatedEventType).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "HANDL-Fgh32", "reduce.wrong.event.type")
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-Fgh32", "reduce.wrong.event.type %s", action.DeactivatedEventType)
}
return crdb.NewUpdateStatement(
e,
@@ -145,8 +160,7 @@ func (p *ActionProjection) reduceActionDeactivated(event eventstore.Event) (*han
func (p *ActionProjection) reduceActionReactivated(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*action.ReactivatedEvent)
if !ok {
logging.LogWithFields("HANDL-Fg4r3", "seq", event.Sequence, "expectedType", action.ReactivatedEventType).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "HANDL-hwdqa", "reduce.wrong.event.type")
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-hwdqa", "reduce.wrong.event.type% s", action.ReactivatedEventType)
}
return crdb.NewUpdateStatement(
e,
@@ -164,8 +178,7 @@ func (p *ActionProjection) reduceActionReactivated(event eventstore.Event) (*han
func (p *ActionProjection) reduceActionRemoved(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*action.RemovedEvent)
if !ok {
logging.LogWithFields("HANDL-Dgwh2", "seq", event.Sequence, "expectedType", action.RemovedEventType).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "HANDL-Dgh2d", "reduce.wrong.event.type")
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-Dgh2d", "reduce.wrong.event.type% s", action.RemovedEventType)
}
return crdb.NewDeleteStatement(
e,