feat(规则): 支持名称代号,便于区分和管理
创建与更新规则时必填全局唯一 name,已有规则迁移回填为 rule-{id}。
This commit is contained in:
+11
-11
@@ -16,12 +16,12 @@ func (s *Store) CreateRule(ctx context.Context, r *model.Rule, channelIDs []int)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
query := `INSERT INTO notification_rule (source_id, event, template_id, conditions, enabled) VALUES (?, ?, ?, ?, ?)`
|
||||
query := `INSERT INTO notification_rule (name, source_id, event, template_id, conditions, enabled) VALUES (?, ?, ?, ?, ?, ?)`
|
||||
condsJSON, err := marshalJSON(r.Conditions)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal conditions: %w", err)
|
||||
}
|
||||
result, err := tx.ExecContext(ctx, query, r.SourceID, r.Event, r.TemplateID, condsJSON, r.Enabled)
|
||||
result, err := tx.ExecContext(ctx, query, r.Name, r.SourceID, r.Event, r.TemplateID, condsJSON, r.Enabled)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create rule: %w", err)
|
||||
}
|
||||
@@ -40,8 +40,8 @@ func (s *Store) CreateRule(ctx context.Context, r *model.Rule, channelIDs []int)
|
||||
func (s *Store) GetRule(ctx context.Context, id int) (*model.Rule, error) {
|
||||
var r model.Rule
|
||||
var condsBytes []byte
|
||||
row := s.DB.QueryRowContext(ctx, `SELECT id, source_id, event, template_id, conditions, enabled, created_at, updated_at FROM notification_rule WHERE id = ?`, id)
|
||||
if err := row.Scan(&r.ID, &r.SourceID, &r.Event, &r.TemplateID, &condsBytes, &r.Enabled, &r.CreatedAt, &r.UpdatedAt); err != nil {
|
||||
row := s.DB.QueryRowContext(ctx, `SELECT id, name, source_id, event, template_id, conditions, enabled, created_at, updated_at FROM notification_rule WHERE id = ?`, id)
|
||||
if err := row.Scan(&r.ID, &r.Name, &r.SourceID, &r.Event, &r.TemplateID, &condsBytes, &r.Enabled, &r.CreatedAt, &r.UpdatedAt); err != nil {
|
||||
return nil, fmt.Errorf("get rule %d: %w", id, err)
|
||||
}
|
||||
if len(condsBytes) > 0 && string(condsBytes) != "null" {
|
||||
@@ -54,9 +54,9 @@ func (s *Store) GetRule(ctx context.Context, id int) (*model.Rule, error) {
|
||||
func (s *Store) GetRuleBySourceEvent(ctx context.Context, sourceID int, event string) (*model.Rule, error) {
|
||||
var r model.Rule
|
||||
var condsBytes []byte
|
||||
query := `SELECT id, source_id, event, template_id, conditions, enabled, created_at, updated_at FROM notification_rule WHERE source_id = ? AND event = ? AND enabled = 1`
|
||||
query := `SELECT id, name, source_id, event, template_id, conditions, enabled, created_at, updated_at FROM notification_rule WHERE source_id = ? AND event = ? AND enabled = 1`
|
||||
row := s.DB.QueryRowContext(ctx, query, sourceID, event)
|
||||
if err := row.Scan(&r.ID, &r.SourceID, &r.Event, &r.TemplateID, &condsBytes, &r.Enabled, &r.CreatedAt, &r.UpdatedAt); err != nil {
|
||||
if err := row.Scan(&r.ID, &r.Name, &r.SourceID, &r.Event, &r.TemplateID, &condsBytes, &r.Enabled, &r.CreatedAt, &r.UpdatedAt); err != nil {
|
||||
return nil, fmt.Errorf("get rule by source+event: %w", err)
|
||||
}
|
||||
if len(condsBytes) > 0 && string(condsBytes) != "null" {
|
||||
@@ -67,7 +67,7 @@ func (s *Store) GetRuleBySourceEvent(ctx context.Context, sourceID int, event st
|
||||
}
|
||||
|
||||
func (s *Store) ListEnabledRulesBySource(ctx context.Context, sourceID int) ([]model.Rule, error) {
|
||||
rows, err := s.DB.QueryContext(ctx, `SELECT id, source_id, event, template_id, conditions, enabled, created_at, updated_at FROM notification_rule WHERE source_id = ? AND enabled = 1`, sourceID)
|
||||
rows, err := s.DB.QueryContext(ctx, `SELECT id, name, source_id, event, template_id, conditions, enabled, created_at, updated_at FROM notification_rule WHERE source_id = ? AND enabled = 1`, sourceID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list enabled rules by source: %w", err)
|
||||
}
|
||||
@@ -86,7 +86,7 @@ func (s *Store) ListRules(ctx context.Context, page PageFilter) ([]model.Rule, i
|
||||
}
|
||||
|
||||
page.Normalize()
|
||||
rows, err := s.DB.QueryContext(ctx, `SELECT id, source_id, event, template_id, conditions, enabled, created_at, updated_at FROM notification_rule ORDER BY id LIMIT ? OFFSET ?`, page.PageSize, page.Offset())
|
||||
rows, err := s.DB.QueryContext(ctx, `SELECT id, name, source_id, event, template_id, conditions, enabled, created_at, updated_at FROM notification_rule ORDER BY id LIMIT ? OFFSET ?`, page.PageSize, page.Offset())
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("list rules: %w", err)
|
||||
}
|
||||
@@ -109,8 +109,8 @@ func (s *Store) UpdateRule(ctx context.Context, id int, r *model.Rule, channelID
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal conditions: %w", err)
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, `UPDATE notification_rule SET source_id=?, event=?, template_id=?, conditions=?, enabled=? WHERE id=?`,
|
||||
r.SourceID, r.Event, r.TemplateID, condsJSON, r.Enabled, id)
|
||||
_, err = tx.ExecContext(ctx, `UPDATE notification_rule SET name=?, source_id=?, event=?, template_id=?, conditions=?, enabled=? WHERE id=?`,
|
||||
r.Name, r.SourceID, r.Event, r.TemplateID, condsJSON, r.Enabled, id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("update rule: %w", err)
|
||||
}
|
||||
@@ -183,7 +183,7 @@ func scanRules(rows *sql.Rows) ([]model.Rule, error) {
|
||||
for rows.Next() {
|
||||
var r model.Rule
|
||||
var condsBytes []byte
|
||||
if err := rows.Scan(&r.ID, &r.SourceID, &r.Event, &r.TemplateID, &condsBytes, &r.Enabled, &r.CreatedAt, &r.UpdatedAt); err != nil {
|
||||
if err := rows.Scan(&r.ID, &r.Name, &r.SourceID, &r.Event, &r.TemplateID, &condsBytes, &r.Enabled, &r.CreatedAt, &r.UpdatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(condsBytes) > 0 && string(condsBytes) != "null" {
|
||||
|
||||
Reference in New Issue
Block a user