Skip to content

Commit

Permalink
Add reference to central entities table in new rule eval history tabl…
Browse files Browse the repository at this point in the history
…es (#4204)

This starts populating the relevant data so we can migrate to using the
central table in a further increment.

Signed-off-by: Juan Antonio Osorio <[email protected]>
  • Loading branch information
JAORMX authored Aug 20, 2024
1 parent d0952b8 commit 4c18b4d
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 28 deletions.
2 changes: 1 addition & 1 deletion cmd/server/app/history_purge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestRecordSize(t *testing.T) {
},
)

require.Equal(t, 80, int(size))
require.Equal(t, 96, int(size))
}

func TestPurgeLoop(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions cmd/server/app/migrate_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ var upCmd = &cobra.Command{
cmd.Printf("Error while populating entities table with pull requests: %v\n", err)
}

if err := store.TemporaryPopulateEvaluationHistory(ctx); err != nil {
cmd.Printf("Error while populating entities table with evaluation history: %v\n", err)
}

return nil
},
}
Expand Down
21 changes: 21 additions & 0 deletions database/migrations/000095_eval_central_entities.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- 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;

-- Drop optional FK towards entity_instances(id) in evaluation_rule_entities

ALTER TABLE evaluation_rule_entities DROP COLUMN entity_instance_id;

COMMIT;
22 changes: 22 additions & 0 deletions database/migrations/000095_eval_central_entities.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- 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;

-- Add optional FK towards entity_instances(id) in evaluation_rule_entities

ALTER TABLE evaluation_rule_entities ADD COLUMN entity_instance_id UUID;
ALTER TABLE evaluation_rule_entities ADD CONSTRAINT fk_entity_instance_id FOREIGN KEY (entity_instance_id) REFERENCES entity_instances(id) ON DELETE CASCADE;

COMMIT;
14 changes: 14 additions & 0 deletions database/mock/store.go

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

28 changes: 25 additions & 3 deletions database/query/eval_history.sql
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ INSERT INTO evaluation_rule_entities(
repository_id,
pull_request_id,
artifact_id,
entity_type
entity_type,
entity_instance_id
) VALUES (
$1,
$2,
$3,
$4,
$5
$5,
$6
)
RETURNING id;

Expand Down Expand Up @@ -105,6 +107,7 @@ SELECT s.id::uuid AS evaluation_id,
WHEN ere.artifact_id IS NOT NULL THEN a.id
END AS uuid
) AS entity_id,
ere.entity_instance_id as new_entity_id,
-- raw fields for entity names
r.repo_owner,
r.repo_name,
Expand Down Expand Up @@ -150,6 +153,7 @@ SELECT s.id::uuid AS evaluation_id,
WHEN ere.artifact_id IS NOT NULL THEN a.id
END AS uuid
) AS entity_id,
ere.entity_instance_id as new_entity_id,
-- raw fields for entity names
r.repo_owner,
r.repo_name,
Expand Down Expand Up @@ -230,7 +234,8 @@ SELECT s.evaluation_time,
WHEN ere.pull_request_id IS NOT NULL THEN ere.pull_request_id
WHEN ere.artifact_id IS NOT NULL THEN ere.artifact_id
END AS uuid
) AS entity_id
) AS entity_id,
ere.entity_instance_id as new_entity_id
FROM evaluation_statuses s
JOIN evaluation_rule_entities ere ON s.rule_entity_id = ere.id
LEFT JOIN latest_evaluation_statuses l
Expand All @@ -246,3 +251,20 @@ SELECT s.evaluation_time,
-- name: DeleteEvaluationHistoryByIDs :execrows
DELETE FROM evaluation_statuses s
WHERE s.id = ANY(sqlc.slice(evaluationIds));

-- TemporaryPopulateEvaluationHistory sets the entity_instance_id column for
-- all existing evaluation_rule_entities records to the id of the entity
-- instance that the rule entity is associated with. We derive this from the entity_type
-- and the corresponding entity id (repository_id, pull_request_id, or artifact_id).
-- Note that there are cases where repository_id and pull_request_id will both be set,
-- so we need to rely on the entity_type to determine which one to use. The same
-- applies to repository_id and artifact_id.

-- name: TemporaryPopulateEvaluationHistory :exec
UPDATE evaluation_rule_entities ere
SET entity_instance_id = CASE
WHEN ere.entity_type = 'repository' THEN ere.repository_id
WHEN ere.entity_type = 'pull_request' THEN ere.pull_request_id
WHEN ere.entity_type = 'artifact' THEN ere.artifact_id
END
WHERE entity_instance_id IS NULL;
62 changes: 49 additions & 13 deletions internal/db/eval_history.sql.go

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

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

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

4 changes: 4 additions & 0 deletions internal/db/profiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ func createRuleEntity(
PullRequestID: uuid.NullUUID{},
ArtifactID: uuid.NullUUID{},
EntityType: EntitiesRepository,
EntityInstanceID: uuid.NullUUID{
UUID: repoID,
Valid: true,
},
},
)
require.NoError(t, err)
Expand Down
8 changes: 8 additions & 0 deletions internal/db/querier.go

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

10 changes: 10 additions & 0 deletions internal/db/repositories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ func createRandomRepository(t *testing.T, project uuid.UUID, prov Provider, opts
require.NoError(t, err)
require.NotEmpty(t, repo)

ei, err := testQueries.CreateEntityWithID(context.Background(), CreateEntityWithIDParams{
ID: repo.ID,
Name: arg.RepoName,
ProjectID: project,
ProviderID: prov.ID,
EntityType: EntitiesRepository,
})
require.NoError(t, err)
require.NotEmpty(t, ei)

require.Equal(t, arg.Provider, repo.Provider)
require.Equal(t, arg.ProjectID, repo.ProjectID)
require.Equal(t, arg.RepoOwner, repo.RepoOwner)
Expand Down
Loading

0 comments on commit 4c18b4d

Please sign in to comment.