Simplify policy update mechanism
Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
|
|||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- Simplified policy data model by removing version field and optimistic concurrency
|
||||||
|
- Refactored policy update flow to load-modify-save pattern
|
||||||
|
|
||||||
## [0.4.1] - 2025-04-09
|
## [0.4.1] - 2025-04-09
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|||||||
@@ -23,19 +23,9 @@ type (
|
|||||||
ReviewDate *time.Time `db:"review_date"`
|
ReviewDate *time.Time `db:"review_date"`
|
||||||
CreatedAt time.Time `db:"created_at"`
|
CreatedAt time.Time `db:"created_at"`
|
||||||
UpdatedAt time.Time `db:"updated_at"`
|
UpdatedAt time.Time `db:"updated_at"`
|
||||||
Version int `db:"version"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Policies []*Policy
|
Policies []*Policy
|
||||||
|
|
||||||
UpdatePolicyParams struct {
|
|
||||||
ExpectedVersion int
|
|
||||||
Name *string
|
|
||||||
Content *string
|
|
||||||
Status *PolicyStatus
|
|
||||||
ReviewDate **time.Time
|
|
||||||
OwnerID *gid.GID
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (p Policy) CursorKey(orderBy PolicyOrderField) page.CursorKey {
|
func (p Policy) CursorKey(orderBy PolicyOrderField) page.CursorKey {
|
||||||
@@ -65,8 +55,7 @@ SELECT
|
|||||||
content,
|
content,
|
||||||
review_date,
|
review_date,
|
||||||
created_at,
|
created_at,
|
||||||
updated_at,
|
updated_at
|
||||||
version
|
|
||||||
FROM
|
FROM
|
||||||
policies
|
policies
|
||||||
WHERE
|
WHERE
|
||||||
@@ -112,8 +101,7 @@ SELECT
|
|||||||
content,
|
content,
|
||||||
review_date,
|
review_date,
|
||||||
created_at,
|
created_at,
|
||||||
updated_at,
|
updated_at
|
||||||
version
|
|
||||||
FROM
|
FROM
|
||||||
policies
|
policies
|
||||||
WHERE
|
WHERE
|
||||||
@@ -160,8 +148,7 @@ INSERT INTO
|
|||||||
content,
|
content,
|
||||||
review_date,
|
review_date,
|
||||||
created_at,
|
created_at,
|
||||||
updated_at,
|
updated_at
|
||||||
version
|
|
||||||
)
|
)
|
||||||
VALUES (
|
VALUES (
|
||||||
@tenant_id,
|
@tenant_id,
|
||||||
@@ -173,8 +160,7 @@ VALUES (
|
|||||||
@content,
|
@content,
|
||||||
@review_date,
|
@review_date,
|
||||||
@created_at,
|
@created_at,
|
||||||
@updated_at,
|
@updated_at
|
||||||
@version
|
|
||||||
);
|
);
|
||||||
`
|
`
|
||||||
|
|
||||||
@@ -189,7 +175,6 @@ VALUES (
|
|||||||
"review_date": p.ReviewDate,
|
"review_date": p.ReviewDate,
|
||||||
"created_at": p.CreatedAt,
|
"created_at": p.CreatedAt,
|
||||||
"updated_at": p.UpdatedAt,
|
"updated_at": p.UpdatedAt,
|
||||||
"version": p.Version,
|
|
||||||
}
|
}
|
||||||
_, err := conn.Exec(ctx, q, args)
|
_, err := conn.Exec(ctx, q, args)
|
||||||
return err
|
return err
|
||||||
@@ -217,58 +202,38 @@ func (p *Policy) Update(
|
|||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
conn pg.Conn,
|
conn pg.Conn,
|
||||||
scope Scoper,
|
scope Scoper,
|
||||||
params UpdatePolicyParams,
|
|
||||||
) error {
|
) error {
|
||||||
q := `
|
q := `
|
||||||
UPDATE policies SET
|
UPDATE
|
||||||
name = COALESCE(@name, name),
|
policies
|
||||||
status = COALESCE(@status, status),
|
SET
|
||||||
content = COALESCE(@content, content),
|
name = @name,
|
||||||
review_date = COALESCE(@review_date, review_date),
|
status = @status,
|
||||||
owner_id = COALESCE(@owner_id, owner_id),
|
content = @content,
|
||||||
updated_at = @updated_at,
|
review_date = @review_date,
|
||||||
version = version + 1
|
owner_id = @owner_id,
|
||||||
|
updated_at = @updated_at
|
||||||
WHERE %s
|
WHERE %s
|
||||||
AND id = @policy_id
|
AND id = @policy_id
|
||||||
AND version = @expected_version
|
|
||||||
RETURNING
|
|
||||||
id,
|
|
||||||
organization_id,
|
|
||||||
owner_id,
|
|
||||||
name,
|
|
||||||
content,
|
|
||||||
review_date,
|
|
||||||
created_at,
|
|
||||||
updated_at,
|
|
||||||
status,
|
|
||||||
version
|
|
||||||
`
|
`
|
||||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||||
|
|
||||||
args := pgx.StrictNamedArgs{
|
args := pgx.StrictNamedArgs{
|
||||||
"policy_id": p.ID,
|
"policy_id": p.ID,
|
||||||
"expected_version": params.ExpectedVersion,
|
|
||||||
"updated_at": time.Now(),
|
"updated_at": time.Now(),
|
||||||
"name": params.Name,
|
"name": p.Name,
|
||||||
"content": params.Content,
|
"content": p.Content,
|
||||||
"status": params.Status,
|
"status": p.Status,
|
||||||
"review_date": params.ReviewDate,
|
"review_date": p.ReviewDate,
|
||||||
"owner_id": params.OwnerID,
|
"owner_id": p.OwnerID,
|
||||||
}
|
}
|
||||||
maps.Copy(args, scope.SQLArguments())
|
maps.Copy(args, scope.SQLArguments())
|
||||||
|
|
||||||
rows, err := conn.Query(ctx, q, args)
|
_, err := conn.Exec(ctx, q, args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("cannot query policies: %w", err)
|
return fmt.Errorf("cannot update policy: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
policy, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[Policy])
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("cannot collect policy: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
*p = policy
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -105,22 +105,42 @@ func (s *PolicyService) Update(
|
|||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
req UpdatePolicyRequest,
|
req UpdatePolicyRequest,
|
||||||
) (*coredata.Policy, error) {
|
) (*coredata.Policy, error) {
|
||||||
params := coredata.UpdatePolicyParams{
|
policy := &coredata.Policy{}
|
||||||
ExpectedVersion: req.ExpectedVersion,
|
|
||||||
Name: req.Name,
|
|
||||||
Content: req.Content,
|
|
||||||
Status: req.Status,
|
|
||||||
ReviewDate: &req.ReviewDate,
|
|
||||||
OwnerID: req.OwnerID,
|
|
||||||
}
|
|
||||||
|
|
||||||
policy := &coredata.Policy{ID: req.ID}
|
|
||||||
|
|
||||||
err := s.svc.pg.WithTx(
|
err := s.svc.pg.WithTx(
|
||||||
ctx,
|
ctx,
|
||||||
func(conn pg.Conn) error {
|
func(conn pg.Conn) error {
|
||||||
return policy.Update(ctx, conn, s.svc.scope, params)
|
if err := policy.LoadByID(ctx, conn, s.svc.scope, req.ID); err != nil {
|
||||||
})
|
return fmt.Errorf("cannot load policy %q: %w", req.ID, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.Name != nil {
|
||||||
|
policy.Name = *req.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.Content != nil {
|
||||||
|
policy.Content = *req.Content
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.Status != nil {
|
||||||
|
policy.Status = *req.Status
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.ReviewDate != nil {
|
||||||
|
policy.ReviewDate = req.ReviewDate
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.OwnerID != nil {
|
||||||
|
policy.OwnerID = *req.OwnerID
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := policy.Update(ctx, conn, s.svc.scope); err != nil {
|
||||||
|
return fmt.Errorf("cannot update policy: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user