fixup! Merge branch 'instance_table_2' into org_table

This commit is contained in:
Iraq Jaber
2025-06-17 12:51:45 +02:00
parent 52e48235cf
commit ae0d759413
10 changed files with 476 additions and 494 deletions

View File

@@ -17,11 +17,11 @@ const (
type Organization struct {
ID string `json:"id,omitempty" db:"id"`
Name string `json:"name,omitempty" db:"name"`
InstanceID string `json:"instance_id,omitempty" db:"instance_id"`
InstanceID string `json:"instanceId,omitempty" db:"instance_id"`
State State `json:"state,omitempty" db:"state"`
CreatedAt time.Time `json:"created_at,omitempty" db:"created_at"`
UpdatedAt time.Time `json:"updated_at,omitempty" db:"updated_at"`
DeletedAt *time.Time `json:"deleted_at,omitempty" db:"deleted_at"`
CreatedAt time.Time `json:"createdAt,omitempty" db:"created_at"`
UpdatedAt time.Time `json:"updatedAt,omitempty" db:"updated_at"`
DeletedAt *time.Time `json:"deletedAt,omitempty" db:"deleted_at"`
}
// organizationColumns define all the columns of the instance table.
@@ -48,12 +48,15 @@ type organizationConditions interface {
IDCondition(instanceID string) database.Condition
// NameCondition returns a filter on the name field.
NameCondition(op database.TextOperation, name string) database.Condition
// StateCondition returns a filter on the name field.
StateCondition(state State) 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 State) database.Change
}

View File

@@ -12,3 +12,9 @@ CREATE TABLE zitadel.organizations(
updated_at TIMESTAMPTZ DEFAULT NOW(),
deleted_at TIMESTAMPTZ DEFAULT NULL
);
CREATE TRIGGER trigger_set_updated_at
BEFORE UPDATE ON zitadel.organizations
FOR EACH ROW
WHEN (OLD.updated_at IS NOT DISTINCT FROM NEW.updated_at)
EXECUTE FUNCTION zitadel.set_updated_at();

View File

@@ -28,7 +28,7 @@ func TestServer_TestOrganizationAddReduces(t *testing.T) {
require.NoError(t, err)
afterCreate := time.Now()
orgRepo := repository.OrgRepository(pool)
orgRepo := repository.OrganizationRepository(pool)
retryDuration, tick := integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
assert.EventuallyWithT(t, func(t *assert.CollectT) {
@@ -66,7 +66,7 @@ func TestServer_TestOrganizationChangeReduces(t *testing.T) {
require.NoError(t, err)
afterCreate := time.Now()
orgRepo := repository.OrgRepository(pool)
orgRepo := repository.OrganizationRepository(pool)
retryDuration, tick := integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
assert.EventuallyWithT(t, func(t *assert.CollectT) {
@@ -96,7 +96,7 @@ func TestServer_TestOrganizationDeactivateReduces(t *testing.T) {
require.NoError(t, err)
afterCreate := time.Now()
orgRepo := repository.OrgRepository(pool)
orgRepo := repository.OrganizationRepository(pool)
retryDuration, tick := integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
assert.EventuallyWithT(t, func(t *assert.CollectT) {
@@ -131,7 +131,7 @@ func TestServer_TestOrganizationActivateReduces(t *testing.T) {
require.NoError(t, err)
afterCreate := time.Now()
orgRepo := repository.OrgRepository(pool)
orgRepo := repository.OrganizationRepository(pool)
retryDuration, tick := integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
assert.EventuallyWithT(t, func(t *assert.CollectT) {
@@ -157,7 +157,7 @@ func TestServer_TestOrganizationRemoveReduces(t *testing.T) {
require.NoError(t, err)
// 2. check org retrivable
orgRepo := repository.OrgRepository(pool)
orgRepo := repository.OrganizationRepository(pool)
retryDuration, tick := integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
assert.EventuallyWithT(t, func(t *assert.CollectT) {
organization, err := orgRepo.Get(CTX,

View File

@@ -96,6 +96,9 @@ func (i *instance) Create(ctx context.Context, instance *domain.Instance) error
// Update implements [domain.InstanceRepository].
func (i instance) Update(ctx context.Context, condition database.Condition, changes ...database.Change) (int64, error) {
if changes == nil {
return 0, nil
}
var builder database.StatementBuilder
builder.WriteString(`UPDATE zitadel.instances SET `)

View File

@@ -340,7 +340,6 @@ func TestGetInstance(t *testing.T) {
require.Nil(t, instance, returnedInstance)
return
}
require.NoError(t, err)
require.Equal(t, returnedInstance.ID, instance.ID)
require.Equal(t, returnedInstance.Name, instance.Name)

View File

@@ -20,7 +20,7 @@ type org struct {
repository
}
func OrgRepository(client database.QueryExecutor) domain.OrganizationRepository {
func OrganizationRepository(client database.QueryExecutor) domain.OrganizationRepository {
return &org{
repository: repository{
client: client,
@@ -110,10 +110,16 @@ func (o *org) Create(ctx context.Context, organization *domain.Organization) err
// Update implements [domain.OrganizationRepository].
func (o org) Update(ctx context.Context, condition database.Condition, changes ...database.Change) (int64, error) {
if changes == nil {
return 0, nil
}
builder := database.StatementBuilder{}
builder.WriteString(`UPDATE zitadel.organizations SET `)
// don't update deleted instances
conditions := []database.Condition{condition, database.IsNull(o.DeletedAtColumn())}
database.Changes(changes).Write(&builder)
o.writeCondition(&builder, condition)
o.writeCondition(&builder, database.And(conditions...))
stmt := builder.String()
@@ -163,6 +169,11 @@ func (o org) NameCondition(op database.TextOperation, name string) database.Cond
return database.NewTextCondition(o.NameColumn(), op, name)
}
// StateCondition implements [domain.organizationConditions].
func (o org) StateCondition(state domain.State) database.Condition {
return database.NewTextCondition(o.StateColumn(), database.TextOperationEqual, state)
}
// -------------------------------------------------------------
// columns
// -------------------------------------------------------------

File diff suppressed because it is too large Load Diff