remove idcondition

This commit is contained in:
adlerhurst
2025-08-05 10:03:15 +02:00
parent e48c565ab6
commit 5a75e9d558
4 changed files with 63 additions and 68 deletions

View File

@@ -125,8 +125,10 @@ func NewActivateOrganizationCommand(instanceID, orgID string) *ActivateOrganizat
func (cmd *ActivateOrganizationCommand) Execute(ctx context.Context, opts *CommandOpts) (err error) { func (cmd *ActivateOrganizationCommand) Execute(ctx context.Context, opts *CommandOpts) (err error) {
repo := orgRepo(opts.DB) repo := orgRepo(opts.DB)
_, err = repo.Update(ctx, _, err = repo.Update(ctx,
repo.IDCondition(cmd.OrgID), database.And(
cmd.InstanceID, repo.InstanceIDCondition(cmd.InstanceID),
repo.IDCondition(cmd.OrgID),
),
repo.SetState(OrgStateActive), repo.SetState(OrgStateActive),
) )
return err return err
@@ -160,8 +162,10 @@ func NewDeactivateOrganizationCommand(instanceID, orgID string) *DeactivateOrgan
func (cmd *DeactivateOrganizationCommand) Execute(ctx context.Context, opts *CommandOpts) (err error) { func (cmd *DeactivateOrganizationCommand) Execute(ctx context.Context, opts *CommandOpts) (err error) {
repo := orgRepo(opts.DB) repo := orgRepo(opts.DB)
_, err = repo.Update(ctx, _, err = repo.Update(ctx,
repo.IDCondition(cmd.OrgID), database.And(
cmd.InstanceID, repo.InstanceIDCondition(cmd.InstanceID),
repo.IDCondition(cmd.OrgID),
),
repo.SetState(OrgStateInactive), repo.SetState(OrgStateInactive),
) )
return err return err
@@ -192,8 +196,10 @@ func NewDeleteOrganizationCommand(instanceID, orgID string) *DeleteOrganizationC
func (cmd *DeleteOrganizationCommand) Execute(ctx context.Context, opts *CommandOpts) (err error) { func (cmd *DeleteOrganizationCommand) Execute(ctx context.Context, opts *CommandOpts) (err error) {
repo := orgRepo(opts.DB) repo := orgRepo(opts.DB)
_, err = repo.Delete(ctx, _, err = repo.Delete(ctx,
repo.IDCondition(cmd.OrgID), database.And(
cmd.InstanceID, repo.InstanceIDCondition(cmd.InstanceID),
repo.IDCondition(cmd.OrgID),
),
) )
return err return err
} }
@@ -238,8 +244,10 @@ func (cmd *UpdateOrganizationCommand) Execute(ctx context.Context, opts *Command
} }
_, err = cmd.repo.Update(ctx, _, err = cmd.repo.Update(ctx,
cmd.repo.IDCondition(cmd.OrgID), database.And(
cmd.InstanceID, cmd.repo.InstanceIDCondition(cmd.InstanceID),
cmd.repo.IDCondition(cmd.OrgID),
),
cmd.changes..., cmd.changes...,
) )
return err return err

View File

@@ -16,8 +16,8 @@ type OrganizationRepository interface {
List(ctx context.Context, opts ...database.QueryOption) ([]*Organization, error) List(ctx context.Context, opts ...database.QueryOption) ([]*Organization, error)
Create(ctx context.Context, instance *Organization) error Create(ctx context.Context, instance *Organization) error
Update(ctx context.Context, id OrgIdentifierCondition, instance_id string, changes ...database.Change) (int64, error) Update(ctx context.Context, condition database.Condition, changes ...database.Change) (int64, error)
Delete(ctx context.Context, id OrgIdentifierCondition, instance_id string) (int64, error) Delete(ctx context.Context, condition database.Condition) (int64, error)
// Domains returns the domain sub repository for the organization. // 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 [Organization].Domains field. // If shouldLoad is true, the domains will be loaded from the database and written to the [Organization].Domains field.
@@ -25,13 +25,6 @@ type OrganizationRepository interface {
Domains(shouldLoad bool) OrganizationDomainRepository Domains(shouldLoad bool) OrganizationDomainRepository
} }
// 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. // organizationColumns define all the columns of the instance table.
type organizationColumns interface { type organizationColumns interface {
// IDColumn returns the column for the id field. // IDColumn returns the column for the id field.
@@ -57,9 +50,9 @@ type organizationColumns interface {
// organizationConditions define all the conditions for the instance table. // organizationConditions define all the conditions for the instance table.
type organizationConditions interface { type organizationConditions interface {
// IDCondition returns an equal filter on the id field. // IDCondition returns an equal filter on the id field.
IDCondition(id string) OrgIdentifierCondition IDCondition(id string) database.Condition
// NameCondition returns a filter on the name field. // 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 returns a filter on the instance id field.
InstanceIDCondition(instanceID string) database.Condition InstanceIDCondition(instanceID string) database.Condition
// StateCondition returns a filter on the name field. // StateCondition returns a filter on the name field.

View File

@@ -103,35 +103,24 @@ func (o *org) Create(ctx context.Context, organization *domain.Organization) err
} }
// Update implements [domain.OrganizationRepository]. // Update implements [domain.OrganizationRepository].
func (o *org) Update(ctx context.Context, id domain.OrgIdentifierCondition, instanceID string, changes ...database.Change) (int64, error) { func (o *org) Update(ctx context.Context, condition database.Condition, changes ...database.Change) (int64, error) {
if len(changes) == 0 { if len(changes) == 0 {
return 0, database.ErrNoChanges return 0, database.ErrNoChanges
} }
builder := database.StatementBuilder{} builder := database.StatementBuilder{}
builder.WriteString(`UPDATE zitadel.organizations SET `) builder.WriteString(`UPDATE zitadel.organizations SET `)
instanceIDCondition := o.InstanceIDCondition(instanceID)
conditions := []database.Condition{id, instanceIDCondition}
database.Changes(changes).Write(&builder) database.Changes(changes).Write(&builder)
writeCondition(&builder, database.And(conditions...)) writeCondition(&builder, condition)
stmt := builder.String() rowsAffected, err := o.client.Exec(ctx, builder.String(), builder.Args()...)
rowsAffected, err := o.client.Exec(ctx, stmt, builder.Args()...)
return rowsAffected, err return rowsAffected, err
} }
// Delete implements [domain.OrganizationRepository]. // Delete implements [domain.OrganizationRepository].
func (o *org) Delete(ctx context.Context, id domain.OrgIdentifierCondition, instanceID string) (int64, error) { func (o *org) Delete(ctx context.Context, condition database.Condition) (int64, error) {
builder := database.StatementBuilder{} builder := database.StatementBuilder{}
builder.WriteString(`DELETE FROM zitadel.organizations`) builder.WriteString(`DELETE FROM zitadel.organizations`)
writeCondition(&builder, condition)
instanceIDCondition := o.InstanceIDCondition(instanceID)
conditions := []database.Condition{id, instanceIDCondition}
writeCondition(&builder, database.And(conditions...))
return o.client.Exec(ctx, builder.String(), builder.Args()...) return o.client.Exec(ctx, builder.String(), builder.Args()...)
} }
@@ -155,13 +144,13 @@ func (o org) SetState(state domain.OrgState) database.Change {
// ------------------------------------------------------------- // -------------------------------------------------------------
// IDCondition implements [domain.organizationConditions]. // IDCondition implements [domain.organizationConditions].
func (o org) IDCondition(id string) domain.OrgIdentifierCondition { func (o org) IDCondition(id string) database.Condition {
return database.NewTextCondition(o.IDColumn(true), database.TextOperationEqual, id) return database.NewTextCondition(o.IDColumn(true), database.TextOperationEqual, id)
} }
// NameCondition implements [domain.organizationConditions]. // NameCondition implements [domain.organizationConditions].
func (o org) NameCondition(name string) domain.OrgIdentifierCondition { func (o org) NameCondition(op database.TextOperation, name string) database.Condition {
return database.NewTextCondition(o.NameColumn(true), database.TextOperationEqual, name) return database.NewTextCondition(o.NameColumn(true), op, name)
} }
// InstanceIDCondition implements [domain.organizationConditions]. // InstanceIDCondition implements [domain.organizationConditions].

View File

@@ -320,8 +320,10 @@ func TestUpdateOrganization(t *testing.T) {
// delete instance // delete instance
_, err = organizationRepo.Delete(ctx, _, err = organizationRepo.Delete(ctx,
organizationRepo.IDCondition(org.ID), database.And(
org.InstanceID, organizationRepo.IDCondition(org.ID),
organizationRepo.InstanceIDCondition(org.InstanceID),
),
) )
require.NoError(t, err) require.NoError(t, err)
@@ -378,8 +380,10 @@ func TestUpdateOrganization(t *testing.T) {
// update org // update org
beforeUpdate := time.Now() beforeUpdate := time.Now()
rowsAffected, err := organizationRepo.Update(ctx, rowsAffected, err := organizationRepo.Update(ctx,
organizationRepo.IDCondition(createdOrg.ID), database.And(
createdOrg.InstanceID, organizationRepo.IDCondition(createdOrg.ID),
organizationRepo.InstanceIDCondition(createdOrg.InstanceID),
),
tt.update..., tt.update...,
) )
afterUpdate := time.Now() afterUpdate := time.Now()
@@ -441,10 +445,10 @@ func TestGetOrganization(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
type test struct { type test struct {
name string name string
testFunc func(ctx context.Context, t *testing.T) *domain.Organization testFunc func(ctx context.Context, t *testing.T) *domain.Organization
orgIdentifierCondition domain.OrgIdentifierCondition condition database.Condition
err error err error
} }
tests := []test{ tests := []test{
@@ -468,7 +472,7 @@ func TestGetOrganization(t *testing.T) {
return &org return &org
}, },
orgIdentifierCondition: orgRepo.IDCondition(organizationId), condition: orgRepo.IDCondition(organizationId),
} }
}(), }(),
func() test { func() test {
@@ -491,7 +495,7 @@ func TestGetOrganization(t *testing.T) {
return &org return &org
}, },
orgIdentifierCondition: orgRepo.NameCondition(organizationName), condition: orgRepo.NameCondition(database.TextOperationEqual, organizationName),
} }
}(), }(),
{ {
@@ -503,8 +507,8 @@ func TestGetOrganization(t *testing.T) {
} }
return &org return &org
}, },
orgIdentifierCondition: orgRepo.NameCondition("non-existent-instance-name"), condition: orgRepo.NameCondition(database.TextOperationEqual, "non-existent-instance-name"),
err: new(database.NoRowFoundError), err: new(database.NoRowFoundError),
}, },
} }
for _, tt := range tests { for _, tt := range tests {
@@ -521,7 +525,7 @@ func TestGetOrganization(t *testing.T) {
returnedOrg, err := orgRepo.Get(ctx, returnedOrg, err := orgRepo.Get(ctx,
database.WithCondition( database.WithCondition(
database.And( database.And(
tt.orgIdentifierCondition, tt.condition,
orgRepo.InstanceIDCondition(org.InstanceID), orgRepo.InstanceIDCondition(org.InstanceID),
), ),
), ),
@@ -821,10 +825,10 @@ func TestDeleteOrganization(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
type test struct { type test struct {
name string name string
testFunc func(ctx context.Context, t *testing.T) testFunc func(ctx context.Context, t *testing.T)
orgIdentifierCondition domain.OrgIdentifierCondition condition database.Condition
noOfDeletedRows int64 noOfDeletedRows int64
} }
tests := []test{ tests := []test{
func() test { func() test {
@@ -851,8 +855,8 @@ func TestDeleteOrganization(t *testing.T) {
organizations[i] = &org organizations[i] = &org
} }
}, },
orgIdentifierCondition: organizationRepo.IDCondition(organizationId), condition: organizationRepo.IDCondition(organizationId),
noOfDeletedRows: noOfOrganizations, noOfDeletedRows: noOfOrganizations,
} }
}(), }(),
func() test { func() test {
@@ -879,16 +883,16 @@ func TestDeleteOrganization(t *testing.T) {
organizations[i] = &org organizations[i] = &org
} }
}, },
orgIdentifierCondition: organizationRepo.NameCondition(organizationName), condition: organizationRepo.NameCondition(database.TextOperationEqual, organizationName),
noOfDeletedRows: noOfOrganizations, noOfDeletedRows: noOfOrganizations,
} }
}(), }(),
func() test { func() test {
organizationRepo := repository.OrganizationRepository(pool) organizationRepo := repository.OrganizationRepository(pool)
non_existent_organization_name := gofakeit.Name() nonExistentOrgName := gofakeit.Name()
return test{ return test{
name: "delete non existent organization", name: "delete non existent organization",
orgIdentifierCondition: organizationRepo.NameCondition(non_existent_organization_name), condition: organizationRepo.NameCondition(database.TextOperationEqual, nonExistentOrgName),
} }
}(), }(),
func() test { func() test {
@@ -917,13 +921,15 @@ func TestDeleteOrganization(t *testing.T) {
// delete organization // delete organization
affectedRows, err := organizationRepo.Delete(ctx, affectedRows, err := organizationRepo.Delete(ctx,
organizationRepo.NameCondition(organizationName), database.And(
organizations[0].InstanceID, organizationRepo.NameCondition(database.TextOperationEqual, organizationName),
organizationRepo.InstanceIDCondition(organizations[0].InstanceID),
),
) )
assert.Equal(t, int64(1), affectedRows) assert.Equal(t, int64(1), affectedRows)
require.NoError(t, err) require.NoError(t, err)
}, },
orgIdentifierCondition: organizationRepo.NameCondition(organizationName), condition: organizationRepo.NameCondition(database.TextOperationEqual, organizationName),
// this test should return 0 affected rows as the org was already deleted // this test should return 0 affected rows as the org was already deleted
noOfDeletedRows: 0, noOfDeletedRows: 0,
} }
@@ -940,8 +946,7 @@ func TestDeleteOrganization(t *testing.T) {
// delete organization // delete organization
noOfDeletedRows, err := organizationRepo.Delete(ctx, noOfDeletedRows, err := organizationRepo.Delete(ctx,
tt.orgIdentifierCondition, tt.condition,
instanceId,
) )
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, noOfDeletedRows, tt.noOfDeletedRows) assert.Equal(t, noOfDeletedRows, tt.noOfDeletedRows)
@@ -950,7 +955,7 @@ func TestDeleteOrganization(t *testing.T) {
organization, err := organizationRepo.Get(ctx, organization, err := organizationRepo.Get(ctx,
database.WithCondition( database.WithCondition(
database.And( database.And(
tt.orgIdentifierCondition, tt.condition,
organizationRepo.InstanceIDCondition(instanceId), organizationRepo.InstanceIDCondition(instanceId),
), ),
), ),