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 1 commit
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
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;
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we haven't written anything to the columns, this is safe to do.

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;
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.

14 changes: 7 additions & 7 deletions internal/db/rule_instances.sql.go

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

17 changes: 4 additions & 13 deletions internal/profiles/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"strings"

"github.com/google/uuid"
"github.com/sqlc-dev/pqtype"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/reflect/protoreflect"
Expand Down Expand Up @@ -773,22 +772,14 @@ func upsertRuleInstances(
return nil, fmt.Errorf("unable to serialize rule params: %w", err)
}

newInstance := db.UpsertRuleInstanceParams{
id, err := qtx.UpsertRuleInstance(ctx, db.UpsertRuleInstanceParams{
ProfileID: profileID,
RuleTypeID: entityRuleTuple.RuleID,
Name: rule.Name,
EntityType: entityType,
Def: pqtype.NullRawMessage{
RawMessage: def,
Valid: def != nil,
},
Params: pqtype.NullRawMessage{
RawMessage: params,
Valid: params != nil,
},
}

id, err := qtx.UpsertRuleInstance(ctx, newInstance)
Def: def,
Params: params,
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume that if there's no params defined then json.Marshall returns {}? I wasn't sure about that since the previous code had an explicit params != nil check. But I guess we'd want to either store {} or make sure to on retrieval convert whatever value is stored to {} .

})
if err != nil {
return nil, fmt.Errorf("unable to insert new rule instance: %w", err)
}
Expand Down