Skip to content

Commit 3713317

Browse files
committed
fix: delete expired rules before insert to avoid UNIQUE constraint error
Expired rules with the same (container, destination, port, action) key were blocking new rule creation because findExistingRule skips expired rows but the UNIQUE constraint still enforces against them.
1 parent 26d2792 commit 3713317

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

internal/greyproxy/crud.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ func CreateRule(db *DB, input RuleCreateInput) (*Rule, error) {
5757
notes = sql.NullString{String: *input.Notes, Valid: true}
5858
}
5959

60+
// Delete any expired rule with the same unique key so the insert won't conflict
61+
db.WriteDB().Exec(
62+
`DELETE FROM rules
63+
WHERE container_pattern = ? AND destination_pattern = ? AND port_pattern = ? AND action = ?
64+
AND expires_at IS NOT NULL AND expires_at <= datetime('now')`,
65+
input.ContainerPattern, input.DestinationPattern, input.PortPattern, input.Action,
66+
)
67+
6068
result, err := db.WriteDB().Exec(
6169
`INSERT INTO rules (container_pattern, destination_pattern, port_pattern, rule_type, action, expires_at, created_by, notes)
6270
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,

0 commit comments

Comments
 (0)