Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dual write rule instances to new and old tables #3486

Merged
merged 6 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions database/mock/store.go

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

6 changes: 3 additions & 3 deletions database/query/profiles.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ INSERT INTO entity_profiles (
$1,
$2,
sqlc.arg(contextual_rules)::jsonb,
FALSE
TRUE
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point, any new or updated profiles will have rules in the new rule instance table, and thus can be considered migrated.

) RETURNING *;

-- name: UpsertProfileForEntity :one
Expand All @@ -40,7 +40,7 @@ INSERT INTO entity_profiles (
) VALUES ($1, $2, sqlc.arg(contextual_rules)::jsonb, false)
ON CONFLICT (entity, profile_id) DO UPDATE SET
contextual_rules = sqlc.arg(contextual_rules)::jsonb,
migrated = FALSE
migrated = TRUE
RETURNING *;

-- name: DeleteProfileForEntity :exec
Expand Down Expand Up @@ -110,7 +110,7 @@ GROUP BY profiles.id;
-- name: CountProfilesByEntityType :many
SELECT COUNT(p.id) AS num_profiles, ep.entity AS profile_entity
FROM profiles AS p
JOIN entity_profiles AS ep ON p.id = ep.profile_id
JOIN entity_profiles AS ep ON p.id = ep.profile_id
GROUP BY ep.entity;

-- name: CountProfilesByName :one
Expand Down
52 changes: 52 additions & 0 deletions database/query/rule_instances.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
-- 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.

-- name: UpsertRuleInstance :one
INSERT INTO rule_instances (
profile_id,
rule_type_id,
name,
entity_type,
def,
params,
created_at,
updated_at
) VALUES(
$1,
$2,
$3,
$4,
$5,
$6,
NOW(),
NOW()
)
ON CONFLICT (profile_id, entity_type, name) DO UPDATE SET
rule_type_id = $2,
def = $5,
params = $6,
updated_at = NOW()
RETURNING id;

-- name: GetRuleInstancesForProfile :many
SELECT * FROM rule_instances WHERE profile_id = $1;

-- name: GetRuleInstancesForProfileEntity :many
SELECT * FROM rule_instances WHERE profile_id = $1 AND entity_type = $2;

-- name: DeleteNonUpdatedRules :exec
DELETE FROM rule_instances
WHERE profile_id = $1
AND entity_type = $2
AND NOT id = ANY(sqlc.arg(updated_ids)::UUID[]);
6 changes: 3 additions & 3 deletions internal/db/profiles.sql.go

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

17 changes: 17 additions & 0 deletions internal/db/querier.go

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

Loading