2025-06-03 17:15:30 +03:00
|
|
|
package setup
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
|
2025-09-08 18:30:03 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/api/scim/metadata"
|
2025-06-03 17:15:30 +03:00
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
|
"github.com/zitadel/zitadel/internal/migration"
|
|
|
|
|
"github.com/zitadel/zitadel/internal/query/projection"
|
2025-09-08 18:30:03 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/serviceping"
|
2025-06-03 17:15:30 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// triggerSteps defines the repeatable migrations that set up triggers
|
|
|
|
|
// for counting resources in the database.
|
|
|
|
|
func triggerSteps(db *database.DB) []migration.RepeatableMigration {
|
|
|
|
|
return []migration.RepeatableMigration{
|
|
|
|
|
// Delete parent count triggers for instances and organizations
|
|
|
|
|
migration.DeleteParentCountsTrigger(db,
|
|
|
|
|
projection.InstanceProjectionTable,
|
|
|
|
|
domain.CountParentTypeInstance,
|
|
|
|
|
projection.InstanceColumnID,
|
|
|
|
|
projection.InstanceColumnID,
|
2025-09-08 18:30:03 +02:00
|
|
|
serviceping.ResourceCountInstance,
|
2025-06-03 17:15:30 +03:00
|
|
|
),
|
|
|
|
|
migration.DeleteParentCountsTrigger(db,
|
|
|
|
|
projection.OrgProjectionTable,
|
|
|
|
|
domain.CountParentTypeOrganization,
|
|
|
|
|
projection.OrgColumnInstanceID,
|
|
|
|
|
projection.OrgColumnID,
|
2025-09-08 18:30:03 +02:00
|
|
|
serviceping.ResourceCountOrganization,
|
2025-06-03 17:15:30 +03:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
// Count triggers for all the resources
|
|
|
|
|
migration.CountTrigger(db,
|
|
|
|
|
projection.OrgProjectionTable,
|
|
|
|
|
domain.CountParentTypeInstance,
|
|
|
|
|
projection.OrgColumnInstanceID,
|
|
|
|
|
projection.OrgColumnInstanceID,
|
2025-09-08 18:30:03 +02:00
|
|
|
serviceping.ResourceCountOrganization,
|
2025-06-03 17:15:30 +03:00
|
|
|
),
|
|
|
|
|
migration.CountTrigger(db,
|
|
|
|
|
projection.ProjectProjectionTable,
|
|
|
|
|
domain.CountParentTypeOrganization,
|
|
|
|
|
projection.ProjectColumnInstanceID,
|
|
|
|
|
projection.ProjectColumnResourceOwner,
|
2025-09-08 18:30:03 +02:00
|
|
|
serviceping.ResourceCountProject,
|
2025-06-03 17:15:30 +03:00
|
|
|
),
|
|
|
|
|
migration.CountTrigger(db,
|
|
|
|
|
projection.UserTable,
|
|
|
|
|
domain.CountParentTypeOrganization,
|
|
|
|
|
projection.UserInstanceIDCol,
|
|
|
|
|
projection.UserResourceOwnerCol,
|
2025-09-08 18:30:03 +02:00
|
|
|
serviceping.ResourceCountUser,
|
|
|
|
|
),
|
|
|
|
|
migration.CountTriggerConditional(db,
|
|
|
|
|
projection.UserTable,
|
|
|
|
|
domain.CountParentTypeOrganization,
|
|
|
|
|
projection.UserInstanceIDCol,
|
|
|
|
|
projection.UserResourceOwnerCol,
|
|
|
|
|
serviceping.ResourceCountUserMachine,
|
|
|
|
|
false, // the user type cannot change, so we do not need to track updates
|
|
|
|
|
&migration.TriggerCondition{
|
|
|
|
|
Column: projection.UserTypeCol,
|
|
|
|
|
// since we marshal the value into and from json,
|
|
|
|
|
// we directly use the float64 value to prevent issues with the comparison of the previous migration
|
|
|
|
|
Value: float64(2),
|
|
|
|
|
},
|
2025-06-03 17:15:30 +03:00
|
|
|
),
|
|
|
|
|
migration.CountTrigger(db,
|
|
|
|
|
projection.InstanceMemberProjectionTable,
|
|
|
|
|
domain.CountParentTypeInstance,
|
|
|
|
|
projection.MemberInstanceID,
|
|
|
|
|
projection.MemberResourceOwner,
|
2025-09-08 18:30:03 +02:00
|
|
|
serviceping.ResourceCountIAMAdmin,
|
2025-06-03 17:15:30 +03:00
|
|
|
),
|
|
|
|
|
migration.CountTrigger(db,
|
|
|
|
|
projection.IDPTable,
|
|
|
|
|
domain.CountParentTypeInstance,
|
|
|
|
|
projection.IDPInstanceIDCol,
|
|
|
|
|
projection.IDPInstanceIDCol,
|
2025-09-08 18:30:03 +02:00
|
|
|
serviceping.ResourceCountIdentityProvider,
|
2025-06-03 17:15:30 +03:00
|
|
|
),
|
|
|
|
|
migration.CountTrigger(db,
|
|
|
|
|
projection.IDPTemplateLDAPTable,
|
|
|
|
|
domain.CountParentTypeInstance,
|
|
|
|
|
projection.LDAPInstanceIDCol,
|
|
|
|
|
projection.LDAPInstanceIDCol,
|
2025-09-08 18:30:03 +02:00
|
|
|
serviceping.ResourceCountIdentityProviderLDAP,
|
2025-06-03 17:15:30 +03:00
|
|
|
),
|
|
|
|
|
migration.CountTrigger(db,
|
|
|
|
|
projection.ActionTable,
|
|
|
|
|
domain.CountParentTypeInstance,
|
|
|
|
|
projection.ActionInstanceIDCol,
|
|
|
|
|
projection.ActionInstanceIDCol,
|
2025-09-08 18:30:03 +02:00
|
|
|
serviceping.ResourceCountActionV1,
|
2025-06-03 17:15:30 +03:00
|
|
|
),
|
|
|
|
|
migration.CountTrigger(db,
|
|
|
|
|
projection.ExecutionTable,
|
|
|
|
|
domain.CountParentTypeInstance,
|
|
|
|
|
projection.ExecutionInstanceIDCol,
|
|
|
|
|
projection.ExecutionInstanceIDCol,
|
2025-09-08 18:30:03 +02:00
|
|
|
serviceping.ResourceCountActionExecution,
|
2025-06-03 17:15:30 +03:00
|
|
|
),
|
|
|
|
|
migration.CountTrigger(db,
|
|
|
|
|
fmt.Sprintf("%s_%s", projection.ExecutionTable, projection.ExecutionTargetSuffix),
|
|
|
|
|
domain.CountParentTypeInstance,
|
|
|
|
|
projection.ExecutionTargetInstanceIDCol,
|
|
|
|
|
projection.ExecutionTargetInstanceIDCol,
|
2025-09-08 18:30:03 +02:00
|
|
|
serviceping.ResourceCountActionExecutionTarget,
|
2025-06-03 17:15:30 +03:00
|
|
|
),
|
|
|
|
|
migration.CountTrigger(db,
|
|
|
|
|
projection.LoginPolicyTable,
|
|
|
|
|
domain.CountParentTypeInstance,
|
|
|
|
|
projection.LoginPolicyInstanceIDCol,
|
|
|
|
|
projection.LoginPolicyInstanceIDCol,
|
2025-09-08 18:30:03 +02:00
|
|
|
serviceping.ResourceCountLoginPolicy,
|
|
|
|
|
),
|
|
|
|
|
migration.CountTriggerConditional(db,
|
|
|
|
|
projection.LoginPolicyTable,
|
|
|
|
|
domain.CountParentTypeInstance,
|
|
|
|
|
projection.LoginPolicyInstanceIDCol,
|
|
|
|
|
projection.LoginPolicyInstanceIDCol,
|
|
|
|
|
serviceping.ResourceCountEnforceMFA,
|
|
|
|
|
true,
|
|
|
|
|
&migration.OrCondition{
|
|
|
|
|
Conditions: []migration.TriggerCondition{
|
|
|
|
|
{Column: projection.LoginPolicyForceMFACol, Value: true},
|
|
|
|
|
{Column: projection.LoginPolicyForceMFALocalOnlyCol, Value: true},
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-06-03 17:15:30 +03:00
|
|
|
),
|
|
|
|
|
migration.CountTrigger(db,
|
|
|
|
|
projection.PasswordComplexityTable,
|
|
|
|
|
domain.CountParentTypeInstance,
|
|
|
|
|
projection.ComplexityPolicyInstanceIDCol,
|
|
|
|
|
projection.ComplexityPolicyInstanceIDCol,
|
2025-09-08 18:30:03 +02:00
|
|
|
serviceping.ResourceCountPasswordComplexityPolicy,
|
2025-06-03 17:15:30 +03:00
|
|
|
),
|
|
|
|
|
migration.CountTrigger(db,
|
|
|
|
|
projection.PasswordAgeTable,
|
|
|
|
|
domain.CountParentTypeInstance,
|
|
|
|
|
projection.AgePolicyInstanceIDCol,
|
|
|
|
|
projection.AgePolicyInstanceIDCol,
|
2025-09-08 18:30:03 +02:00
|
|
|
serviceping.ResourceCountPasswordExpiryPolicy,
|
2025-06-03 17:15:30 +03:00
|
|
|
),
|
|
|
|
|
migration.CountTrigger(db,
|
|
|
|
|
projection.LockoutPolicyTable,
|
|
|
|
|
domain.CountParentTypeInstance,
|
|
|
|
|
projection.LockoutPolicyInstanceIDCol,
|
|
|
|
|
projection.LockoutPolicyInstanceIDCol,
|
2025-09-08 18:30:03 +02:00
|
|
|
serviceping.ResourceCountLockoutPolicy,
|
|
|
|
|
),
|
|
|
|
|
migration.CountTriggerConditional(db,
|
|
|
|
|
projection.NotificationPolicyProjectionTable,
|
|
|
|
|
domain.CountParentTypeInstance,
|
|
|
|
|
projection.NotificationPolicyColumnInstanceID,
|
|
|
|
|
projection.NotificationPolicyColumnInstanceID,
|
|
|
|
|
serviceping.ResourceCountPasswordChangeNotification,
|
|
|
|
|
true,
|
|
|
|
|
&migration.TriggerCondition{
|
|
|
|
|
Column: projection.NotificationPolicyColumnPasswordChange,
|
|
|
|
|
Value: true,
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
migration.CountTriggerConditional(db,
|
|
|
|
|
projection.UserMetadataProjectionTable,
|
|
|
|
|
domain.CountParentTypeOrganization,
|
|
|
|
|
projection.UserMetadataColumnInstanceID,
|
|
|
|
|
projection.LockoutPolicyResourceOwnerCol,
|
|
|
|
|
serviceping.ResourceCountScimProvisionedUser,
|
|
|
|
|
false, // the key cannot change, so we do not need to track updates
|
|
|
|
|
&migration.TriggerCondition{
|
|
|
|
|
Column: projection.UserMetadataColumnKey,
|
|
|
|
|
Value: metadata.KeyEmails,
|
|
|
|
|
},
|
2025-06-03 17:15:30 +03:00
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
}
|