diff --git a/backend/v3/domain/organization.go b/backend/v3/domain/organization.go index 3d99aaacb2..7baa8debcb 100644 --- a/backend/v3/domain/organization.go +++ b/backend/v3/domain/organization.go @@ -61,7 +61,7 @@ type OrganizationRepository interface { organizationChanges Get(ctx context.Context, opts ...database.Condition) (*Organization, error) - List(ctx context.Context, opts ...database.Condition) ([]Organization, error) + List(ctx context.Context, opts ...database.Condition) ([]*Organization, error) Create(ctx context.Context, instance *Organization) error Update(ctx context.Context, condition database.Condition, changes ...database.Change) (int64, error) diff --git a/backend/v3/storage/database/events_testing/instance_test.go b/backend/v3/storage/database/events_testing/instance_test.go index 9d7a293a22..f675dc5e90 100644 --- a/backend/v3/storage/database/events_testing/instance_test.go +++ b/backend/v3/storage/database/events_testing/instance_test.go @@ -112,12 +112,12 @@ func TestServer_TestInstanceDeleteReduces(t *testing.T) { instanceRepo := repository.InstanceRepository(pool) retryDuration, tick := integration.WaitForAndTickWithMaxDuration(CTX, time.Minute) - assert.EventuallyWithT(t, func(ttt *assert.CollectT) { + assert.EventuallyWithT(t, func(t *assert.CollectT) { instance, err := instanceRepo.Get(CTX, instanceRepo.NameCondition(database.TextOperationEqual, instanceName), ) // event instance.removed require.Nil(t, instance) - require.NoError(ttt, err) + require.NoError(t, err) }, retryDuration, tick) } diff --git a/backend/v3/storage/database/repository/instance.go b/backend/v3/storage/database/repository/instance.go index 150fb1fcc3..44ff0e7112 100644 --- a/backend/v3/storage/database/repository/instance.go +++ b/backend/v3/storage/database/repository/instance.go @@ -213,9 +213,18 @@ func scanInstance(ctx context.Context, querier database.Querier, builder *databa if err != nil { return nil, err } + if err != nil { + return nil, nil + } instance := new(domain.Instance) if err := rows.(database.CollectableRows).CollectExactlyOneRow(instance); 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, nil + } return nil, err } @@ -229,6 +238,12 @@ func scanInstances(ctx context.Context, querier database.Querier, builder *datab } if err := rows.(database.CollectableRows).Collect(&instances); 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, nil + } return nil, err } diff --git a/backend/v3/storage/database/repository/org.go b/backend/v3/storage/database/repository/org.go index 2da2bf2f29..c7ea29e085 100644 --- a/backend/v3/storage/database/repository/org.go +++ b/backend/v3/storage/database/repository/org.go @@ -5,7 +5,6 @@ import ( "errors" "time" - "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgconn" "github.com/zitadel/zitadel/backend/v3/domain" "github.com/zitadel/zitadel/backend/v3/storage/database" @@ -43,17 +42,17 @@ func (o *org) Get(ctx context.Context, opts ...database.Condition) (*domain.Orga andCondition := database.And(opts...) o.writeCondition(&builder, andCondition) - rows, err := o.client.Query(ctx, builder.String(), builder.Args()...) - if err != nil { - return nil, err - } - defer rows.Close() + // rows, err := o.client.Query(ctx, builder.String(), builder.Args()...) + // if err != nil { + // return nil, err + // } + // defer rows.Close() - return scanOrganization(rows) + return scanOrganization(ctx, o.client, &builder) } // List implements [domain.OrganizationRepository]. -func (o *org) List(ctx context.Context, opts ...database.Condition) ([]domain.Organization, error) { +func (o *org) List(ctx context.Context, opts ...database.Condition) ([]*domain.Organization, error) { builder := database.StatementBuilder{} builder.WriteString(queryOrganizationStmt) @@ -63,13 +62,13 @@ func (o *org) List(ctx context.Context, opts ...database.Condition) ([]domain.Or andCondition := database.And(opts...) o.writeCondition(&builder, andCondition) - rows, err := o.client.Query(ctx, builder.String(), builder.Args()...) - if err != nil { - return nil, err - } - defer rows.Close() + // rows, err := o.client.Query(ctx, builder.String(), builder.Args()...) + // if err != nil { + // return nil, err + // } + // defer rows.Close() - return scanOrganizations(rows) + return scanOrganizations(ctx, o.client, &builder) } const createOrganizationStmt = `INSERT INTO zitadel.organizations (id, name, instance_id, state)` + @@ -206,9 +205,17 @@ func (o *org) writeCondition( condition.Write(builder) } -func scanOrganization(rows database.Rows) (*domain.Organization, error) { - organization, err := pgx.CollectOneRow[domain.Organization](rows, pgx.RowToStructByNameLax[domain.Organization]) +func scanOrganization(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 + } + if err != nil { + return nil, err + } + + organization := &domain.Organization{} + if err := rows.(database.CollectableRows).CollectExactlyOneRow(organization); 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 @@ -218,12 +225,17 @@ func scanOrganization(rows database.Rows) (*domain.Organization, error) { return nil, err } - return &organization, nil + return organization, nil } -func scanOrganizations(rows database.Rows) ([]domain.Organization, error) { - organizations, err := pgx.CollectRows[domain.Organization](rows, pgx.RowToStructByNameLax[domain.Organization]) +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 { // 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