mirror of
https://github.com/andwati/zitadel.git
synced 2026-10-22 23:42:16 +00:00
# Which Problems Are Solved Relational table's primary keys are part of the SQL table definition. However, by abstraction through the repository interfaces callers (service layer) have no clear understanding of the primary key definion. This might lead to confusion during implementation: - The repository makes a sanity check on required conditions before query execution and returns an error if the condition was not met. - When a Get method is executed, an error is returned if not exactly one row is found. Get replaces the legacy GetXxxByID methods we have on projections. However, a list of flexible conditions can be passed. - Operations like UPDATE and DELETE should often only affect a single row, identified by the primary key. If the primary key conditions are missing an error is returned. Because there is no "type safety" enforced here, the errors are returned during runtime. When all possible combinations of API arguments are not properly tested such errors are actually bugs in the experience of the end-user. (The API should do it's own input validation, but a programmer's error is easily made). # How the Problems Are Solved Add `PrimaryKeyColumns` and `PrimaryKeyCondition` to existing repository intefaces. # Additional Changes - Where reducers already used repositories, the PrimaryKeyConditions are used where possible. # Additional Context - Part of https://github.com/zitadel/zitadel/wiki/Decision-Log#expose-primary-key-definition-in-repository-interfaces --------- Co-authored-by: Marco A. <[email protected]>
68 lines
1.7 KiB
Go
68 lines
1.7 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/zitadel/zitadel/backend/v3/domain"
|
|
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
|
)
|
|
|
|
func writeCondition(
|
|
builder *database.StatementBuilder,
|
|
condition database.Condition,
|
|
) {
|
|
if condition == nil {
|
|
return
|
|
}
|
|
builder.WriteString(" WHERE ")
|
|
condition.Write(builder)
|
|
}
|
|
|
|
func checkRestrictingColumns(
|
|
condition database.Condition,
|
|
requiredColumns ...database.Column,
|
|
) error {
|
|
for _, col := range requiredColumns {
|
|
if !condition.IsRestrictingColumn(col) {
|
|
return database.NewMissingConditionError(col)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// checkPKCondition checks if the Primary Key columns are part of the condition.
|
|
// This can ensure only a single row is affected by updates and deletes.
|
|
func checkPKCondition(
|
|
repo domain.Repository,
|
|
condition database.Condition,
|
|
) error {
|
|
return checkRestrictingColumns(
|
|
condition,
|
|
repo.PrimaryKeyColumns()...,
|
|
)
|
|
}
|
|
|
|
func getOne[Target any](ctx context.Context, querier database.Querier, builder *database.StatementBuilder) (*Target, error) {
|
|
rows, err := querier.Query(ctx, builder.String(), builder.Args()...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var target Target
|
|
if err := rows.(database.CollectableRows).CollectExactlyOneRow(&target); err != nil {
|
|
return nil, err
|
|
}
|
|
return &target, nil
|
|
}
|
|
|
|
func getMany[Target any](ctx context.Context, querier database.Querier, builder *database.StatementBuilder) ([]*Target, error) {
|
|
rows, err := querier.Query(ctx, builder.String(), builder.Args()...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var targets []*Target
|
|
if err := rows.(database.CollectableRows).Collect(&targets); err != nil {
|
|
return nil, err
|
|
}
|
|
return targets, nil
|
|
}
|