mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-21 12:28:46 +00:00
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.
This commit is contained in:
@@ -69,6 +69,8 @@ type instanceConditions interface {
|
||||
IDCondition(instanceID string) database.Condition
|
||||
// NameCondition returns a filter on the name field.
|
||||
NameCondition(op database.TextOperation, name string) database.Condition
|
||||
// ExistsDomain returns a filter on the instance domains.
|
||||
ExistsDomain(cond database.Condition) database.Condition
|
||||
}
|
||||
|
||||
// instanceChanges define all the changes for the instance table.
|
||||
@@ -99,17 +101,16 @@ type InstanceRepository interface {
|
||||
// Member returns the member repository which is a sub repository of the instance repository.
|
||||
// Member() MemberRepository
|
||||
|
||||
Get(ctx context.Context, opts ...database.QueryOption) (*Instance, error)
|
||||
List(ctx context.Context, opts ...database.QueryOption) ([]*Instance, error)
|
||||
Get(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*Instance, error)
|
||||
List(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) ([]*Instance, error)
|
||||
|
||||
Create(ctx context.Context, instance *Instance) error
|
||||
Update(ctx context.Context, id string, changes ...database.Change) (int64, error)
|
||||
Delete(ctx context.Context, id string) (int64, error)
|
||||
Create(ctx context.Context, client database.QueryExecutor, instance *Instance) error
|
||||
Update(ctx context.Context, client database.QueryExecutor, id string, changes ...database.Change) (int64, error)
|
||||
Delete(ctx context.Context, client database.QueryExecutor, id string) (int64, error)
|
||||
|
||||
// Domains returns the domain sub repository for the instance.
|
||||
// If shouldLoad is true, the domains will be loaded from the database and written to the [Instance].Domains field.
|
||||
// If shouldLoad is set to true once, the Domains field will be set even if shouldLoad is false in the future.
|
||||
Domains(shouldLoad bool) InstanceDomainRepository
|
||||
// LoadDomains loads the domains of the given instance.
|
||||
// If it is called the [Instance].Domains field will be set on future calls to Get or List.
|
||||
LoadDomains() InstanceRepository
|
||||
}
|
||||
|
||||
type CreateInstance struct {
|
||||
|
||||
@@ -65,15 +65,15 @@ type InstanceDomainRepository interface {
|
||||
// Get returns a single domain based on the criteria.
|
||||
// If no domain is found, it returns an error of type [database.ErrNotFound].
|
||||
// If multiple domains are found, it returns an error of type [database.ErrMultipleRows].
|
||||
Get(ctx context.Context, opts ...database.QueryOption) (*InstanceDomain, error)
|
||||
Get(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*InstanceDomain, error)
|
||||
// List returns a list of domains based on the criteria.
|
||||
// If no domains are found, it returns an empty slice.
|
||||
List(ctx context.Context, opts ...database.QueryOption) ([]*InstanceDomain, error)
|
||||
List(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) ([]*InstanceDomain, error)
|
||||
|
||||
// Add adds a new domain to the instance.
|
||||
Add(ctx context.Context, domain *AddInstanceDomain) error
|
||||
Add(ctx context.Context, client database.QueryExecutor, domain *AddInstanceDomain) error
|
||||
// Update updates an existing domain in the instance.
|
||||
Update(ctx context.Context, condition database.Condition, changes ...database.Change) (int64, error)
|
||||
Update(ctx context.Context, client database.QueryExecutor, condition database.Condition, changes ...database.Change) (int64, error)
|
||||
// Remove removes a domain from the instance.
|
||||
Remove(ctx context.Context, condition database.Condition) (int64, error)
|
||||
Remove(ctx context.Context, client database.QueryExecutor, condition database.Condition) (int64, error)
|
||||
}
|
||||
|
||||
@@ -26,13 +26,6 @@ type Organization struct {
|
||||
Domains []*OrganizationDomain `json:"domains,omitempty" db:"-"` // domains need to be handled separately
|
||||
}
|
||||
|
||||
// OrgIdentifierCondition is used to help specify a single Organization,
|
||||
// it will either be used as the organization ID or organization name,
|
||||
// as organizations can be identified either using (instanceID + ID) OR (instanceID + name)
|
||||
type OrgIdentifierCondition interface {
|
||||
database.Condition
|
||||
}
|
||||
|
||||
// organizationColumns define all the columns of the instance table.
|
||||
type organizationColumns interface {
|
||||
// IDColumn returns the column for the id field.
|
||||
@@ -52,13 +45,15 @@ type organizationColumns interface {
|
||||
// organizationConditions define all the conditions for the instance table.
|
||||
type organizationConditions interface {
|
||||
// IDCondition returns an equal filter on the id field.
|
||||
IDCondition(instanceID string) OrgIdentifierCondition
|
||||
IDCondition(instanceID string) database.Condition
|
||||
// NameCondition returns a filter on the name field.
|
||||
NameCondition(name string) OrgIdentifierCondition
|
||||
NameCondition(op database.TextOperation, name string) database.Condition
|
||||
// InstanceIDCondition returns a filter on the instance id field.
|
||||
InstanceIDCondition(instanceID string) database.Condition
|
||||
// StateCondition returns a filter on the name field.
|
||||
StateCondition(state OrgState) database.Condition
|
||||
// ExistsDomain returns a filter on the organizations domains.
|
||||
ExistsDomain(cond database.Condition) database.Condition
|
||||
}
|
||||
|
||||
// organizationChanges define all the changes for the instance table.
|
||||
@@ -75,17 +70,16 @@ type OrganizationRepository interface {
|
||||
organizationConditions
|
||||
organizationChanges
|
||||
|
||||
Get(ctx context.Context, opts ...database.QueryOption) (*Organization, error)
|
||||
List(ctx context.Context, opts ...database.QueryOption) ([]*Organization, error)
|
||||
Get(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*Organization, error)
|
||||
List(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) ([]*Organization, error)
|
||||
|
||||
Create(ctx context.Context, instance *Organization) error
|
||||
Update(ctx context.Context, id OrgIdentifierCondition, instance_id string, changes ...database.Change) (int64, error)
|
||||
Delete(ctx context.Context, id OrgIdentifierCondition, instance_id string) (int64, error)
|
||||
Create(ctx context.Context, client database.QueryExecutor, org *Organization) error
|
||||
Update(ctx context.Context, client database.QueryExecutor, condition database.Condition, changes ...database.Change) (int64, error)
|
||||
Delete(ctx context.Context, client database.QueryExecutor, condition database.Condition) (int64, error)
|
||||
|
||||
// Domains returns the domain sub repository for the organization.
|
||||
// If shouldLoad is true, the domains will be loaded from the database and written to the [Instance].Domains field.
|
||||
// If shouldLoad is set to true once, the Domains field will be set event if shouldLoad is false in the future.
|
||||
Domains(shouldLoad bool) OrganizationDomainRepository
|
||||
// LoadDomains loads the domains of the given organizations.
|
||||
// If it is called the [Organization].Domains field will be set on future calls to Get or List.
|
||||
LoadDomains() OrganizationRepository
|
||||
}
|
||||
|
||||
type CreateOrganization struct {
|
||||
|
||||
@@ -70,15 +70,15 @@ type OrganizationDomainRepository interface {
|
||||
// Get returns a single domain based on the criteria.
|
||||
// If no domain is found, it returns an error of type [database.ErrNotFound].
|
||||
// If multiple domains are found, it returns an error of type [database.ErrMultipleRows].
|
||||
Get(ctx context.Context, opts ...database.QueryOption) (*OrganizationDomain, error)
|
||||
Get(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*OrganizationDomain, error)
|
||||
// List returns a list of domains based on the criteria.
|
||||
// If no domains are found, it returns an empty slice.
|
||||
List(ctx context.Context, opts ...database.QueryOption) ([]*OrganizationDomain, error)
|
||||
List(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) ([]*OrganizationDomain, error)
|
||||
|
||||
// Add adds a new domain to the organization.
|
||||
Add(ctx context.Context, domain *AddOrganizationDomain) error
|
||||
Add(ctx context.Context, client database.QueryExecutor, domain *AddOrganizationDomain) error
|
||||
// Update updates an existing domain in the organization.
|
||||
Update(ctx context.Context, condition database.Condition, changes ...database.Change) (int64, error)
|
||||
Update(ctx context.Context, client database.QueryExecutor, condition database.Condition, changes ...database.Change) (int64, error)
|
||||
// Remove removes a domain from the organization.
|
||||
Remove(ctx context.Context, condition database.Condition) (int64, error)
|
||||
Remove(ctx context.Context, client database.QueryExecutor, condition database.Condition) (int64, error)
|
||||
}
|
||||
|
||||
@@ -57,13 +57,13 @@ type UserRepository interface {
|
||||
userConditions
|
||||
userChanges
|
||||
// Get returns a user based on the given condition.
|
||||
Get(ctx context.Context, opts ...database.QueryOption) (*User, error)
|
||||
Get(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*User, error)
|
||||
// List returns a list of users based on the given condition.
|
||||
List(ctx context.Context, opts ...database.QueryOption) ([]*User, error)
|
||||
List(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) ([]*User, error)
|
||||
// Create creates a new user.
|
||||
Create(ctx context.Context, user *User) error
|
||||
Create(ctx context.Context, client database.QueryExecutor, user *User) error
|
||||
// Delete removes users based on the given condition.
|
||||
Delete(ctx context.Context, condition database.Condition) error
|
||||
Delete(ctx context.Context, client database.QueryExecutor, condition database.Condition) error
|
||||
// Human returns the [HumanRepository].
|
||||
Human() HumanRepository
|
||||
// Machine returns the [MachineRepository].
|
||||
@@ -143,9 +143,9 @@ type HumanRepository interface {
|
||||
humanChanges
|
||||
|
||||
// Get returns an email based on the given condition.
|
||||
GetEmail(ctx context.Context, condition database.Condition) (*Email, error)
|
||||
GetEmail(ctx context.Context, client database.QueryExecutor, condition database.Condition) (*Email, error)
|
||||
// Update updates human users based on the given condition and changes.
|
||||
Update(ctx context.Context, condition database.Condition, changes ...database.Change) error
|
||||
Update(ctx context.Context, client database.QueryExecutor, condition database.Condition, changes ...database.Change) error
|
||||
}
|
||||
|
||||
// machineColumns define all the columns of the machine table which inherits the user table.
|
||||
@@ -172,7 +172,7 @@ type machineChanges interface {
|
||||
// MachineRepository is the interface for the machine repository it inherits the user repository.
|
||||
type MachineRepository interface {
|
||||
// Update updates machine users based on the given condition and changes.
|
||||
Update(ctx context.Context, condition database.Condition, changes ...database.Change) error
|
||||
Update(ctx context.Context, client database.QueryExecutor, condition database.Condition, changes ...database.Change) error
|
||||
|
||||
machineColumns
|
||||
machineConditions
|
||||
|
||||
Reference in New Issue
Block a user