From b8d5d0a180ffa2f362d9400e73e7a08ddb5d74ee Mon Sep 17 00:00:00 2001 From: Michal Krzyz Date: Tue, 1 Oct 2024 15:48:21 +0200 Subject: [PATCH] feat(authN): Add to all DB entries 'Modified_by'... (#81) Add metadata for componentInstance, componentVersion --- internal/api/graphql/graph/generated.go | 234 ++++++++++-------- internal/api/graphql/graph/model/models.go | 8 +- .../api/graphql/graph/model/models_gen.go | 4 +- .../componentInstance/directRelations.graphql | 8 +- .../issue/directRelations.graphql | 6 +- .../graph/schema/component_instance.graphqls | 5 +- .../graph/schema/component_version.graphqls | 3 +- .../component_instance_handler.go | 2 + .../component_version_handler.go | 2 + .../database/mariadb/component_instance.go | 10 +- .../database/mariadb/component_version.go | 9 +- internal/database/mariadb/entity.go | 12 + internal/database/mariadb/init/schema.sql | 4 + 13 files changed, 184 insertions(+), 123 deletions(-) diff --git a/internal/api/graphql/graph/generated.go b/internal/api/graphql/graph/generated.go index 924e2d10..c8f2b98e 100644 --- a/internal/api/graphql/graph/generated.go +++ b/internal/api/graphql/graph/generated.go @@ -154,12 +154,11 @@ type ComplexityRoot struct { ComponentVersion func(childComplexity int) int ComponentVersionID func(childComplexity int) int Count func(childComplexity int) int - CreatedAt func(childComplexity int) int ID func(childComplexity int) int IssueMatches func(childComplexity int, filter *model.IssueMatchFilter, first *int, after *string) int + Metadata func(childComplexity int) int Service func(childComplexity int) int ServiceID func(childComplexity int) int - UpdatedAt func(childComplexity int) int } ComponentInstanceConnection struct { @@ -179,6 +178,7 @@ type ComplexityRoot struct { ComponentInstances func(childComplexity int, first *int, after *string) int ID func(childComplexity int) int Issues func(childComplexity int, first *int, after *string) int + Metadata func(childComplexity int) int Version func(childComplexity int) int } @@ -1148,13 +1148,6 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.ComponentInstance.Count(childComplexity), true - case "ComponentInstance.createdAt": - if e.complexity.ComponentInstance.CreatedAt == nil { - break - } - - return e.complexity.ComponentInstance.CreatedAt(childComplexity), true - case "ComponentInstance.id": if e.complexity.ComponentInstance.ID == nil { break @@ -1174,6 +1167,13 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.ComponentInstance.IssueMatches(childComplexity, args["filter"].(*model.IssueMatchFilter), args["first"].(*int), args["after"].(*string)), true + case "ComponentInstance.metadata": + if e.complexity.ComponentInstance.Metadata == nil { + break + } + + return e.complexity.ComponentInstance.Metadata(childComplexity), true + case "ComponentInstance.service": if e.complexity.ComponentInstance.Service == nil { break @@ -1188,13 +1188,6 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.ComponentInstance.ServiceID(childComplexity), true - case "ComponentInstance.updatedAt": - if e.complexity.ComponentInstance.UpdatedAt == nil { - break - } - - return e.complexity.ComponentInstance.UpdatedAt(childComplexity), true - case "ComponentInstanceConnection.edges": if e.complexity.ComponentInstanceConnection.Edges == nil { break @@ -1275,6 +1268,13 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.ComponentVersion.Issues(childComplexity, args["first"].(*int), args["after"].(*string)), true + case "ComponentVersion.metadata": + if e.complexity.ComponentVersion.Metadata == nil { + break + } + + return e.complexity.ComponentVersion.Metadata(childComplexity), true + case "ComponentVersion.version": if e.complexity.ComponentVersion.Version == nil { break @@ -12632,6 +12632,8 @@ func (ec *executionContext) fieldContext_ComponentInstance_componentVersion(_ co return ec.fieldContext_ComponentVersion_issues(ctx, field) case "componentInstances": return ec.fieldContext_ComponentVersion_componentInstances(ctx, field) + case "metadata": + return ec.fieldContext_ComponentVersion_metadata(ctx, field) } return nil, fmt.Errorf("no field named %q was found under type ComponentVersion", field.Name) }, @@ -12797,49 +12799,8 @@ func (ec *executionContext) fieldContext_ComponentInstance_service(_ context.Con return fc, nil } -func (ec *executionContext) _ComponentInstance_createdAt(ctx context.Context, field graphql.CollectedField, obj *model.ComponentInstance) (ret graphql.Marshaler) { - fc, err := ec.fieldContext_ComponentInstance_createdAt(ctx, field) - if err != nil { - return graphql.Null - } - ctx = graphql.WithFieldContext(ctx, fc) - defer func() { - if r := recover(); r != nil { - ec.Error(ctx, ec.Recover(ctx, r)) - ret = graphql.Null - } - }() - resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { - ctx = rctx // use context from middleware stack in children - return obj.CreatedAt, nil - }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } - if resTmp == nil { - return graphql.Null - } - res := resTmp.(*string) - fc.Result = res - return ec.marshalODateTime2ᚖstring(ctx, field.Selections, res) -} - -func (ec *executionContext) fieldContext_ComponentInstance_createdAt(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { - fc = &graphql.FieldContext{ - Object: "ComponentInstance", - Field: field, - IsMethod: false, - IsResolver: false, - Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { - return nil, errors.New("field of type DateTime does not have child fields") - }, - } - return fc, nil -} - -func (ec *executionContext) _ComponentInstance_updatedAt(ctx context.Context, field graphql.CollectedField, obj *model.ComponentInstance) (ret graphql.Marshaler) { - fc, err := ec.fieldContext_ComponentInstance_updatedAt(ctx, field) +func (ec *executionContext) _ComponentInstance_metadata(ctx context.Context, field graphql.CollectedField, obj *model.ComponentInstance) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_ComponentInstance_metadata(ctx, field) if err != nil { return graphql.Null } @@ -12852,7 +12813,7 @@ func (ec *executionContext) _ComponentInstance_updatedAt(ctx context.Context, fi }() resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.UpdatedAt, nil + return obj.Metadata, nil }) if err != nil { ec.Error(ctx, err) @@ -12861,19 +12822,31 @@ func (ec *executionContext) _ComponentInstance_updatedAt(ctx context.Context, fi if resTmp == nil { return graphql.Null } - res := resTmp.(*string) + res := resTmp.(*model.Metadata) fc.Result = res - return ec.marshalODateTime2ᚖstring(ctx, field.Selections, res) + return ec.marshalOMetadata2ᚖgithubᚗcomᚋcloudoperatorsᚋheurekaᚋinternalᚋapiᚋgraphqlᚋgraphᚋmodelᚐMetadata(ctx, field.Selections, res) } -func (ec *executionContext) fieldContext_ComponentInstance_updatedAt(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { +func (ec *executionContext) fieldContext_ComponentInstance_metadata(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { fc = &graphql.FieldContext{ Object: "ComponentInstance", Field: field, IsMethod: false, IsResolver: false, Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { - return nil, errors.New("field of type DateTime does not have child fields") + switch field.Name { + case "created_at": + return ec.fieldContext_Metadata_created_at(ctx, field) + case "created_by": + return ec.fieldContext_Metadata_created_by(ctx, field) + case "deleted_at": + return ec.fieldContext_Metadata_deleted_at(ctx, field) + case "updated_at": + return ec.fieldContext_Metadata_updated_at(ctx, field) + case "updated_by": + return ec.fieldContext_Metadata_updated_by(ctx, field) + } + return nil, fmt.Errorf("no field named %q was found under type Metadata", field.Name) }, } return fc, nil @@ -13083,10 +13056,8 @@ func (ec *executionContext) fieldContext_ComponentInstanceEdge_node(_ context.Co return ec.fieldContext_ComponentInstance_serviceId(ctx, field) case "service": return ec.fieldContext_ComponentInstance_service(ctx, field) - case "createdAt": - return ec.fieldContext_ComponentInstance_createdAt(ctx, field) - case "updatedAt": - return ec.fieldContext_ComponentInstance_updatedAt(ctx, field) + case "metadata": + return ec.fieldContext_ComponentInstance_metadata(ctx, field) } return nil, fmt.Errorf("no field named %q was found under type ComponentInstance", field.Name) }, @@ -13440,6 +13411,59 @@ func (ec *executionContext) fieldContext_ComponentVersion_componentInstances(ctx return fc, nil } +func (ec *executionContext) _ComponentVersion_metadata(ctx context.Context, field graphql.CollectedField, obj *model.ComponentVersion) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_ComponentVersion_metadata(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.Metadata, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + return graphql.Null + } + res := resTmp.(*model.Metadata) + fc.Result = res + return ec.marshalOMetadata2ᚖgithubᚗcomᚋcloudoperatorsᚋheurekaᚋinternalᚋapiᚋgraphqlᚋgraphᚋmodelᚐMetadata(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_ComponentVersion_metadata(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "ComponentVersion", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + switch field.Name { + case "created_at": + return ec.fieldContext_Metadata_created_at(ctx, field) + case "created_by": + return ec.fieldContext_Metadata_created_by(ctx, field) + case "deleted_at": + return ec.fieldContext_Metadata_deleted_at(ctx, field) + case "updated_at": + return ec.fieldContext_Metadata_updated_at(ctx, field) + case "updated_by": + return ec.fieldContext_Metadata_updated_by(ctx, field) + } + return nil, fmt.Errorf("no field named %q was found under type Metadata", field.Name) + }, + } + return fc, nil +} + func (ec *executionContext) _ComponentVersionConnection_totalCount(ctx context.Context, field graphql.CollectedField, obj *model.ComponentVersionConnection) (ret graphql.Marshaler) { fc, err := ec.fieldContext_ComponentVersionConnection_totalCount(ctx, field) if err != nil { @@ -13640,6 +13664,8 @@ func (ec *executionContext) fieldContext_ComponentVersionEdge_node(_ context.Con return ec.fieldContext_ComponentVersion_issues(ctx, field) case "componentInstances": return ec.fieldContext_ComponentVersion_componentInstances(ctx, field) + case "metadata": + return ec.fieldContext_ComponentVersion_metadata(ctx, field) } return nil, fmt.Errorf("no field named %q was found under type ComponentVersion", field.Name) }, @@ -16099,10 +16125,8 @@ func (ec *executionContext) fieldContext_IssueMatch_componentInstance(_ context. return ec.fieldContext_ComponentInstance_serviceId(ctx, field) case "service": return ec.fieldContext_ComponentInstance_service(ctx, field) - case "createdAt": - return ec.fieldContext_ComponentInstance_createdAt(ctx, field) - case "updatedAt": - return ec.fieldContext_ComponentInstance_updatedAt(ctx, field) + case "metadata": + return ec.fieldContext_ComponentInstance_metadata(ctx, field) } return nil, fmt.Errorf("no field named %q was found under type ComponentInstance", field.Name) }, @@ -20143,10 +20167,8 @@ func (ec *executionContext) fieldContext_Mutation_createComponentInstance(ctx co return ec.fieldContext_ComponentInstance_serviceId(ctx, field) case "service": return ec.fieldContext_ComponentInstance_service(ctx, field) - case "createdAt": - return ec.fieldContext_ComponentInstance_createdAt(ctx, field) - case "updatedAt": - return ec.fieldContext_ComponentInstance_updatedAt(ctx, field) + case "metadata": + return ec.fieldContext_ComponentInstance_metadata(ctx, field) } return nil, fmt.Errorf("no field named %q was found under type ComponentInstance", field.Name) }, @@ -20220,10 +20242,8 @@ func (ec *executionContext) fieldContext_Mutation_updateComponentInstance(ctx co return ec.fieldContext_ComponentInstance_serviceId(ctx, field) case "service": return ec.fieldContext_ComponentInstance_service(ctx, field) - case "createdAt": - return ec.fieldContext_ComponentInstance_createdAt(ctx, field) - case "updatedAt": - return ec.fieldContext_ComponentInstance_updatedAt(ctx, field) + case "metadata": + return ec.fieldContext_ComponentInstance_metadata(ctx, field) } return nil, fmt.Errorf("no field named %q was found under type ComponentInstance", field.Name) }, @@ -20348,6 +20368,8 @@ func (ec *executionContext) fieldContext_Mutation_createComponentVersion(ctx con return ec.fieldContext_ComponentVersion_issues(ctx, field) case "componentInstances": return ec.fieldContext_ComponentVersion_componentInstances(ctx, field) + case "metadata": + return ec.fieldContext_ComponentVersion_metadata(ctx, field) } return nil, fmt.Errorf("no field named %q was found under type ComponentVersion", field.Name) }, @@ -20417,6 +20439,8 @@ func (ec *executionContext) fieldContext_Mutation_updateComponentVersion(ctx con return ec.fieldContext_ComponentVersion_issues(ctx, field) case "componentInstances": return ec.fieldContext_ComponentVersion_componentInstances(ctx, field) + case "metadata": + return ec.fieldContext_ComponentVersion_metadata(ctx, field) } return nil, fmt.Errorf("no field named %q was found under type ComponentVersion", field.Name) }, @@ -31757,29 +31781,7 @@ func (ec *executionContext) _ComponentInstance(ctx context.Context, sel ast.Sele continue } out.Values[i] = ec._ComponentInstance_service(ctx, field, obj) - case "createdAt": - field := field - - if field.Deferrable != nil { - dfs, ok := deferred[field.Deferrable.Label] - di := 0 - if ok { - dfs.AddField(field) - di = len(dfs.Values) - 1 - } else { - dfs = graphql.NewFieldSet([]graphql.CollectedField{field}) - deferred[field.Deferrable.Label] = dfs - } - dfs.Concurrently(di, func(ctx context.Context) graphql.Marshaler { - return ec._ComponentInstance_createdAt(ctx, field, obj) - }) - - // don't run the out.Concurrently() call below - out.Values[i] = graphql.Null - continue - } - out.Values[i] = ec._ComponentInstance_createdAt(ctx, field, obj) - case "updatedAt": + case "metadata": field := field if field.Deferrable != nil { @@ -31793,14 +31795,14 @@ func (ec *executionContext) _ComponentInstance(ctx context.Context, sel ast.Sele deferred[field.Deferrable.Label] = dfs } dfs.Concurrently(di, func(ctx context.Context) graphql.Marshaler { - return ec._ComponentInstance_updatedAt(ctx, field, obj) + return ec._ComponentInstance_metadata(ctx, field, obj) }) // don't run the out.Concurrently() call below out.Values[i] = graphql.Null continue } - out.Values[i] = ec._ComponentInstance_updatedAt(ctx, field, obj) + out.Values[i] = ec._ComponentInstance_metadata(ctx, field, obj) default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -32157,6 +32159,28 @@ func (ec *executionContext) _ComponentVersion(ctx context.Context, sel ast.Selec continue } out.Values[i] = ec._ComponentVersion_componentInstances(ctx, field, obj) + case "metadata": + field := field + + if field.Deferrable != nil { + dfs, ok := deferred[field.Deferrable.Label] + di := 0 + if ok { + dfs.AddField(field) + di = len(dfs.Values) - 1 + } else { + dfs = graphql.NewFieldSet([]graphql.CollectedField{field}) + deferred[field.Deferrable.Label] = dfs + } + dfs.Concurrently(di, func(ctx context.Context) graphql.Marshaler { + return ec._ComponentVersion_metadata(ctx, field, obj) + }) + + // don't run the out.Concurrently() call below + out.Values[i] = graphql.Null + continue + } + out.Values[i] = ec._ComponentVersion_metadata(ctx, field, obj) default: panic("unknown field " + strconv.Quote(field.Name)) } diff --git a/internal/api/graphql/graph/model/models.go b/internal/api/graphql/graph/model/models.go index 9f3fa361..19d2984b 100644 --- a/internal/api/graphql/graph/model/models.go +++ b/internal/api/graphql/graph/model/models.go @@ -298,9 +298,9 @@ func NewIssueVariant(issueVariant *entity.IssueVariant) IssueVariant { func NewIssueVariantEdge(issueVariant *entity.IssueVariant) IssueVariantEdge { iv := NewIssueVariant(issueVariant) issueVariantEdge := IssueVariantEdge{ - Node: &iv, - Cursor: &iv.ID, - Metadata: getModelMetadata(issueVariant.Metadata), + Node: &iv, + Cursor: &iv.ID, + Metadata: getModelMetadata(issueVariant.Metadata), } return issueVariantEdge } @@ -441,6 +441,7 @@ func NewComponentVersion(componentVersion *entity.ComponentVersion) ComponentVer ID: fmt.Sprintf("%d", componentVersion.Id), Version: &componentVersion.Version, ComponentID: util.Ptr(fmt.Sprintf("%d", componentVersion.ComponentId)), + Metadata: getModelMetadata(componentVersion.Metadata), } } @@ -463,6 +464,7 @@ func NewComponentInstance(componentInstance *entity.ComponentInstance) Component Count: &count, ComponentVersionID: util.Ptr(fmt.Sprintf("%d", componentInstance.ComponentVersionId)), ServiceID: util.Ptr(fmt.Sprintf("%d", componentInstance.ServiceId)), + Metadata: getModelMetadata(componentInstance.Metadata), } } diff --git a/internal/api/graphql/graph/model/models_gen.go b/internal/api/graphql/graph/model/models_gen.go index 54a26178..58f219b1 100644 --- a/internal/api/graphql/graph/model/models_gen.go +++ b/internal/api/graphql/graph/model/models_gen.go @@ -161,8 +161,7 @@ type ComponentInstance struct { IssueMatches *IssueMatchConnection `json:"issueMatches,omitempty"` ServiceID *string `json:"serviceId,omitempty"` Service *Service `json:"service,omitempty"` - CreatedAt *string `json:"createdAt,omitempty"` - UpdatedAt *string `json:"updatedAt,omitempty"` + Metadata *Metadata `json:"metadata,omitempty"` } func (ComponentInstance) IsNode() {} @@ -205,6 +204,7 @@ type ComponentVersion struct { Component *Component `json:"component,omitempty"` Issues *IssueConnection `json:"issues,omitempty"` ComponentInstances *ComponentInstanceConnection `json:"componentInstances,omitempty"` + Metadata *Metadata `json:"metadata,omitempty"` } func (ComponentVersion) IsNode() {} diff --git a/internal/api/graphql/graph/queryCollection/componentInstance/directRelations.graphql b/internal/api/graphql/graph/queryCollection/componentInstance/directRelations.graphql index e7d97d74..ca024035 100644 --- a/internal/api/graphql/graph/queryCollection/componentInstance/directRelations.graphql +++ b/internal/api/graphql/graph/queryCollection/componentInstance/directRelations.graphql @@ -25,8 +25,10 @@ query ($filter: ComponentInstanceFilter, $first: Int, $after: String) { id name } - createdAt - updatedAt + metadata { + created_at + updated_at + } issueMatches { totalCount edges { @@ -57,4 +59,4 @@ query ($filter: ComponentInstanceFilter, $first: Int, $after: String) { } } } -} \ No newline at end of file +} diff --git a/internal/api/graphql/graph/queryCollection/issue/directRelations.graphql b/internal/api/graphql/graph/queryCollection/issue/directRelations.graphql index 20b394fe..2fbfbcbe 100644 --- a/internal/api/graphql/graph/queryCollection/issue/directRelations.graphql +++ b/internal/api/graphql/graph/queryCollection/issue/directRelations.graphql @@ -67,8 +67,10 @@ query ($filter: IssueFilter, $first: Int, $after: String) { componentInstance { id count - createdAt - updatedAt + metadata { + created_at + updated_at + } ccrn } } diff --git a/internal/api/graphql/graph/schema/component_instance.graphqls b/internal/api/graphql/graph/schema/component_instance.graphqls index 9342c93e..03fa4ba8 100644 --- a/internal/api/graphql/graph/schema/component_instance.graphqls +++ b/internal/api/graphql/graph/schema/component_instance.graphqls @@ -10,8 +10,7 @@ type ComponentInstance implements Node { issueMatches(filter: IssueMatchFilter, first: Int, after: String): IssueMatchConnection serviceId: String service: Service - createdAt: DateTime - updatedAt: DateTime + metadata: Metadata } input ComponentInstanceInput { @@ -34,4 +33,4 @@ type ComponentInstanceEdge implements Edge { input ComponentInstanceFilter { issueMatchId: [String], -} \ No newline at end of file +} diff --git a/internal/api/graphql/graph/schema/component_version.graphqls b/internal/api/graphql/graph/schema/component_version.graphqls index cebb4e36..573e6ddf 100644 --- a/internal/api/graphql/graph/schema/component_version.graphqls +++ b/internal/api/graphql/graph/schema/component_version.graphqls @@ -8,6 +8,7 @@ type ComponentVersion implements Node { component: Component issues(first: Int, after: String): IssueConnection componentInstances(first: Int, after: String): ComponentInstanceConnection + metadata: Metadata } input ComponentVersionInput { @@ -29,4 +30,4 @@ type ComponentVersionEdge implements Edge { input ComponentVersionFilter { issueId: [String], version: [String], -} \ No newline at end of file +} diff --git a/internal/app/component_instance/component_instance_handler.go b/internal/app/component_instance/component_instance_handler.go index b03cbd99..fda1efd1 100644 --- a/internal/app/component_instance/component_instance_handler.go +++ b/internal/app/component_instance/component_instance_handler.go @@ -111,6 +111,7 @@ func (ci *componentInstanceHandler) ListComponentInstances(filter *entity.Compon } func (ci *componentInstanceHandler) CreateComponentInstance(componentInstance *entity.ComponentInstance) (*entity.ComponentInstance, error) { + componentInstance.CreatedBy = "Creator" l := logrus.WithFields(logrus.Fields{ "event": CreateComponentInstanceEventName, "object": componentInstance, @@ -131,6 +132,7 @@ func (ci *componentInstanceHandler) CreateComponentInstance(componentInstance *e } func (ci *componentInstanceHandler) UpdateComponentInstance(componentInstance *entity.ComponentInstance) (*entity.ComponentInstance, error) { + componentInstance.UpdatedBy = "Updater" l := logrus.WithFields(logrus.Fields{ "event": UpdateComponentInstanceEventName, "object": componentInstance, diff --git a/internal/app/component_version/component_version_handler.go b/internal/app/component_version/component_version_handler.go index 39d73dac..ea42bb2b 100644 --- a/internal/app/component_version/component_version_handler.go +++ b/internal/app/component_version/component_version_handler.go @@ -109,6 +109,7 @@ func (cv *componentVersionHandler) ListComponentVersions(filter *entity.Componen } func (cv *componentVersionHandler) CreateComponentVersion(componentVersion *entity.ComponentVersion) (*entity.ComponentVersion, error) { + componentVersion.CreatedBy = "Creator" l := logrus.WithFields(logrus.Fields{ "event": CreateComponentVersionEventName, "object": componentVersion, @@ -129,6 +130,7 @@ func (cv *componentVersionHandler) CreateComponentVersion(componentVersion *enti } func (cv *componentVersionHandler) UpdateComponentVersion(componentVersion *entity.ComponentVersion) (*entity.ComponentVersion, error) { + componentVersion.UpdatedBy = "Updater" l := logrus.WithFields(logrus.Fields{ "event": UpdateComponentVersionEventName, "object": componentVersion, diff --git a/internal/database/mariadb/component_instance.go b/internal/database/mariadb/component_instance.go index 3556e68e..89535dc9 100644 --- a/internal/database/mariadb/component_instance.go +++ b/internal/database/mariadb/component_instance.go @@ -69,6 +69,10 @@ func (s *SqlDatabase) getComponentInstanceUpdateFields(componentInstance *entity if componentInstance.ServiceId != 0 { fl = append(fl, "componentinstance_service_id = :componentinstance_service_id") } + if componentInstance.UpdatedBy != "" { + fl = append(fl, "componentinstance_updated_by = :componentinstance_updated_by") + } + return strings.Join(fl, ", ") } @@ -208,12 +212,14 @@ func (s *SqlDatabase) CreateComponentInstance(componentInstance *entity.Componen componentinstance_ccrn, componentinstance_count, componentinstance_component_version_id, - componentinstance_service_id + componentinstance_service_id, + componentinstance_created_by ) VALUES ( :componentinstance_ccrn, :componentinstance_count, :componentinstance_component_version_id, - :componentinstance_service_id + :componentinstance_service_id, + :componentinstance_created_by ) ` diff --git a/internal/database/mariadb/component_version.go b/internal/database/mariadb/component_version.go index 1644431a..f9d38d33 100644 --- a/internal/database/mariadb/component_version.go +++ b/internal/database/mariadb/component_version.go @@ -51,6 +51,9 @@ func (s *SqlDatabase) getComponentVersionUpdateFields(componentVersion *entity.C if componentVersion.ComponentId != 0 { fl = append(fl, "componentversion_component_id = :componentversion_component_id") } + if componentVersion.UpdatedBy != "" { + fl = append(fl, "componentversion_updated_by = :componentversion_updated_by") + } return strings.Join(fl, ", ") } @@ -201,10 +204,12 @@ func (s *SqlDatabase) CreateComponentVersion(componentVersion *entity.ComponentV query := ` INSERT INTO ComponentVersion ( componentversion_component_id, - componentversion_version + componentversion_version, + componentversion_created_by ) VALUES ( :componentversion_component_id, - :componentversion_version + :componentversion_version, + :componentversion_created_by ) ` diff --git a/internal/database/mariadb/entity.go b/internal/database/mariadb/entity.go index d9f07e09..d85f7d6b 100644 --- a/internal/database/mariadb/entity.go +++ b/internal/database/mariadb/entity.go @@ -446,8 +446,10 @@ type ComponentVersionRow struct { Version sql.NullString `db:"componentversion_version" json:"version"` ComponentId sql.NullInt64 `db:"componentversion_component_id"` CreatedAt sql.NullTime `db:"componentversion_created_at" json:"created_at"` + CreatedBy sql.NullString `db:"componentversion_created_by" json:"created_by"` DeletedAt sql.NullTime `db:"componentversion_deleted_at" json:"deleted_at,omitempty"` UpdatedAt sql.NullTime `db:"componentversion_updated_at" json:"updated_at"` + UpdatedBy sql.NullString `db:"componentversion_updated_by" json:"updated_by"` } func (cvr *ComponentVersionRow) AsComponentVersion() entity.ComponentVersion { @@ -457,8 +459,10 @@ func (cvr *ComponentVersionRow) AsComponentVersion() entity.ComponentVersion { ComponentId: GetInt64Value(cvr.ComponentId), Metadata: entity.Metadata{ CreatedAt: GetTimeValue(cvr.CreatedAt), + CreatedBy: GetStringValue(cvr.CreatedBy), DeletedAt: GetTimeValue(cvr.DeletedAt), UpdatedAt: GetTimeValue(cvr.UpdatedAt), + UpdatedBy: GetStringValue(cvr.UpdatedBy), }, } } @@ -468,8 +472,10 @@ func (cvr *ComponentVersionRow) FromComponentVersion(cv *entity.ComponentVersion cvr.Version = sql.NullString{String: cv.Version, Valid: true} cvr.ComponentId = sql.NullInt64{Int64: cv.ComponentId, Valid: true} cvr.CreatedAt = sql.NullTime{Time: cv.CreatedAt, Valid: true} + cvr.CreatedBy = sql.NullString{String: cv.CreatedBy, Valid: true} cvr.DeletedAt = sql.NullTime{Time: cv.DeletedAt, Valid: true} cvr.UpdatedAt = sql.NullTime{Time: cv.UpdatedAt, Valid: true} + cvr.UpdatedBy = sql.NullString{String: cv.UpdatedBy, Valid: true} } type SupportGroupRow struct { @@ -605,8 +611,10 @@ type ComponentInstanceRow struct { ComponentVersionId sql.NullInt64 `db:"componentinstance_component_version_id"` ServiceId sql.NullInt64 `db:"componentinstance_service_id"` CreatedAt sql.NullTime `db:"componentinstance_created_at" json:"created_at"` + CreatedBy sql.NullString `db:"componentinstance_created_by" json:"created_by"` DeletedAt sql.NullTime `db:"componentinstance_deleted_at" json:"deleted_at,omitempty"` UpdatedAt sql.NullTime `db:"componentinstance_updated_at" json:"updated_at"` + UpdatedBy sql.NullString `db:"componentinstance_updated_by" json:"udpated_by"` } func (cir *ComponentInstanceRow) AsComponentInstance() entity.ComponentInstance { @@ -620,8 +628,10 @@ func (cir *ComponentInstanceRow) AsComponentInstance() entity.ComponentInstance ServiceId: GetInt64Value(cir.ServiceId), Metadata: entity.Metadata{ CreatedAt: GetTimeValue(cir.CreatedAt), + CreatedBy: GetStringValue(cir.CreatedBy), DeletedAt: GetTimeValue(cir.DeletedAt), UpdatedAt: GetTimeValue(cir.UpdatedAt), + UpdatedBy: GetStringValue(cir.UpdatedBy), }, } } @@ -633,8 +643,10 @@ func (cir *ComponentInstanceRow) FromComponentInstance(ci *entity.ComponentInsta cir.ComponentVersionId = sql.NullInt64{Int64: ci.ComponentVersionId, Valid: true} cir.ServiceId = sql.NullInt64{Int64: ci.ServiceId, Valid: true} cir.CreatedAt = sql.NullTime{Time: ci.CreatedAt, Valid: true} + cir.CreatedBy = sql.NullString{String: ci.CreatedBy, Valid: true} cir.DeletedAt = sql.NullTime{Time: ci.DeletedAt, Valid: true} cir.UpdatedAt = sql.NullTime{Time: ci.UpdatedAt, Valid: true} + cir.UpdatedBy = sql.NullString{String: ci.UpdatedBy, Valid: true} } type UserRow struct { diff --git a/internal/database/mariadb/init/schema.sql b/internal/database/mariadb/init/schema.sql index cdb279d6..e11511d8 100644 --- a/internal/database/mariadb/init/schema.sql +++ b/internal/database/mariadb/init/schema.sql @@ -29,8 +29,10 @@ create table if not exists ComponentVersion componentversion_version varchar(256) not null, componentversion_component_id int unsigned not null, componentversion_created_at timestamp default current_timestamp() not null, + componentversion_created_by varchar(256) null, componentversion_deleted_at timestamp null, componentversion_updated_at timestamp default current_timestamp() not null on update current_timestamp(), + componentversion_updated_by varchar(256) null, constraint id_UNIQUE unique (componentversion_id), constraint version_component_unique @@ -108,8 +110,10 @@ create table if not exists ComponentInstance componentinstance_component_version_id int unsigned not null, componentinstance_service_id int unsigned not null, componentinstance_created_at timestamp default current_timestamp() not null, + componentinstance_created_by varchar(256) null, componentinstance_deleted_at timestamp null, componentinstance_updated_at timestamp default current_timestamp() not null on update current_timestamp(), + componentinstance_updated_by varchar(256) null, constraint id_UNIQUE unique (componentinstance_id), constraint name_service_unique