mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-13 17:17:59 +00:00
fixup! fixup! fixup! feat(db): adding org table to relational model
This commit is contained in:
@@ -61,7 +61,7 @@ type OrganizationRepository interface {
|
|||||||
organizationChanges
|
organizationChanges
|
||||||
|
|
||||||
Get(ctx context.Context, opts ...database.Condition) (*Organization, error)
|
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
|
Create(ctx context.Context, instance *Organization) error
|
||||||
Update(ctx context.Context, condition database.Condition, changes ...database.Change) (int64, error)
|
Update(ctx context.Context, condition database.Condition, changes ...database.Change) (int64, error)
|
||||||
|
@@ -112,12 +112,12 @@ func TestServer_TestInstanceDeleteReduces(t *testing.T) {
|
|||||||
|
|
||||||
instanceRepo := repository.InstanceRepository(pool)
|
instanceRepo := repository.InstanceRepository(pool)
|
||||||
retryDuration, tick := integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
|
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,
|
instance, err := instanceRepo.Get(CTX,
|
||||||
instanceRepo.NameCondition(database.TextOperationEqual, instanceName),
|
instanceRepo.NameCondition(database.TextOperationEqual, instanceName),
|
||||||
)
|
)
|
||||||
// event instance.removed
|
// event instance.removed
|
||||||
require.Nil(t, instance)
|
require.Nil(t, instance)
|
||||||
require.NoError(ttt, err)
|
require.NoError(t, err)
|
||||||
}, retryDuration, tick)
|
}, retryDuration, tick)
|
||||||
}
|
}
|
||||||
|
@@ -213,9 +213,18 @@ func scanInstance(ctx context.Context, querier database.Querier, builder *databa
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
instance := new(domain.Instance)
|
instance := new(domain.Instance)
|
||||||
if err := rows.(database.CollectableRows).CollectExactlyOneRow(instance); err != nil {
|
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
|
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 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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -5,7 +5,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5"
|
|
||||||
"github.com/jackc/pgx/v5/pgconn"
|
"github.com/jackc/pgx/v5/pgconn"
|
||||||
"github.com/zitadel/zitadel/backend/v3/domain"
|
"github.com/zitadel/zitadel/backend/v3/domain"
|
||||||
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
"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...)
|
andCondition := database.And(opts...)
|
||||||
o.writeCondition(&builder, andCondition)
|
o.writeCondition(&builder, andCondition)
|
||||||
|
|
||||||
rows, err := o.client.Query(ctx, builder.String(), builder.Args()...)
|
// rows, err := o.client.Query(ctx, builder.String(), builder.Args()...)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return nil, err
|
// return nil, err
|
||||||
}
|
// }
|
||||||
defer rows.Close()
|
// defer rows.Close()
|
||||||
|
|
||||||
return scanOrganization(rows)
|
return scanOrganization(ctx, o.client, &builder)
|
||||||
}
|
}
|
||||||
|
|
||||||
// List implements [domain.OrganizationRepository].
|
// 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 := database.StatementBuilder{}
|
||||||
|
|
||||||
builder.WriteString(queryOrganizationStmt)
|
builder.WriteString(queryOrganizationStmt)
|
||||||
@@ -63,13 +62,13 @@ func (o *org) List(ctx context.Context, opts ...database.Condition) ([]domain.Or
|
|||||||
andCondition := database.And(opts...)
|
andCondition := database.And(opts...)
|
||||||
o.writeCondition(&builder, andCondition)
|
o.writeCondition(&builder, andCondition)
|
||||||
|
|
||||||
rows, err := o.client.Query(ctx, builder.String(), builder.Args()...)
|
// rows, err := o.client.Query(ctx, builder.String(), builder.Args()...)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return nil, err
|
// return nil, err
|
||||||
}
|
// }
|
||||||
defer rows.Close()
|
// defer rows.Close()
|
||||||
|
|
||||||
return scanOrganizations(rows)
|
return scanOrganizations(ctx, o.client, &builder)
|
||||||
}
|
}
|
||||||
|
|
||||||
const createOrganizationStmt = `INSERT INTO zitadel.organizations (id, name, instance_id, state)` +
|
const createOrganizationStmt = `INSERT INTO zitadel.organizations (id, name, instance_id, state)` +
|
||||||
@@ -206,9 +205,17 @@ func (o *org) writeCondition(
|
|||||||
condition.Write(builder)
|
condition.Write(builder)
|
||||||
}
|
}
|
||||||
|
|
||||||
func scanOrganization(rows database.Rows) (*domain.Organization, error) {
|
func scanOrganization(ctx context.Context, querier database.Querier, builder *database.StatementBuilder) (*domain.Organization, error) {
|
||||||
organization, err := pgx.CollectOneRow[domain.Organization](rows, pgx.RowToStructByNameLax[domain.Organization])
|
rows, err := querier.Query(ctx, builder.String(), builder.Args()...)
|
||||||
if err != nil {
|
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
|
// if no results returned, this is not a error
|
||||||
// it just means the organization was not found
|
// it just means the organization was not found
|
||||||
// the caller should check if the returned organization is nil
|
// 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 nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &organization, nil
|
return organization, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func scanOrganizations(rows database.Rows) ([]domain.Organization, error) {
|
func scanOrganizations(ctx context.Context, querier database.Querier, builder *database.StatementBuilder) ([]*domain.Organization, error) {
|
||||||
organizations, err := pgx.CollectRows[domain.Organization](rows, pgx.RowToStructByNameLax[domain.Organization])
|
rows, err := querier.Query(ctx, builder.String(), builder.Args()...)
|
||||||
if err != nil {
|
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
|
// if no results returned, this is not a error
|
||||||
// it just means the organization was not found
|
// it just means the organization was not found
|
||||||
// the caller should check if the returned organization is nil
|
// the caller should check if the returned organization is nil
|
||||||
|
Reference in New Issue
Block a user