Skip to content

Commit

Permalink
Format SQL;
Browse files Browse the repository at this point in the history
  • Loading branch information
ycliuhw committed May 15, 2024
1 parent a5a28ea commit d6cf27d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
4 changes: 2 additions & 2 deletions domain/secret/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,10 @@ func (s *SecretService) UpdateCharmSecret(ctx context.Context, uri *secrets.URI,
p.RotatePolicy = &rotatePolicy
if params.RotatePolicy.WillRotate() {
policy, err := s.st.GetRotatePolicy(ctx, uri)
if err != nil && !errors.Is(err, secreterrors.SecretNotFound) {
if err != nil {
return errors.Trace(err)
}
if errors.Is(err, secreterrors.SecretNotFound) || !policy.WillRotate() {
if !policy.WillRotate() {
p.NextRotateTime = params.RotatePolicy.NextRotateTime(s.clock.Now())
}
}
Expand Down
2 changes: 1 addition & 1 deletion domain/secret/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ func (s *serviceSuite) TestUpdateCharmSecret(c *gc.C) {
}).Return("manage", nil)
s.state.EXPECT().GetRotatePolicy(gomock.Any(), uri).Return(
coresecrets.RotateNever, // No rotate policy.
secreterrors.SecretNotFound)
nil)
s.state.EXPECT().UpdateSecret(gomock.Any(), uri, gomock.Any()).DoAndReturn(func(_ context.Context, _ *coresecrets.URI, got domainsecret.UpsertSecretParams) error {
c.Assert(got.NextRotateTime, gc.NotNil)
c.Assert(*got.NextRotateTime, jc.Almost, *p.NextRotateTime)
Expand Down
32 changes: 17 additions & 15 deletions domain/secret/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1058,29 +1058,31 @@ func (st State) GetRotationExpiryInfo(ctx context.Context, uri *coresecrets.URI)
if err != nil {
return nil, errors.Trace(err)
}

input := secretID{ID: uri.ID}
result := secretInfo{}
stmt, err := st.Prepare(`
WITH rev AS
(SELECT uuid,MAX(revision) AS latest_revision
WITH rev AS (
SELECT uuid, MAX(revision) AS latest_revision
FROM secret_revision
WHERE secret_id = $secretID.id)
SELECT sp.policy AS &secretInfo.policy,
sro.next_rotation_time AS &secretInfo.next_rotation_time,
sre.expire_time AS &secretInfo.latest_expire_time,
rev.latest_revision AS &secretInfo.latest_revision
WHERE secret_id = $secretID.id
)
SELECT
sp.policy AS &secretInfo.policy,
sro.next_rotation_time AS &secretInfo.next_rotation_time,
sre.expire_time AS &secretInfo.latest_expire_time,
rev.latest_revision AS &secretInfo.latest_revision
FROM secret_metadata sm, rev
INNER JOIN secret_rotate_policy sp ON sp.id = sm.rotate_policy_id
LEFT JOIN secret_rotation sro ON sro.secret_id = sm.secret_id
LEFT JOIN secret_revision_expire sre ON sre.revision_uuid = rev.uuid
WHERE sm.secret_id = $secretID.id`, secretID{}, secretInfo{})
JOIN secret_rotate_policy sp ON sp.id = sm.rotate_policy_id
LEFT JOIN secret_rotation sro ON sro.secret_id = sm.secret_id
LEFT JOIN secret_revision_expire sre ON sre.revision_uuid = rev.uuid
WHERE sm.secret_id = $secretID.id`, input, result)

if err != nil {
return nil, errors.Trace(err)
}

var result secretInfo
if err := db.Txn(ctx, func(ctx context.Context, tx *sqlair.TX) error {
err := tx.Query(ctx, stmt, secretID{ID: uri.ID}).Get(&result)
err := tx.Query(ctx, stmt, input).Get(&result)
if errors.Is(err, sqlair.ErrNoRows) {
return fmt.Errorf("secret %q not found%w", uri, errors.Hide(secreterrors.SecretNotFound))
}
Expand Down Expand Up @@ -1110,7 +1112,7 @@ func (st State) GetRotatePolicy(ctx context.Context, uri *coresecrets.URI) (core
stmt, err := st.Prepare(`
SELECT srp.policy AS &secretInfo.policy
FROM secret_metadata sm
INNER JOIN secret_rotate_policy srp ON srp.id = sm.rotate_policy_id
JOIN secret_rotate_policy srp ON srp.id = sm.rotate_policy_id
WHERE sm.secret_id = $secretID.id`, secretID{}, secretInfo{})
if err != nil {
return coresecrets.RotateNever, errors.Trace(err)
Expand Down

0 comments on commit d6cf27d

Please sign in to comment.