mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-26 06:18:46 +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,8 +3,6 @@ package projection
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/caos/logging"
|
||||
|
||||
"github.com/caos/zitadel/internal/domain"
|
||||
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
@@ -16,18 +14,79 @@ import (
|
||||
"github.com/caos/zitadel/internal/repository/org"
|
||||
)
|
||||
|
||||
const (
|
||||
FeatureTable = "projections.features"
|
||||
|
||||
FeatureAggregateIDCol = "aggregate_id"
|
||||
FeatureInstanceIDCol = "instance_id"
|
||||
FeatureChangeDateCol = "change_date"
|
||||
FeatureSequenceCol = "sequence"
|
||||
FeatureIsDefaultCol = "is_default"
|
||||
FeatureTierNameCol = "tier_name"
|
||||
FeatureTierDescriptionCol = "tier_description"
|
||||
FeatureStateCol = "state"
|
||||
FeatureStateDescriptionCol = "state_description"
|
||||
FeatureAuditLogRetentionCol = "audit_log_retention"
|
||||
FeatureLoginPolicyFactorsCol = "login_policy_factors"
|
||||
FeatureLoginPolicyIDPCol = "login_policy_idp"
|
||||
FeatureLoginPolicyPasswordlessCol = "login_policy_passwordless"
|
||||
FeatureLoginPolicyRegistrationCol = "login_policy_registration"
|
||||
FeatureLoginPolicyUsernameLoginCol = "login_policy_username_login"
|
||||
FeatureLoginPolicyPasswordResetCol = "login_policy_password_reset"
|
||||
FeaturePasswordComplexityPolicyCol = "password_complexity_policy"
|
||||
FeatureLabelPolicyPrivateLabelCol = "label_policy_private_label"
|
||||
FeatureLabelPolicyWatermarkCol = "label_policy_watermark"
|
||||
FeatureCustomDomainCol = "custom_domain"
|
||||
FeaturePrivacyPolicyCol = "privacy_policy"
|
||||
FeatureMetadataUserCol = "metadata_user"
|
||||
FeatureCustomTextMessageCol = "custom_text_message"
|
||||
FeatureCustomTextLoginCol = "custom_text_login"
|
||||
FeatureLockoutPolicyCol = "lockout_policy"
|
||||
FeatureActionsAllowedCol = "actions_allowed"
|
||||
FeatureMaxActionsCol = "max_actions"
|
||||
)
|
||||
|
||||
type FeatureProjection struct {
|
||||
crdb.StatementHandler
|
||||
}
|
||||
|
||||
const (
|
||||
FeatureTable = "zitadel.projections.features"
|
||||
)
|
||||
|
||||
func NewFeatureProjection(ctx context.Context, config crdb.StatementHandlerConfig) *FeatureProjection {
|
||||
p := new(FeatureProjection)
|
||||
config.ProjectionName = FeatureTable
|
||||
config.Reducers = p.reducers()
|
||||
config.InitCheck = crdb.NewTableCheck(
|
||||
crdb.NewTable([]*crdb.Column{
|
||||
crdb.NewColumn(FeatureAggregateIDCol, crdb.ColumnTypeText),
|
||||
crdb.NewColumn(FeatureInstanceIDCol, crdb.ColumnTypeText),
|
||||
crdb.NewColumn(FeatureChangeDateCol, crdb.ColumnTypeTimestamp),
|
||||
crdb.NewColumn(FeatureSequenceCol, crdb.ColumnTypeInt64),
|
||||
crdb.NewColumn(FeatureIsDefaultCol, crdb.ColumnTypeBool, crdb.Default(false)),
|
||||
crdb.NewColumn(FeatureTierNameCol, crdb.ColumnTypeText),
|
||||
crdb.NewColumn(FeatureTierDescriptionCol, crdb.ColumnTypeText),
|
||||
crdb.NewColumn(FeatureStateCol, crdb.ColumnTypeEnum, crdb.Default(0)),
|
||||
crdb.NewColumn(FeatureStateDescriptionCol, crdb.ColumnTypeText),
|
||||
crdb.NewColumn(FeatureAuditLogRetentionCol, crdb.ColumnTypeInt64, crdb.Default(0)),
|
||||
crdb.NewColumn(FeatureLoginPolicyFactorsCol, crdb.ColumnTypeBool, crdb.Default(false)),
|
||||
crdb.NewColumn(FeatureLoginPolicyIDPCol, crdb.ColumnTypeBool, crdb.Default(false)),
|
||||
crdb.NewColumn(FeatureLoginPolicyPasswordlessCol, crdb.ColumnTypeBool, crdb.Default(false)),
|
||||
crdb.NewColumn(FeatureLoginPolicyRegistrationCol, crdb.ColumnTypeBool, crdb.Default(false)),
|
||||
crdb.NewColumn(FeatureLoginPolicyUsernameLoginCol, crdb.ColumnTypeBool, crdb.Default(false)),
|
||||
crdb.NewColumn(FeatureLoginPolicyPasswordResetCol, crdb.ColumnTypeBool, crdb.Default(false)),
|
||||
crdb.NewColumn(FeaturePasswordComplexityPolicyCol, crdb.ColumnTypeBool, crdb.Default(false)),
|
||||
crdb.NewColumn(FeatureLabelPolicyPrivateLabelCol, crdb.ColumnTypeBool, crdb.Default(false)),
|
||||
crdb.NewColumn(FeatureLabelPolicyWatermarkCol, crdb.ColumnTypeBool, crdb.Default(false)),
|
||||
crdb.NewColumn(FeatureCustomDomainCol, crdb.ColumnTypeBool, crdb.Default(false)),
|
||||
crdb.NewColumn(FeaturePrivacyPolicyCol, crdb.ColumnTypeBool, crdb.Default(false)),
|
||||
crdb.NewColumn(FeatureMetadataUserCol, crdb.ColumnTypeBool, crdb.Default(false)),
|
||||
crdb.NewColumn(FeatureCustomTextMessageCol, crdb.ColumnTypeBool, crdb.Default(false)),
|
||||
crdb.NewColumn(FeatureCustomTextLoginCol, crdb.ColumnTypeBool, crdb.Default(false)),
|
||||
crdb.NewColumn(FeatureLockoutPolicyCol, crdb.ColumnTypeBool, crdb.Default(false)),
|
||||
crdb.NewColumn(FeatureActionsAllowedCol, crdb.ColumnTypeEnum, crdb.Default(0)),
|
||||
crdb.NewColumn(FeatureMaxActionsCol, crdb.ColumnTypeInt64, crdb.Default(0)),
|
||||
},
|
||||
crdb.NewPrimaryKey(FeatureInstanceIDCol, FeatureAggregateIDCol),
|
||||
),
|
||||
)
|
||||
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
|
||||
return p
|
||||
}
|
||||
@@ -59,35 +118,6 @@ func (p *FeatureProjection) reducers() []handler.AggregateReducer {
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
FeatureAggregateIDCol = "aggregate_id"
|
||||
FeatureChangeDateCol = "change_date"
|
||||
FeatureSequenceCol = "sequence"
|
||||
FeatureIsDefaultCol = "is_default"
|
||||
FeatureTierNameCol = "tier_name"
|
||||
FeatureTierDescriptionCol = "tier_description"
|
||||
FeatureStateCol = "state"
|
||||
FeatureStateDescriptionCol = "state_description"
|
||||
FeatureAuditLogRetentionCol = "audit_log_retention"
|
||||
FeatureLoginPolicyFactorsCol = "login_policy_factors"
|
||||
FeatureLoginPolicyIDPCol = "login_policy_idp"
|
||||
FeatureLoginPolicyPasswordlessCol = "login_policy_passwordless"
|
||||
FeatureLoginPolicyRegistrationCol = "login_policy_registration"
|
||||
FeatureLoginPolicyUsernameLoginCol = "login_policy_username_login"
|
||||
FeatureLoginPolicyPasswordResetCol = "login_policy_password_reset"
|
||||
FeaturePasswordComplexityPolicyCol = "password_complexity_policy"
|
||||
FeatureLabelPolicyPrivateLabelCol = "label_policy_private_label"
|
||||
FeatureLabelPolicyWatermarkCol = "label_policy_watermark"
|
||||
FeatureCustomDomainCol = "custom_domain"
|
||||
FeaturePrivacyPolicyCol = "privacy_policy"
|
||||
FeatureMetadataUserCol = "metadata_user"
|
||||
FeatureCustomTextMessageCol = "custom_text_message"
|
||||
FeatureCustomTextLoginCol = "custom_text_login"
|
||||
FeatureLockoutPolicyCol = "lockout_policy"
|
||||
FeatureActionsAllowedCol = "actions_allowed"
|
||||
FeatureMaxActionsCol = "max_actions"
|
||||
)
|
||||
|
||||
func (p *FeatureProjection) reduceFeatureSet(event eventstore.Event) (*handler.Statement, error) {
|
||||
var featureEvent features.FeaturesSetEvent
|
||||
var isDefault bool
|
||||
@@ -99,12 +129,12 @@ func (p *FeatureProjection) reduceFeatureSet(event eventstore.Event) (*handler.S
|
||||
featureEvent = e.FeaturesSetEvent
|
||||
isDefault = false
|
||||
default:
|
||||
logging.LogWithFields("HANDL-M9ets", "seq", event.Sequence(), "expectedTypes", []eventstore.EventType{org.FeaturesSetEventType, iam.FeaturesSetEventType}).Error("wrong event type")
|
||||
return nil, errors.ThrowInvalidArgument(nil, "HANDL-K0erf", "reduce.wrong.event.type")
|
||||
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-K0erf", "reduce.wrong.event.type %v", []eventstore.EventType{org.FeaturesSetEventType, iam.FeaturesSetEventType})
|
||||
}
|
||||
|
||||
cols := []handler.Column{
|
||||
handler.NewCol(FeatureAggregateIDCol, featureEvent.Aggregate().ID),
|
||||
handler.NewCol(FeatureInstanceIDCol, featureEvent.Aggregate().InstanceID),
|
||||
handler.NewCol(FeatureChangeDateCol, featureEvent.CreationDate()),
|
||||
handler.NewCol(FeatureSequenceCol, featureEvent.Sequence()),
|
||||
handler.NewCol(FeatureIsDefaultCol, isDefault),
|
||||
@@ -196,8 +226,7 @@ func (p *FeatureProjection) reduceFeatureSet(event eventstore.Event) (*handler.S
|
||||
func (p *FeatureProjection) reduceFeatureRemoved(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*org.FeaturesRemovedEvent)
|
||||
if !ok {
|
||||
logging.LogWithFields("HANDL-fN903", "seq", event.Sequence(), "expectedType", org.FeaturesRemovedEventType).Error("wrong event type")
|
||||
return nil, errors.ThrowInvalidArgument(nil, "HANDL-0p4rf", "reduce.wrong.event.type")
|
||||
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-0p4rf", "reduce.wrong.event.type %s", org.FeaturesRemovedEventType)
|
||||
}
|
||||
return crdb.NewDeleteStatement(
|
||||
e,
|
||||
|
||||
Reference in New Issue
Block a user