mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-09 15:45:12 +00:00
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.
95 lines
3.7 KiB
Go
95 lines
3.7 KiB
Go
package domain
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
|
)
|
|
|
|
//go:generate enumer -type OrgState -transform lower -trimprefix OrgState -sql
|
|
type OrgState uint8
|
|
|
|
const (
|
|
OrgStateActive OrgState = iota
|
|
OrgStateInactive
|
|
)
|
|
|
|
type Organization struct {
|
|
ID string `json:"id,omitempty" db:"id"`
|
|
Name string `json:"name,omitempty" db:"name"`
|
|
InstanceID string `json:"instanceId,omitempty" db:"instance_id"`
|
|
State OrgState `json:"state,omitempty" db:"state"`
|
|
CreatedAt time.Time `json:"createdAt,omitzero" db:"created_at"`
|
|
UpdatedAt time.Time `json:"updatedAt,omitzero" db:"updated_at"`
|
|
|
|
Domains []*OrganizationDomain `json:"domains,omitempty" db:"-"` // domains need to be handled separately
|
|
}
|
|
|
|
// organizationColumns define all the columns of the instance table.
|
|
type organizationColumns interface {
|
|
// IDColumn returns the column for the id field.
|
|
IDColumn() database.Column
|
|
// NameColumn returns the column for the name field.
|
|
NameColumn() database.Column
|
|
// InstanceIDColumn returns the column for the default org id field
|
|
InstanceIDColumn() database.Column
|
|
// StateColumn returns the column for the name field.
|
|
StateColumn() database.Column
|
|
// CreatedAtColumn returns the column for the created at field.
|
|
CreatedAtColumn() database.Column
|
|
// UpdatedAtColumn returns the column for the updated at field.
|
|
UpdatedAtColumn() database.Column
|
|
}
|
|
|
|
// organizationConditions define all the conditions for the instance table.
|
|
type organizationConditions interface {
|
|
// IDCondition returns an equal filter on the id field.
|
|
IDCondition(instanceID string) database.Condition
|
|
// NameCondition returns a filter on the name field.
|
|
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.
|
|
type organizationChanges interface {
|
|
// SetName sets the name column.
|
|
SetName(name string) database.Change
|
|
// SetState sets the name column.
|
|
SetState(state OrgState) database.Change
|
|
}
|
|
|
|
// OrganizationRepository is the interface for the instance repository.
|
|
type OrganizationRepository interface {
|
|
organizationColumns
|
|
organizationConditions
|
|
organizationChanges
|
|
|
|
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, 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)
|
|
|
|
// 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 {
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
// MemberRepository is a sub repository of the org repository and maybe the instance repository.
|
|
type MemberRepository interface {
|
|
AddMember(ctx context.Context, orgID, userID string, roles []string) error
|
|
SetMemberRoles(ctx context.Context, orgID, userID string, roles []string) error
|
|
RemoveMember(ctx context.Context, orgID, userID string) error
|
|
}
|