Skip to content

Commit

Permalink
Dual write rule instances to new and old tables (#3486)
Browse files Browse the repository at this point in the history
Relates to #3485

Any time a profile is created or updated, write the rule instances to
both the new and old rule instance tables. Mark the entries in the old
tables as migrated so that we skip over them when we run the migration
process.

Also made def/params field nullable.
  • Loading branch information
dmjb authored Jun 5, 2024
1 parent b2aecdf commit f170622
Show file tree
Hide file tree
Showing 10 changed files with 442 additions and 16 deletions.
20 changes: 20 additions & 0 deletions database/migrations/000061_rule_instance_not_null_def.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- 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;

ALTER TABLE rule_instances ALTER COLUMN def DROP NOT NULL;
ALTER TABLE rule_instances ALTER COLUMN params DROP NOT NULL;

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

ALTER TABLE rule_instances ALTER COLUMN def SET NOT NULL;
ALTER TABLE rule_instances ALTER COLUMN params SET NOT NULL;

COMMIT;
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
) 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[]);
18 changes: 9 additions & 9 deletions internal/db/models.go

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

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

0 comments on commit f170622

Please sign in to comment.