Files
zitadel/backend/v3/storage/database/operators_enumer.go
Silvan cccfc816f6 refactor: database interaction and error handling (#10762)
This pull request introduces a significant refactoring of the database
interaction layer, focusing on improving explicitness, transactional
control, and error handling. The core change is the removal of the
stateful `QueryExecutor` from repository instances. Instead, it is now
passed as an argument to each method that interacts with the database.

This change makes transaction management more explicit and flexible, as
the same repository instance can be used with a database pool or a
specific transaction without needing to be re-instantiated.

### Key Changes

- **Explicit `QueryExecutor` Passing:**
- All repository methods (`Get`, `List`, `Create`, `Update`, `Delete`,
etc.) in `InstanceRepository`, `OrganizationRepository`,
`UserRepository`, and their sub-repositories now require a
`database.QueryExecutor` (e.g., a `*pgxpool.Pool` or `pgx.Tx`) as the
first argument.
- Repository constructors no longer accept a `QueryExecutor`. For
example, `repository.InstanceRepository(pool)` is now
`repository.InstanceRepository()`.

- **Enhanced Error Handling:**
- A new `database.MissingConditionError` is introduced to enforce
required query conditions, such as ensuring an `instance_id` is always
present in `UPDATE` and `DELETE` operations.
- The database error wrapper in the `postgres` package now correctly
identifies and wraps `pgx.ErrTooManyRows` and similar errors from the
`scany` library into a `database.MultipleRowsFoundError`.

- **Improved Database Conditions:**
- The `database.Condition` interface now includes a
`ContainsColumn(Column) bool` method. This allows for runtime checks to
ensure that critical filters (like `instance_id`) are included in a
query, preventing accidental cross-tenant data modification.
- A new `database.Exists()` condition has been added to support `EXISTS`
subqueries, enabling more complex filtering logic, such as finding an
organization that has a specific domain.

- **Repository and Interface Refactoring:**
- The method for loading related entities (e.g., domains for an
organization) has been changed from a boolean flag (`Domains(true)`) to
a more explicit, chainable method (`LoadDomains()`). This returns a new
repository instance configured to load the sub-resource, promoting
immutability.
- The custom `OrgIdentifierCondition` has been removed in favor of using
the standard `database.Condition` interface, simplifying the API.

- **Code Cleanup and Test Updates:**
  - Unnecessary struct embeddings and metadata have been removed.
- All integration and repository tests have been updated to reflect the
new method signatures, passing the database pool or transaction object
explicitly.
- New tests have been added to cover the new `ExistsDomain`
functionality and other enhancements.

These changes make the data access layer more robust, predictable, and
easier to work with, especially in the context of database transactions.
2025-09-24 10:12:31 +00:00

242 lines
7.5 KiB
Go

// Code generated by "enumer -type NumberOperation,TextOperation,BytesOperation -linecomment -output ./operators_enumer.go"; DO NOT EDIT.
package database
import (
"fmt"
"strings"
)
const _NumberOperationName = "=<><<=>>="
var _NumberOperationIndex = [...]uint8{0, 1, 3, 4, 6, 7, 9}
const _NumberOperationLowerName = "=<><<=>>="
func (i NumberOperation) String() string {
i -= 1
if i >= NumberOperation(len(_NumberOperationIndex)-1) {
return fmt.Sprintf("NumberOperation(%d)", i+1)
}
return _NumberOperationName[_NumberOperationIndex[i]:_NumberOperationIndex[i+1]]
}
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
func _NumberOperationNoOp() {
var x [1]struct{}
_ = x[NumberOperationEqual-(1)]
_ = x[NumberOperationNotEqual-(2)]
_ = x[NumberOperationLessThan-(3)]
_ = x[NumberOperationAtLeast-(4)]
_ = x[NumberOperationGreaterThan-(5)]
_ = x[NumberOperationAtMost-(6)]
}
var _NumberOperationValues = []NumberOperation{NumberOperationEqual, NumberOperationNotEqual, NumberOperationLessThan, NumberOperationAtLeast, NumberOperationGreaterThan, NumberOperationAtMost}
var _NumberOperationNameToValueMap = map[string]NumberOperation{
_NumberOperationName[0:1]: NumberOperationEqual,
_NumberOperationLowerName[0:1]: NumberOperationEqual,
_NumberOperationName[1:3]: NumberOperationNotEqual,
_NumberOperationLowerName[1:3]: NumberOperationNotEqual,
_NumberOperationName[3:4]: NumberOperationLessThan,
_NumberOperationLowerName[3:4]: NumberOperationLessThan,
_NumberOperationName[4:6]: NumberOperationAtLeast,
_NumberOperationLowerName[4:6]: NumberOperationAtLeast,
_NumberOperationName[6:7]: NumberOperationGreaterThan,
_NumberOperationLowerName[6:7]: NumberOperationGreaterThan,
_NumberOperationName[7:9]: NumberOperationAtMost,
_NumberOperationLowerName[7:9]: NumberOperationAtMost,
}
var _NumberOperationNames = []string{
_NumberOperationName[0:1],
_NumberOperationName[1:3],
_NumberOperationName[3:4],
_NumberOperationName[4:6],
_NumberOperationName[6:7],
_NumberOperationName[7:9],
}
// NumberOperationString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum.
func NumberOperationString(s string) (NumberOperation, error) {
if val, ok := _NumberOperationNameToValueMap[s]; ok {
return val, nil
}
if val, ok := _NumberOperationNameToValueMap[strings.ToLower(s)]; ok {
return val, nil
}
return 0, fmt.Errorf("%s does not belong to NumberOperation values", s)
}
// NumberOperationValues returns all values of the enum
func NumberOperationValues() []NumberOperation {
return _NumberOperationValues
}
// NumberOperationStrings returns a slice of all String values of the enum
func NumberOperationStrings() []string {
strs := make([]string, len(_NumberOperationNames))
copy(strs, _NumberOperationNames)
return strs
}
// IsANumberOperation returns "true" if the value is listed in the enum definition. "false" otherwise
func (i NumberOperation) IsANumberOperation() bool {
for _, v := range _NumberOperationValues {
if i == v {
return true
}
}
return false
}
const _TextOperationName = "=<>LIKE"
var _TextOperationIndex = [...]uint8{0, 1, 3, 7}
const _TextOperationLowerName = "=<>like"
func (i TextOperation) String() string {
i -= 1
if i >= TextOperation(len(_TextOperationIndex)-1) {
return fmt.Sprintf("TextOperation(%d)", i+1)
}
return _TextOperationName[_TextOperationIndex[i]:_TextOperationIndex[i+1]]
}
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
func _TextOperationNoOp() {
var x [1]struct{}
_ = x[TextOperationEqual-(1)]
_ = x[TextOperationNotEqual-(2)]
_ = x[TextOperationStartsWith-(3)]
}
var _TextOperationValues = []TextOperation{TextOperationEqual, TextOperationNotEqual, TextOperationStartsWith}
var _TextOperationNameToValueMap = map[string]TextOperation{
_TextOperationName[0:1]: TextOperationEqual,
_TextOperationLowerName[0:1]: TextOperationEqual,
_TextOperationName[1:3]: TextOperationNotEqual,
_TextOperationLowerName[1:3]: TextOperationNotEqual,
_TextOperationName[3:7]: TextOperationStartsWith,
_TextOperationLowerName[3:7]: TextOperationStartsWith,
}
var _TextOperationNames = []string{
_TextOperationName[0:1],
_TextOperationName[1:3],
_TextOperationName[3:7],
}
// TextOperationString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum.
func TextOperationString(s string) (TextOperation, error) {
if val, ok := _TextOperationNameToValueMap[s]; ok {
return val, nil
}
if val, ok := _TextOperationNameToValueMap[strings.ToLower(s)]; ok {
return val, nil
}
return 0, fmt.Errorf("%s does not belong to TextOperation values", s)
}
// TextOperationValues returns all values of the enum
func TextOperationValues() []TextOperation {
return _TextOperationValues
}
// TextOperationStrings returns a slice of all String values of the enum
func TextOperationStrings() []string {
strs := make([]string, len(_TextOperationNames))
copy(strs, _TextOperationNames)
return strs
}
// IsATextOperation returns "true" if the value is listed in the enum definition. "false" otherwise
func (i TextOperation) IsATextOperation() bool {
for _, v := range _TextOperationValues {
if i == v {
return true
}
}
return false
}
const _BytesOperationName = "=<>"
var _BytesOperationIndex = [...]uint8{0, 1, 3}
const _BytesOperationLowerName = "=<>"
func (i BytesOperation) String() string {
i -= 1
if i >= BytesOperation(len(_BytesOperationIndex)-1) {
return fmt.Sprintf("BytesOperation(%d)", i+1)
}
return _BytesOperationName[_BytesOperationIndex[i]:_BytesOperationIndex[i+1]]
}
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
func _BytesOperationNoOp() {
var x [1]struct{}
_ = x[BytesOperationEqual-(1)]
_ = x[BytesOperationNotEqual-(2)]
}
var _BytesOperationValues = []BytesOperation{BytesOperationEqual, BytesOperationNotEqual}
var _BytesOperationNameToValueMap = map[string]BytesOperation{
_BytesOperationName[0:1]: BytesOperationEqual,
_BytesOperationLowerName[0:1]: BytesOperationEqual,
_BytesOperationName[1:3]: BytesOperationNotEqual,
_BytesOperationLowerName[1:3]: BytesOperationNotEqual,
}
var _BytesOperationNames = []string{
_BytesOperationName[0:1],
_BytesOperationName[1:3],
}
// BytesOperationString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum.
func BytesOperationString(s string) (BytesOperation, error) {
if val, ok := _BytesOperationNameToValueMap[s]; ok {
return val, nil
}
if val, ok := _BytesOperationNameToValueMap[strings.ToLower(s)]; ok {
return val, nil
}
return 0, fmt.Errorf("%s does not belong to BytesOperation values", s)
}
// BytesOperationValues returns all values of the enum
func BytesOperationValues() []BytesOperation {
return _BytesOperationValues
}
// BytesOperationStrings returns a slice of all String values of the enum
func BytesOperationStrings() []string {
strs := make([]string, len(_BytesOperationNames))
copy(strs, _BytesOperationNames)
return strs
}
// IsABytesOperation returns "true" if the value is listed in the enum definition. "false" otherwise
func (i BytesOperation) IsABytesOperation() bool {
for _, v := range _BytesOperationValues {
if i == v {
return true
}
}
return false
}