Skip to content

Conversation

aniketio-ctrl
Copy link
Member

@aniketio-ctrl aniketio-ctrl commented Oct 3, 2025

📄 Summary

Currently Rule which our part of planned maintenance give error on delete due to foreign key constraint on planned maintenance table, so we added functionality to remove it from the table first then on deleting the rule.

The problem arises because there is no delete cascade on rule foreign constraint in planned maintenance table, and it would require migration, with sqlite we need to delete and create the table again.



Important

Fixes rule deletion with planned maintenance by removing from planned_maintenance_rule table first.

  • Behavior:
    • Fixes deletion of rules with planned maintenance by first removing them from planned_maintenance_rule table in DeleteRule() in sqlrulestore/rule.go.
  • Tests:
    • Adds TestDeleteRule in manager_test.go to verify rule deletion with planned maintenance.
    • Updates ExpectDeleteRule in rulestoretest/rule.go to expect deletion from planned_maintenance_rule table.

This description was created by Ellipsis for 0634a88. You can customize this summary. It will automatically update as commits are pushed.

@github-actions github-actions bot added the bug Something isn't working label Oct 3, 2025
Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

Caution

Changes requested ❌

Reviewed everything up to 13884cc in 6 minutes and 9 seconds. Click for details.
  • Reviewed 48 lines of code in 3 files
  • Skipped 0 files when reviewing.
  • Skipped posting 3 draft comments. View those below.
  • Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. pkg/query-service/rules/manager.go:485
  • Draft comment:
    Use a consistent UUID-to-string conversion method. Here you use id.StringValue() in some places and id.String() in others.
  • Reason this comment was not posted:
    Comment was not on a location in the diff, so it can't be submitted as a review comment.
2. pkg/ruler/rulestore/sqlrulestore/maintenance.go:227
  • Draft comment:
    The new DeletePlannedMaintenanceRulesByRuleID function is implemented correctly. Consider adding logging of the deletion outcome for easier debugging.
  • Reason this comment was not posted:
    Confidence changes required: 30% <= threshold 50% None
3. pkg/types/ruletypes/maintenance.go:340
  • Draft comment:
    Document the purpose and usage of the new DeletePlannedMaintenanceRulesByRuleID method in the MaintenanceStore interface for clarity.
  • Reason this comment was not posted:
    Confidence changes required: 30% <= threshold 50% None

Workflow ID: wflow_IPjpLiozR1FX5DYX

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

@aniketio-ctrl aniketio-ctrl marked this pull request as draft October 3, 2025 13:33
@aniketio-ctrl aniketio-ctrl marked this pull request as ready for review October 6, 2025 09:38
Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

Caution

Changes requested ❌

Reviewed everything up to 0634a88 in 1 minute and 36 seconds. Click for details.
  • Reviewed 196 lines of code in 3 files
  • Skipped 0 files when reviewing.
  • Skipped posting 1 draft comments. View those below.
  • Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. pkg/query-service/rules/manager_test.go:816
  • Draft comment:
    TestDeleteRule: Good table‐driven deletion test covering planned maintenance cleanup. Consider asserting that no orphaned planned maintenance records remain (if possible) to strengthen the verification.
  • Reason this comment was not posted:
    Decided after close inspection that this draft comment was likely wrong and/or not actionable: usefulness confidence = 10% vs. threshold = 50% The comment suggests a potentially valid test improvement, but there are several issues: 1) It's speculative - we don't see evidence that orphaned maintenance records are actually a concern 2) It's not clearly actionable without more context about the maintenance system 3) The comment uses "if possible" which makes it uncertain 4) The test is already verifying the core deletion contract through mocks. The suggestion could catch real bugs if orphaned maintenance records are a known issue in the system. I may be dismissing a valid testing concern. While orphaned records could be an issue, the comment is too speculative without evidence. A good comment would need to demonstrate that this is a real problem and provide clear guidance on how to test for it. The comment should be deleted as it makes a speculative suggestion without clear evidence of the problem or actionable guidance on implementing the test.

Workflow ID: wflow_CwRcqZPFZwI88hls

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.


// ExpectDeleteRule sets up SQL expectations for DeleteRule operation
func (m *MockSQLRuleStore) ExpectDeleteRule(ruleID valuer.UUID) {
plannedMaintenancePattern := `DELETE FROM "planned_maintenance_rule".+WHERE \(rule_id = '` + ruleID.StringValue() + `'\)`
Copy link
Contributor

Choose a reason for hiding this comment

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

ExpectDeleteRule now expects deletion from the 'planned_maintenance_rule' table. Please add an inline comment explaining that this is done to work around the lack of cascade delete for foreign key constraints.

Suggested change
plannedMaintenancePattern := `DELETE FROM "planned_maintenance_rule".+WHERE \(rule_id = '` + ruleID.StringValue() + `'\)`
plannedMaintenancePattern := `DELETE FROM "planned_maintenance_rule".+WHERE (rule_id = '` + ruleID.StringValue() + `')` // Workaround: no cascade delete for FK constraints, so must delete explicitly


func (r *rule) DeleteRule(ctx context.Context, id valuer.UUID, cb func(context.Context) error) error {
if err := r.sqlstore.RunInTxCtx(ctx, nil, func(ctx context.Context) error {
_, err := r.sqlstore.
Copy link
Contributor

Choose a reason for hiding this comment

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

In DeleteRule, add a comment explaining that the deletion of planned maintenance records is performed first to avoid foreign key constraint errors due to lack of cascade delete.

@vikrantgupta25 vikrantgupta25 requested a review from Copilot October 8, 2025 19:18
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request fixes a foreign key constraint issue when deleting rules that are part of planned maintenance. The fix removes the rule from the planned_maintenance_rule table before deleting the rule itself.

  • Adds deletion of planned maintenance rule associations before rule deletion
  • Updates test expectations to include the new planned maintenance deletion step
  • Adds comprehensive test coverage for rule deletion with planned maintenance

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
pkg/ruler/rulestore/sqlrulestore/rule.go Adds deletion from planned_maintenance_rule table before deleting the rule
pkg/ruler/rulestore/rulestoretest/rule.go Updates mock expectations to include planned maintenance rule deletion
pkg/query-service/rules/manager_test.go Adds comprehensive test cases for rule deletion scenarios

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +59 to +63
BunDBCtx(ctx).
NewDelete().
Model(new(ruletypes.StorablePlannedMaintenanceRule)).
Where("rule_id = ?", id.StringValue()).
Exec(ctx)
Copy link

Copilot AI Oct 8, 2025

Choose a reason for hiding this comment

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

The deletion from planned_maintenance_rule table should handle the case where no rows are affected, as this is expected when a rule has no planned maintenance associations. Consider storing the result and checking if it's acceptable to have 0 affected rows.

Copilot uses AI. Check for mistakes.

@srikanthccv
Copy link
Member

I created two issues #9270 and #9272. The planned downtime will be part of alertmanager work and shares the same spirit that of the route policy i.e downtime based on the set of labels and ruleId as a label to suppress a specific. Eventually, we will get rid of the table with the flattened rules mapping and instead communicate with AM do schedule downtimes directly.

@srikanthccv
Copy link
Member

@aniketio-ctrl

  1. create a view rules_active, update all the implementation to use the view instead of direct rules.
  2. when a rule is deleted, mark the deleted to true for rules.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants