Merge branch 'clean-transactional-propsal' into rt-domains

This commit is contained in:
adlerhurst
2025-07-16 09:28:57 +02:00
14 changed files with 44 additions and 180 deletions

View File

@@ -9,16 +9,15 @@ import (
)
type Instance struct {
ID string `json:"id,omitempty" db:"id"`
Name string `json:"name,omitempty" db:"name"`
DefaultOrgID string `json:"defaultOrgId,omitempty" db:"default_org_id"`
IAMProjectID string `json:"iamProjectId,omitempty" db:"iam_project_id"`
ConsoleClientID string `json:"consoleClientId,omitempty" db:"console_client_id"`
ConsoleAppID string `json:"consoleAppId,omitempty" db:"console_app_id"`
DefaultLanguage string `json:"defaultLanguage,omitempty" db:"default_language"`
CreatedAt time.Time `json:"createdAt" db:"created_at"`
UpdatedAt time.Time `json:"updatedAt" db:"updated_at"`
DeletedAt *time.Time `json:"deletedAt" db:"deleted_at"`
ID string `json:"id,omitempty" db:"id"`
Name string `json:"name,omitempty" db:"name"`
DefaultOrgID string `json:"defaultOrgId,omitempty" db:"default_org_id"`
IAMProjectID string `json:"iamProjectId,omitempty" db:"iam_project_id"`
ConsoleClientID string `json:"consoleClientId,omitempty" db:"console_client_id"`
ConsoleAppID string `json:"consoleAppId,omitempty" db:"console_app_id"`
DefaultLanguage string `json:"defaultLanguage,omitempty" db:"default_language"`
CreatedAt time.Time `json:"createdAt" db:"created_at"`
UpdatedAt time.Time `json:"updatedAt" db:"updated_at"`
}
type instanceCacheIndex uint8
@@ -58,8 +57,6 @@ type instanceColumns interface {
CreatedAtColumn() database.Column
// UpdatedAtColumn returns the column for the updated at field.
UpdatedAtColumn() database.Column
// DeletedAtColumn returns the column for the deleted at field.
DeletedAtColumn() database.Column
}
// instanceConditions define all the conditions for the instance table.

View File

@@ -7,22 +7,20 @@ import (
"github.com/zitadel/zitadel/backend/v3/storage/database"
)
//go:generate enumer -type OrgState -transform lower -trimprefix OrgState
type OrgState uint8
type OrgState string
const (
OrgStateActive OrgState = iota
OrgStateInactive
OrgStateActive OrgState = "active"
OrgStateInactive OrgState = "inactive"
)
type Organization struct {
ID string `json:"id,omitempty" db:"id"`
Name string `json:"name,omitempty" db:"name"`
InstanceID string `json:"instanceId,omitempty" db:"instance_id"`
State string `json:"state,omitempty" db:"state"`
CreatedAt time.Time `json:"createdAt,omitzero" db:"created_at"`
UpdatedAt time.Time `json:"updatedAt,omitzero" db:"updated_at"`
DeletedAt *time.Time `json:"deletedAt,omitempty" db:"deleted_at"`
ID string `json:"id,omitempty" db:"id"`
Name string `json:"name,omitempty" db:"name"`
InstanceID string `json:"instanceId,omitempty" db:"instance_id"`
State OrgState `json:"state,omitempty" db:"state"`
CreatedAt time.Time `json:"createdAt,omitempty" db:"created_at"`
UpdatedAt time.Time `json:"updatedAt,omitempty" db:"updated_at"`
}
// OrgIdentifierCondition is used to help specify a single Organization,
@@ -46,8 +44,6 @@ type organizationColumns interface {
CreatedAtColumn() database.Column
// UpdatedAtColumn returns the column for the updated at field.
UpdatedAtColumn() database.Column
// DeletedAtColumn returns the column for the deleted at field.
DeletedAtColumn() database.Column
}
// organizationConditions define all the conditions for the instance table.
@@ -77,7 +73,7 @@ type OrganizationRepository interface {
organizationChanges
Get(ctx context.Context, id OrgIdentifierCondition, instance_id string, opts ...database.Condition) (*Organization, error)
List(ctx context.Context, opts ...database.Condition) ([]*Organization, error)
List(ctx context.Context, conditions ...database.Condition) ([]*Organization, error)
Create(ctx context.Context, instance *Organization) error
Update(ctx context.Context, id OrgIdentifierCondition, instance_id string, changes ...database.Change) (int64, error)

View File

@@ -1,78 +0,0 @@
// Code generated by "enumer -type OrgState -transform lower -trimprefix OrgState"; DO NOT EDIT.
package domain
import (
"fmt"
"strings"
)
const _OrgStateName = "activeinactive"
var _OrgStateIndex = [...]uint8{0, 6, 14}
const _OrgStateLowerName = "activeinactive"
func (i OrgState) String() string {
if i < 0 || i >= OrgState(len(_OrgStateIndex)-1) {
return fmt.Sprintf("OrgState(%d)", i)
}
return _OrgStateName[_OrgStateIndex[i]:_OrgStateIndex[i+1]]
}
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
func _OrgStateNoOp() {
var x [1]struct{}
_ = x[OrgStateActive-(0)]
_ = x[OrgStateInactive-(1)]
}
var _OrgStateValues = []OrgState{OrgStateActive, OrgStateInactive}
var _OrgStateNameToValueMap = map[string]OrgState{
_OrgStateName[0:6]: OrgStateActive,
_OrgStateLowerName[0:6]: OrgStateActive,
_OrgStateName[6:14]: OrgStateInactive,
_OrgStateLowerName[6:14]: OrgStateInactive,
}
var _OrgStateNames = []string{
_OrgStateName[0:6],
_OrgStateName[6:14],
}
// OrgStateString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum.
func OrgStateString(s string) (OrgState, error) {
if val, ok := _OrgStateNameToValueMap[s]; ok {
return val, nil
}
if val, ok := _OrgStateNameToValueMap[strings.ToLower(s)]; ok {
return val, nil
}
return 0, fmt.Errorf("%s does not belong to OrgState values", s)
}
// OrgStateValues returns all values of the enum
func OrgStateValues() []OrgState {
return _OrgStateValues
}
// OrgStateStrings returns a slice of all String values of the enum
func OrgStateStrings() []string {
strs := make([]string, len(_OrgStateNames))
copy(strs, _OrgStateNames)
return strs
}
// IsAOrgState returns "true" if the value is listed in the enum definition. "false" otherwise
func (i OrgState) IsAOrgState() bool {
for _, v := range _OrgStateValues {
if i == v {
return true
}
}
return false
}

View File

@@ -7,8 +7,7 @@ CREATE TABLE IF NOT EXISTS zitadel.instances(
console_app_id TEXT, -- NOT NULL,
default_language TEXT, -- NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW() NOT NULL,
updated_at TIMESTAMPTZ DEFAULT NOW() NOT NULL,
deleted_at TIMESTAMPTZ DEFAULT NULL
updated_at TIMESTAMPTZ DEFAULT NOW() NOT NULL
);
CREATE OR REPLACE FUNCTION zitadel.set_updated_at()

View File

@@ -10,21 +10,12 @@ CREATE TABLE zitadel.organizations(
state zitadel.organization_state NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW() NOT NULL,
updated_at TIMESTAMPTZ DEFAULT NOW() NOT NULL,
deleted_at TIMESTAMPTZ DEFAULT NULL,
PRIMARY KEY (instance_id, id)
);
CREATE UNIQUE INDEX org_unique_instance_id_name_idx
ON zitadel.organizations (instance_id, name)
WHERE deleted_at IS NULL;
-- users are able to set the id for organizations
CREATE INDEX org_id_not_deleted_idx ON zitadel.organizations (id)
WHERE deleted_at IS NULL;
CREATE INDEX org_name_not_deleted_idx ON zitadel.organizations (name)
WHERE deleted_at IS NULL;
ON zitadel.organizations (instance_id, name);
CREATE TRIGGER trigger_set_updated_at
BEFORE UPDATE ON zitadel.organizations

View File

@@ -54,7 +54,6 @@ func TestServer_TestInstanceReduces(t *testing.T) {
assert.WithinRange(t, instance.CreatedAt, beforeCreate, afterCreate)
// event instance.added
assert.WithinRange(t, instance.UpdatedAt, beforeCreate, afterCreate)
assert.Nil(t, instance.DeletedAt)
}, retryDuration, tick)
})
@@ -132,7 +131,7 @@ func TestServer_TestInstanceReduces(t *testing.T) {
)
// event instance.removed
assert.Nil(t, instance)
require.NoError(t, err)
require.Equal(t, repository.ErrResourceDoesNotExist, err)
}, retryDuration, tick)
})
}

