Skip to content

Commit

Permalink
Enforce central entity ID in EEA and rule eval tables (#4220)
Browse files Browse the repository at this point in the history
These were nullable as we were introducing them into minder. Now that
we've ensured they're everywhere, we can enforce them as not null.

Signed-off-by: Juan Antonio Osorio <[email protected]>
  • Loading branch information
JAORMX authored Aug 21, 2024
1 parent 7a0da06 commit 2ab6721
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 59 deletions.
23 changes: 23 additions & 0 deletions database/migrations/000097_entity_instance_not_null.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- Copyright 2024 Stacklok, Inc
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

BEGIN;

-- Make entity_instance_id nullable in entity_execution_lock, flush_cache and evaluation_rule_entities.

ALTER TABLE entity_execution_lock ALTER COLUMN entity_instance_id DROP NOT NULL;
ALTER TABLE flush_cache ALTER COLUMN entity_instance_id DROP NOT NULL;
ALTER TABLE evaluation_rule_entities ALTER COLUMN entity_instance_id DROP NOT NULL;

COMMIT;
24 changes: 24 additions & 0 deletions database/migrations/000097_entity_instance_not_null.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- Copyright 2024 Stacklok, Inc
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

BEGIN;

-- Make entity_instance_id not nullable in entity_execution_lock, flush_cache and evaluation_rule_entities.
-- Note that at this point it is verified that the entity_instance_id is not null in all these tables.

ALTER TABLE entity_execution_lock ALTER COLUMN entity_instance_id SET NOT NULL;
ALTER TABLE flush_cache ALTER COLUMN entity_instance_id SET NOT NULL;
ALTER TABLE evaluation_rule_entities ALTER COLUMN entity_instance_id SET NOT NULL;

COMMIT;
6 changes: 3 additions & 3 deletions database/query/entity_execution_lock.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ INSERT INTO entity_execution_lock(
sqlc.narg(artifact_id)::UUID,
sqlc.narg(pull_request_id)::UUID,
sqlc.arg(project_id)::UUID,
sqlc.narg(entity_instance_id)::UUID
sqlc.arg(entity_instance_id)::UUID
) ON CONFLICT(entity, COALESCE(repository_id, '00000000-0000-0000-0000-000000000000'::UUID), COALESCE(artifact_id, '00000000-0000-0000-0000-000000000000'::UUID), COALESCE(pull_request_id, '00000000-0000-0000-0000-000000000000'::UUID))
DO UPDATE SET
locked_by = gen_random_uuid(),
last_lock_time = NOW(),
entity_instance_id = sqlc.narg(entity_instance_id)::UUID
entity_instance_id = sqlc.arg(entity_instance_id)::UUID
WHERE entity_execution_lock.last_lock_time < (NOW() - (@interval::TEXT || ' seconds')::interval)
RETURNING *;

Expand Down Expand Up @@ -65,7 +65,7 @@ INSERT INTO flush_cache(
sqlc.narg(artifact_id)::UUID,
sqlc.narg(pull_request_id)::UUID,
sqlc.arg(project_id)::UUID,
sqlc.narg(entity_instance_id)::UUID
sqlc.arg(entity_instance_id)::UUID
) ON CONFLICT(entity, COALESCE(repository_id, '00000000-0000-0000-0000-000000000000'::UUID), COALESCE(artifact_id, '00000000-0000-0000-0000-000000000000'::UUID), COALESCE(pull_request_id, '00000000-0000-0000-0000-000000000000'::UUID))
DO NOTHING
RETURNING *;
Expand Down
4 changes: 2 additions & 2 deletions internal/db/entity_execution_lock.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 19 additions & 16 deletions internal/db/entity_execution_lock.sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ func TestQueries_LockIfThresholdNotExceeded(t *testing.T) {
go func() {
defer wg.Done()
_, err := testQueries.LockIfThresholdNotExceeded(context.Background(), LockIfThresholdNotExceededParams{
Entity: EntitiesRepository,
RepositoryID: uuid.NullUUID{UUID: repo.ID, Valid: true},
ArtifactID: uuid.NullUUID{},
PullRequestID: uuid.NullUUID{},
Interval: fmt.Sprintf("%d", threshold),
ProjectID: project.ID,
Entity: EntitiesRepository,
RepositoryID: uuid.NullUUID{UUID: repo.ID, Valid: true},
ArtifactID: uuid.NullUUID{},
PullRequestID: uuid.NullUUID{},
Interval: fmt.Sprintf("%d", threshold),
ProjectID: project.ID,
EntityInstanceID: repo.ID,
})

if err != nil && errors.Is(err, sql.ErrNoRows) {
Expand All @@ -64,11 +65,12 @@ func TestQueries_LockIfThresholdNotExceeded(t *testing.T) {
queueCount.Add(1)

_, err := testQueries.EnqueueFlush(context.Background(), EnqueueFlushParams{
Entity: EntitiesRepository,
RepositoryID: uuid.NullUUID{UUID: repo.ID, Valid: true},
ArtifactID: uuid.NullUUID{},
PullRequestID: uuid.NullUUID{},
ProjectID: project.ID,
Entity: EntitiesRepository,
RepositoryID: uuid.NullUUID{UUID: repo.ID, Valid: true},
ArtifactID: uuid.NullUUID{},
PullRequestID: uuid.NullUUID{},
ProjectID: project.ID,
EntityInstanceID: repo.ID,
})
if err == nil {
effectiveFlush.Add(1)
Expand All @@ -90,11 +92,12 @@ func TestQueries_LockIfThresholdNotExceeded(t *testing.T) {
t.Log("Attempting to acquire lock now that threshold has passed")

_, err := testQueries.LockIfThresholdNotExceeded(context.Background(), LockIfThresholdNotExceededParams{
Entity: EntitiesRepository,
RepositoryID: uuid.NullUUID{UUID: repo.ID, Valid: true},
ArtifactID: uuid.NullUUID{},
PullRequestID: uuid.NullUUID{},
Interval: fmt.Sprintf("%d", threshold),
Entity: EntitiesRepository,
RepositoryID: uuid.NullUUID{UUID: repo.ID, Valid: true},
ArtifactID: uuid.NullUUID{},
PullRequestID: uuid.NullUUID{},
Interval: fmt.Sprintf("%d", threshold),
EntityInstanceID: repo.ID,
})

assert.NoError(t, err, "expected no error")
Expand Down
18 changes: 9 additions & 9 deletions internal/db/eval_history.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions internal/db/eval_history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,11 +718,8 @@ func createRandomEvaluationRuleEntity(
UUID: entityID,
Valid: true,
},
EntityType: EntitiesRepository,
EntityInstanceID: uuid.NullUUID{
UUID: entityID,
Valid: true,
},
EntityType: EntitiesRepository,
EntityInstanceID: entityID,
},
)
require.NoError(t, err)
Expand Down
6 changes: 3 additions & 3 deletions internal/db/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 4 additions & 7 deletions internal/db/profiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,10 @@ func createRuleEntity(
UUID: repoID,
Valid: true,
},
PullRequestID: uuid.NullUUID{},
ArtifactID: uuid.NullUUID{},
EntityType: EntitiesRepository,
EntityInstanceID: uuid.NullUUID{
UUID: repoID,
Valid: true,
},
PullRequestID: uuid.NullUUID{},
ArtifactID: uuid.NullUUID{},
EntityType: EntitiesRepository,
EntityInstanceID: repoID,
},
)
require.NoError(t, err)
Expand Down
8 changes: 1 addition & 7 deletions internal/eea/eea.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,9 @@ func (e *EEA) aggregate(msg *message.Message) (*message.Message, error) {
Str("entity", inf.Type.ToString()).
Logger()

var entityID uuid.NullUUID
eID, err := inf.GetID()
entityID, err := inf.GetID()
if err != nil {
logger.Debug().AnErr("error getting entity ID", err).Msgf("Entity ID was not set for event %s", inf.Type)
} else {
entityID = uuid.NullUUID{
UUID: eID,
Valid: true,
}
}

// We need to check that the resources still exist before attempting to lock them.
Expand Down
14 changes: 9 additions & 5 deletions internal/eea/eea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,11 @@ func TestFlushAll(t *testing.T) {
mockStore.EXPECT().ListFlushCache(ctx).
Return([]db.FlushCache{
{
ID: uuid.New(),
Entity: db.EntitiesRepository,
RepositoryID: uuid.NullUUID{UUID: repoID, Valid: true},
QueuedAt: time.Now(),
ID: uuid.New(),
Entity: db.EntitiesRepository,
RepositoryID: uuid.NullUUID{UUID: repoID, Valid: true},
QueuedAt: time.Now(),
EntityInstanceID: repoID,
},
}, nil)

Expand Down Expand Up @@ -259,7 +260,8 @@ func TestFlushAll(t *testing.T) {
UUID: artID,
Valid: true,
},
QueuedAt: time.Now(),
EntityInstanceID: artID,
QueuedAt: time.Now(),
},
}, nil)

Expand Down Expand Up @@ -318,6 +320,7 @@ func TestFlushAll(t *testing.T) {
UUID: projectID,
Valid: true,
},
EntityInstanceID: artID,
},
}, nil)

Expand Down Expand Up @@ -367,6 +370,7 @@ func TestFlushAll(t *testing.T) {
UUID: projectID,
Valid: true,
},
EntityInstanceID: artID,
},
}, nil)

Expand Down
4 changes: 2 additions & 2 deletions internal/history/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func paramsFromEntity(
Valid: true,
}

params.EntityID = nullableEntityID
params.EntityID = entityID

switch entityType {
case db.EntitiesRepository:
Expand All @@ -193,7 +193,7 @@ type ruleEntityParams struct {
PullRequestID uuid.NullUUID
// Is the target entity ID. We'll be replacing the single-entity IDs
// with this one.
EntityID uuid.NullUUID
EntityID uuid.UUID
}

func (_ *evaluationHistoryService) ListEvaluationHistory(
Expand Down

0 comments on commit 2ab6721

Please sign in to comment.