mirror of
https://github.com/andwati/zitadel.git
synced 2026-10-19 22:14:25 +00:00
# Which Problems Are Solved Add projects to the relational tables # How the Problems Are Solved - Define table migrations - Define and implement Project and Project Role repositories. - Provide projection handlers to populate the relational tables. # Additional Changes - Statement Builder now has a constructor which allows setting of a base query with arguments. - Certain operations, like Get, Update and Delete require the Primary Key to be set as conditions. However, this requires knowledge of the implementation and table definition. This PR proposes an additional condition for repositories: `PrimaryKeyCondition`. This gives clarity on the required IDs for these operations. - Added couple of helpers to the repository package, to allow more DRY code. - getOne / getMany: generic functions for query execution and scanning. - checkRestrictingColumns, checkPkCondition: simplify condition checking, instead of using ladders of conditionals. - Added a couple of helpers to the repository test package: - Transaction, savepoint and rollback helpers. - Create instance and organization helpers for objects that depend on them (like projects). # Additional Context - after https://github.com/zitadel/zitadel/pull/10809 - closes #10765
71 lines
1.7 KiB
Go
71 lines
1.7 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"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
|
|
}
|
|
|
|
type pkRepository interface {
|
|
PrimaryKeyColumns() []database.Column
|
|
}
|
|
|
|
// 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 pkRepository,
|
|
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
|
|
}
|