View File

@@ -46,7 +46,6 @@ func TestServer_TestOrganizationReduces(t *testing.T) {
assert.Equal(t, domain.OrgStateActive.String(), organization.State)
assert.WithinRange(t, organization.CreatedAt, beforeCreate, afterCreate)
assert.WithinRange(t, organization.UpdatedAt, beforeCreate, afterCreate)
assert.Nil(t, organization.DeletedAt)
}, retryDuration, tick)
})
@@ -209,7 +208,7 @@ func TestServer_TestOrganizationReduces(t *testing.T) {
orgRepo.NameCondition(orgName),
instanceID,
)
require.NoError(t, err)
require.Equal(t, repository.ErrResourceDoesNotExist, err)
// event org.remove
assert.Nil(t, organization)

View File

@@ -3,7 +3,6 @@ package repository
import (
"context"
"errors"
"time"
"github.com/jackc/pgx/v5/pgconn"
@@ -31,7 +30,7 @@ func InstanceRepository(client database.QueryExecutor) domain.InstanceRepository
// repository
// -------------------------------------------------------------
const queryInstanceStmt = `SELECT id, name, default_org_id, iam_project_id, console_client_id, console_app_id, default_language, created_at, updated_at, deleted_at` +
const queryInstanceStmt = `SELECT id, name, default_org_id, iam_project_id, console_client_id, console_app_id, default_language, created_at, updated_at` +
` FROM zitadel.instances`
// Get implements [domain.InstanceRepository].
@@ -41,23 +40,20 @@ func (i *instance) Get(ctx context.Context, id string) (*domain.Instance, error)
builder.WriteString(queryInstanceStmt)
idCondition := i.IDCondition(id)
// return only non deleted instances
conditions := []database.Condition{idCondition, database.IsNull(i.DeletedAtColumn())}
writeCondition(&builder, database.And(conditions...))
writeCondition(&builder, idCondition)
return scanInstance(ctx, i.client, &builder)
}
// List implements [domain.InstanceRepository].
func (i *instance) List(ctx context.Context, opts ...database.Condition) ([]*domain.Instance, error) {
func (i *instance) List(ctx context.Context, conditions ...database.Condition) ([]*domain.Instance, error) {
var builder database.StatementBuilder
builder.WriteString(queryInstanceStmt)
// return only non deleted instances
opts = append(opts, database.IsNull(i.DeletedAtColumn()))
notDeletedCondition := database.And(opts...)
writeCondition(&builder, notDeletedCondition)
if conditions != nil {
writeCondition(&builder, database.And(conditions...))
}
return scanInstances(ctx, i.client, &builder)
}
@@ -109,9 +105,7 @@ func (i instance) Update(ctx context.Context, id string, changes ...database.Cha
database.Changes(changes).Write(&builder)
idCondition := i.IDCondition(id)
// don't update deleted instances
conditions := []database.Condition{idCondition, database.IsNull(i.DeletedAtColumn())}
writeCondition(&builder, database.And(conditions...))
writeCondition(&builder, idCondition)
stmt := builder.String()
@@ -123,13 +117,10 @@ func (i instance) Update(ctx context.Context, id string, changes ...database.Cha
func (i instance) Delete(ctx context.Context, id string) (int64, error) {
var builder database.StatementBuilder
builder.WriteString(`UPDATE zitadel.instances SET deleted_at = $1`)
builder.AppendArgs(time.Now())
builder.WriteString(`DELETE FROM zitadel.instances`)
// don't delete already deleted instance
idCondition := i.IDCondition(id)
conditions := []database.Condition{idCondition, database.IsNull(i.DeletedAtColumn())}
writeCondition(&builder, database.And(conditions...))
writeCondition(&builder, idCondition)
return i.client.Exec(ctx, builder.String(), builder.Args()...)
}
@@ -206,11 +197,6 @@ func (instance) UpdatedAtColumn() database.Column {
return database.NewColumn("updated_at")
}
// DeletedAtColumn implements [domain.instanceColumns].
func (instance) DeletedAtColumn() database.Column {
return database.NewColumn("deleted_at")
}
func scanInstance(ctx context.Context, querier database.Querier, builder *database.StatementBuilder) (*domain.Instance, error) {
rows, err := querier.Query(ctx, builder.String(), builder.Args()...)
if err != nil {

View File

@@ -185,7 +185,6 @@ func TestCreateInstance(t *testing.T) {
assert.Equal(t, tt.instance.DefaultLanguage, instance.DefaultLanguage)
assert.WithinRange(t, instance.CreatedAt, beforeCreate, afterCreate)
assert.WithinRange(t, instance.UpdatedAt, beforeCreate, afterCreate)
assert.Nil(t, instance.DeletedAt)
})
}
}
@@ -298,7 +297,6 @@ func TestUpdateInstance(t *testing.T) {
assert.Equal(t, newName, instance.Name)
assert.WithinRange(t, instance.UpdatedAt, beforeUpdate, afterUpdate)
assert.Nil(t, instance.DeletedAt)
})
}
}

