diff --git a/backend/v3/domain/instance.go b/backend/v3/domain/instance.go index 10a3620788..9b95d111c0 100644 --- a/backend/v3/domain/instance.go +++ b/backend/v3/domain/instance.go @@ -84,7 +84,7 @@ type InstanceRepository interface { // Member() MemberRepository Get(ctx context.Context, id string) (*Instance, error) - List(ctx context.Context, opts ...database.Condition) ([]*Instance, error) + List(ctx context.Context, conditions ...database.Condition) ([]*Instance, error) Create(ctx context.Context, instance *Instance) error Update(ctx context.Context, id string, changes ...database.Change) (int64, error) diff --git a/backend/v3/domain/organization.go b/backend/v3/domain/organization.go index 870cc77838..5f4ab169d3 100644 --- a/backend/v3/domain/organization.go +++ b/backend/v3/domain/organization.go @@ -37,7 +37,7 @@ type organizationColumns interface { 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 returns the column for the instance id field InstanceIDColumn() database.Column // StateColumn returns the column for the name field. StateColumn() database.Column diff --git a/backend/v3/storage/database/dialect/postgres/migration/003_id_providers_table/up.sql b/backend/v3/storage/database/dialect/postgres/migration/003_id_providers_table/up.sql index d4c0f46b18..ee872b72fa 100644 --- a/backend/v3/storage/database/dialect/postgres/migration/003_id_providers_table/up.sql +++ b/backend/v3/storage/database/dialect/postgres/migration/003_id_providers_table/up.sql @@ -30,7 +30,6 @@ CREATE TABLE identity_providers ( , created_at TIMESTAMPTZ NOT NULL DEFAULT now() , updated_at TIMESTAMPTZ NOT NULL DEFAULT now() - , deleted_at TIMESTAMPTZ , PRIMARY KEY (instance_id, id) , CONSTRAINT identity_providers_unique UNIQUE NULLS NOT DISTINCT (instance_id, org_id, id) diff --git a/backend/v3/storage/database/repository/instance.go b/backend/v3/storage/database/repository/instance.go index 63f878574c..0df054af63 100644 --- a/backend/v3/storage/database/repository/instance.go +++ b/backend/v3/storage/database/repository/instance.go @@ -172,28 +172,19 @@ func (instance) UpdatedAtColumn() database.Column { } func scanInstance(ctx context.Context, querier database.Querier, builder *database.StatementBuilder) (*domain.Instance, error) { - rows, err := querier.Query(ctx, builder.String(), builder.Args()...) + instance := &domain.Instance{} + err := scan(ctx, querier, builder, instance) if err != nil { return nil, err } - - instance := new(domain.Instance) - if err := rows.(database.CollectableRows).CollectExactlyOneRow(instance); err != nil { - return nil, err - } - return instance, nil } -func scanInstances(ctx context.Context, querier database.Querier, builder *database.StatementBuilder) (instances []*domain.Instance, err error) { - rows, err := querier.Query(ctx, builder.String(), builder.Args()...) +func scanInstances(ctx context.Context, querier database.Querier, builder *database.StatementBuilder) ([]*domain.Instance, error) { + instances := []*domain.Instance{} + err := scanMultiple(ctx, querier, builder, &instances) if err != nil { return nil, err } - - if err := rows.(database.CollectableRows).Collect(&instances); err != nil { - return nil, err - } - return instances, nil } diff --git a/backend/v3/storage/database/repository/org.go b/backend/v3/storage/database/repository/org.go index e8053aadd9..050450932e 100644 --- a/backend/v3/storage/database/repository/org.go +++ b/backend/v3/storage/database/repository/org.go @@ -217,27 +217,18 @@ func (org) UpdatedAtColumn() database.Column { } func scanOrganization(ctx context.Context, querier database.Querier, builder *database.StatementBuilder) (*domain.Organization, error) { - rows, err := querier.Query(ctx, builder.String(), builder.Args()...) + organization := &domain.Organization{} + err := scan(ctx, querier, builder, organization) if err != nil { return nil, err } - - organization := &domain.Organization{} - if err := rows.(database.CollectableRows).CollectExactlyOneRow(organization); err != nil { - return nil, err - } - return organization, nil } func scanOrganizations(ctx context.Context, querier database.Querier, builder *database.StatementBuilder) ([]*domain.Organization, error) { - rows, err := querier.Query(ctx, builder.String(), builder.Args()...) - if err != nil { - return nil, err - } - organizations := []*domain.Organization{} - if err := rows.(database.CollectableRows).Collect(&organizations); err != nil { + err := scanMultiple(ctx, querier, builder, &organizations) + if err != nil { return nil, err } return organizations, nil diff --git a/backend/v3/storage/database/repository/repository.go b/backend/v3/storage/database/repository/repository.go index c5b9ff81f0..4a8343a0cf 100644 --- a/backend/v3/storage/database/repository/repository.go +++ b/backend/v3/storage/database/repository/repository.go @@ -1,6 +1,9 @@ package repository import ( + "context" + "errors" + "github.com/zitadel/zitadel/backend/v3/storage/database" ) @@ -18,3 +21,36 @@ func writeCondition( builder.WriteString(" WHERE ") condition.Write(builder) } + +func scan(ctx context.Context, querier database.Querier, builder *database.StatementBuilder, res any) error { + rows, err := querier.Query(ctx, builder.String(), builder.Args()...) + if err != nil { + return err + } + + if err := rows.(database.CollectableRows).CollectExactlyOneRow(res); err != nil { + if err.Error() == "no rows in result set" { + return ErrResourceDoesNotExist + } + return err + } + return nil +} + +func scanMultiple(ctx context.Context, querier database.Querier, builder *database.StatementBuilder, res any) error { + rows, err := querier.Query(ctx, builder.String(), builder.Args()...) + if err != nil { + return err + } + + if err := rows.(database.CollectableRows).Collect(res); err != nil { + // if no results returned, this is not a error + // it just means the organization was not found + // the caller should check if the returned organization is nil + if err.Error() == "no rows in result set" { + return nil + } + return err + } + return nil +}