mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-12 01:37:31 +00:00
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:
@@ -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,45 @@ import (
|
||||
"github.com/caos/zitadel/internal/repository/policy"
|
||||
)
|
||||
|
||||
const (
|
||||
PasswordAgeTable = "projections.password_age_policies"
|
||||
|
||||
AgePolicyIDCol = "id"
|
||||
AgePolicyCreationDateCol = "creation_date"
|
||||
AgePolicyChangeDateCol = "change_date"
|
||||
AgePolicySequenceCol = "sequence"
|
||||
AgePolicyStateCol = "state"
|
||||
AgePolicyIsDefaultCol = "is_default"
|
||||
AgePolicyResourceOwnerCol = "resource_owner"
|
||||
AgePolicyInstanceIDCol = "instance_id"
|
||||
AgePolicyExpireWarnDaysCol = "expire_warn_days"
|
||||
AgePolicyMaxAgeDaysCol = "max_age_days"
|
||||
)
|
||||
|
||||
type PasswordAgeProjection struct {
|
||||
crdb.StatementHandler
|
||||
}
|
||||
|
||||
const (
|
||||
PasswordAgeTable = "zitadel.projections.password_age_policies"
|
||||
)
|
||||
|
||||
func NewPasswordAgeProjection(ctx context.Context, config crdb.StatementHandlerConfig) *PasswordAgeProjection {
|
||||
p := new(PasswordAgeProjection)
|
||||
config.ProjectionName = PasswordAgeTable
|
||||
config.Reducers = p.reducers()
|
||||
config.InitCheck = crdb.NewTableCheck(
|
||||
crdb.NewTable([]*crdb.Column{
|
||||
crdb.NewColumn(AgePolicyIDCol, crdb.ColumnTypeText),
|
||||
crdb.NewColumn(AgePolicyCreationDateCol, crdb.ColumnTypeTimestamp),
|
||||
crdb.NewColumn(AgePolicyChangeDateCol, crdb.ColumnTypeTimestamp),
|
||||
crdb.NewColumn(AgePolicySequenceCol, crdb.ColumnTypeInt64),
|
||||
crdb.NewColumn(AgePolicyStateCol, crdb.ColumnTypeEnum),
|
||||
crdb.NewColumn(AgePolicyIsDefaultCol, crdb.ColumnTypeBool, crdb.Default(false)),
|
||||
crdb.NewColumn(AgePolicyResourceOwnerCol, crdb.ColumnTypeText),
|
||||
crdb.NewColumn(AgePolicyInstanceIDCol, crdb.ColumnTypeText),
|
||||
crdb.NewColumn(AgePolicyExpireWarnDaysCol, crdb.ColumnTypeInt64),
|
||||
crdb.NewColumn(AgePolicyMaxAgeDaysCol, crdb.ColumnTypeInt64),
|
||||
},
|
||||
crdb.NewPrimaryKey(AgePolicyInstanceIDCol, AgePolicyIDCol),
|
||||
),
|
||||
)
|
||||
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
|
||||
return p
|
||||
}
|
||||
@@ -76,8 +102,7 @@ func (p *PasswordAgeProjection) reduceAdded(event eventstore.Event) (*handler.St
|
||||
policyEvent = e.PasswordAgePolicyAddedEvent
|
||||
isDefault = true
|
||||
default:
|
||||
logging.LogWithFields("PROJE-stxcL", "seq", event.Sequence(), "expectedTypes", []eventstore.EventType{org.PasswordAgePolicyAddedEventType, iam.PasswordAgePolicyAddedEventType}).Error("wrong event type")
|
||||
return nil, errors.ThrowInvalidArgument(nil, "PROJE-CJqF0", "reduce.wrong.event.type")
|
||||
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-CJqF0", "reduce.wrong.event.type %v", []eventstore.EventType{org.PasswordAgePolicyAddedEventType, iam.PasswordAgePolicyAddedEventType})
|
||||
}
|
||||
return crdb.NewCreateStatement(
|
||||
&policyEvent,
|
||||
@@ -91,6 +116,7 @@ func (p *PasswordAgeProjection) reduceAdded(event eventstore.Event) (*handler.St
|
||||
handler.NewCol(AgePolicyMaxAgeDaysCol, policyEvent.MaxAgeDays),
|
||||
handler.NewCol(AgePolicyIsDefaultCol, isDefault),
|
||||
handler.NewCol(AgePolicyResourceOwnerCol, policyEvent.Aggregate().ResourceOwner),
|
||||
handler.NewCol(AgePolicyInstanceIDCol, policyEvent.Aggregate().InstanceID),
|
||||
}), nil
|
||||
}
|
||||
|
||||
@@ -102,8 +128,7 @@ func (p *PasswordAgeProjection) reduceChanged(event eventstore.Event) (*handler.
|
||||
case *iam.PasswordAgePolicyChangedEvent:
|
||||
policyEvent = e.PasswordAgePolicyChangedEvent
|
||||
default:
|
||||
logging.LogWithFields("PROJE-EZ53p", "seq", event.Sequence(), "expectedTypes", []eventstore.EventType{org.PasswordAgePolicyChangedEventType, iam.PasswordAgePolicyChangedEventType}).Error("wrong event type")
|
||||
return nil, errors.ThrowInvalidArgument(nil, "PROJE-i7FZt", "reduce.wrong.event.type")
|
||||
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-i7FZt", "reduce.wrong.event.type %v", []eventstore.EventType{org.PasswordAgePolicyChangedEventType, iam.PasswordAgePolicyChangedEventType})
|
||||
}
|
||||
cols := []handler.Column{
|
||||
handler.NewCol(AgePolicyChangeDateCol, policyEvent.CreationDate()),
|
||||
@@ -126,8 +151,7 @@ func (p *PasswordAgeProjection) reduceChanged(event eventstore.Event) (*handler.
|
||||
func (p *PasswordAgeProjection) reduceRemoved(event eventstore.Event) (*handler.Statement, error) {
|
||||
policyEvent, ok := event.(*org.PasswordAgePolicyRemovedEvent)
|
||||
if !ok {
|
||||
logging.LogWithFields("PROJE-iwqfN", "seq", event.Sequence(), "expectedType", org.PasswordAgePolicyRemovedEventType).Error("wrong event type")
|
||||
return nil, errors.ThrowInvalidArgument(nil, "PROJE-EtHWB", "reduce.wrong.event.type")
|
||||
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-EtHWB", "reduce.wrong.event.type %s", org.PasswordAgePolicyRemovedEventType)
|
||||
}
|
||||
return crdb.NewDeleteStatement(
|
||||
policyEvent,
|
||||
@@ -135,15 +159,3 @@ func (p *PasswordAgeProjection) reduceRemoved(event eventstore.Event) (*handler.
|
||||
handler.NewCond(AgePolicyIDCol, policyEvent.Aggregate().ID),
|
||||
}), nil
|
||||
}
|
||||
|
||||
const (
|
||||
AgePolicyCreationDateCol = "creation_date"
|
||||
AgePolicyChangeDateCol = "change_date"
|
||||
AgePolicySequenceCol = "sequence"
|
||||
AgePolicyIDCol = "id"
|
||||
AgePolicyStateCol = "state"
|
||||
AgePolicyExpireWarnDaysCol = "expire_warn_days"
|
||||
AgePolicyMaxAgeDaysCol = "max_age_days"
|
||||
AgePolicyIsDefaultCol = "is_default"
|
||||
AgePolicyResourceOwnerCol = "resource_owner"
|
||||
)
|
||||
|
Reference in New Issue
Block a user