View File

@@ -3,7 +3,6 @@ package repository
import (
"context"
"errors"
"time"
"github.com/jackc/pgx/v5/pgconn"
@@ -31,7 +30,7 @@ func OrganizationRepository(client database.QueryExecutor) domain.OrganizationRe
}
}
const queryOrganizationStmt = `SELECT id, name, instance_id, state, created_at, updated_at, deleted_at` +
const queryOrganizationStmt = `SELECT id, name, instance_id, state, created_at, updated_at` +
` FROM zitadel.organizations`
// Get implements [domain.OrganizationRepository].
@@ -41,24 +40,22 @@ func (o *org) Get(ctx context.Context, id domain.OrgIdentifierCondition, instanc
builder.WriteString(queryOrganizationStmt)
instanceIDCondition := o.InstanceIDCondition(instanceID)
// don't update deleted organizations
nonDeletedOrgs := database.IsNull(o.DeletedAtColumn())
conditions = append(conditions, id, instanceIDCondition, nonDeletedOrgs)
conditions = append(conditions, id, instanceIDCondition)
writeCondition(&builder, database.And(conditions...))
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, conditions ...database.Condition) ([]*domain.Organization, error) {
builder := database.StatementBuilder{}
builder.WriteString(queryOrganizationStmt)
// return only non deleted organizations
opts = append(opts, database.IsNull(o.DeletedAtColumn()))
writeCondition(&builder, database.And(opts...))
if conditions != nil {
writeCondition(&builder, database.And(conditions...))
}
orderBy := database.OrderBy(o.CreatedAtColumn())
orderBy.Write(&builder)
@@ -124,10 +121,8 @@ func (o org) Update(ctx context.Context, id domain.OrgIdentifierCondition, insta
builder.WriteString(`UPDATE zitadel.organizations SET `)
instanceIDCondition := o.InstanceIDCondition(instanceID)
// don't update deleted organizations
nonDeletedOrgs := database.IsNull(o.DeletedAtColumn())
conditions := []database.Condition{id, instanceIDCondition, nonDeletedOrgs}
conditions := []database.Condition{id, instanceIDCondition}
database.Changes(changes).Write(&builder)
writeCondition(&builder, database.And(conditions...))
@@ -141,14 +136,11 @@ func (o org) Update(ctx context.Context, id domain.OrgIdentifierCondition, insta
func (o org) Delete(ctx context.Context, id domain.OrgIdentifierCondition, instanceID string) (int64, error) {
builder := database.StatementBuilder{}
builder.WriteString(`UPDATE zitadel.organizations SET deleted_at = $1`)
builder.AppendArgs(time.Now())
builder.WriteString(`DELETE FROM zitadel.organizations`)
instanceIDCondition := o.InstanceIDCondition(instanceID)
// don't update deleted organizations
nonDeletedOrgs := database.IsNull(o.DeletedAtColumn())
conditions := []database.Condition{id, instanceIDCondition, nonDeletedOrgs}
conditions := []database.Condition{id, instanceIDCondition}
writeCondition(&builder, database.And(conditions...))
return o.client.Exec(ctx, builder.String(), builder.Args()...)
@@ -189,7 +181,7 @@ func (o org) InstanceIDCondition(instanceID string) database.Condition {
// StateCondition implements [domain.organizationConditions].
func (o org) StateCondition(state domain.OrgState) database.Condition {
return database.NewTextCondition(o.StateColumn(), database.TextOperationEqual, state.String())
return database.NewTextCondition(o.StateColumn(), database.TextOperationEqual, state)
}
// -------------------------------------------------------------
@@ -226,11 +218,6 @@ func (org) UpdatedAtColumn() database.Column {
return database.NewColumn("updated_at")
}
// DeletedAtColumn implements [domain.organizationColumns].
func (org) DeletedAtColumn() database.Column {
return database.NewColumn("deleted_at")
}
// -------------------------------------------------------------
// scanners
// -------------------------------------------------------------

View File

@@ -247,7 +247,6 @@ func TestCreateOrganization(t *testing.T) {
assert.Equal(t, tt.organization.State, organization.State)
assert.WithinRange(t, organization.CreatedAt, beforeCreate, afterCreate)
assert.WithinRange(t, organization.UpdatedAt, beforeCreate, afterCreate)
assert.Nil(t, organization.DeletedAt)
})
}
}
@@ -400,7 +399,6 @@ func TestUpdateOrganization(t *testing.T) {
assert.Equal(t, createdOrg.Name, organization.Name)
assert.Equal(t, createdOrg.State, organization.State)
assert.WithinRange(t, organization.UpdatedAt, beforeUpdate, afterUpdate)
assert.Nil(t, organization.DeletedAt)
})
}
}

View File

@@ -97,11 +97,8 @@ func (p *instanceRelationalProjection) reduceInstanceDelete(event eventstore.Eve
if !ok {
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-so2am1", "reduce.wrong.event.type %s", instance.InstanceChangedEventType)
}
return handler.NewUpdateStatement(
return handler.NewDeleteStatement(
e,
[]handler.Column{
handler.NewCol(DeletedAt, e.CreationDate()),
},
[]handler.Condition{
handler.NewCond(InstanceColumnID, e.Aggregate().InstanceID),
},

View File

@@ -171,12 +171,8 @@ func (p *orgRelationalProjection) reduceOrgRelationalRemoved(event eventstore.Ev
if !ok {
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-DGm9g", "reduce.wrong.event.type %s", org.OrgRemovedEventType)
}
return handler.NewUpdateStatement(
return handler.NewDeleteStatement(
e,
[]handler.Column{
handler.NewCol(UpdatedAt, e.CreationDate()),
handler.NewCol(DeletedAt, e.CreationDate()),
},
[]handler.Condition{
handler.NewCond(OrgColumnID, e.Aggregate().ID),
handler.NewCond(OrgColumnInstanceID, e.Aggregate().InstanceID),

View File

@@ -4,5 +4,4 @@ const (
State = "state"
CreatedAt = "created_at"
UpdatedAt = "updated_at"
DeletedAt = "deleted_at"
)