mcpField
C
constraints-reviewer

constraints-reviewer

github.com/majesticlabs-dev测试与质量5036 次安装7 周前更新
代码仓库

关于 constraints-reviewer

用于检查数据库约束、外键和引用完整性的审查技能,示例基于 Rails 与 ActiveRecord,可帮助发现数据库强制规则缺失及孤立记录等问题。

主要功能

  1. 01核对模型验证与数据库约束
  2. 02检查外键和删除级联
  3. 03审查多态关联保护
  4. 04查找孤立记录
  5. 05生成通过/警告/失败报告

适用场景

  1. 01审查 Rails 模式完整性
  2. 02查找缺少数据库强制约束的验证
  3. 03检查关联记录的删除行为

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/majesticlabs-dev/majestic-marketplace --skill constraints-reviewer

安装后在 Claude Code 中输入/constraints-reviewer确认已加载。

详细介绍

约束与引用完整性审查

审查数据库约束、外键和数据完整性规则。

数据库级强制执行

# PROBLEM: Model-only validation (race condition vulnerable)
validates :email, uniqueness: true

# SOLUTION: Database constraint + model validation
add_index :users, :email, unique: true
validates :email, uniqueness: true

防止竞态条件

# PROBLEM: Check-then-insert race condition
def claim_slot
  return false if Slot.where(user_id: user.id).exists?
  Slot.create!(user_id: user.id)
end

# SOLUTION: Database constraint with rescue
def claim_slot
  Slot.create!(user_id: user.id)
rescue ActiveRecord::RecordNotUnique
  false
end

缺失约束检查清单

验证必需的数据库约束
presence: trueNOT NULL
uniqueness: true唯一索引
belongs_to外键
enum检查约束
numericality: { in: }检查约束

外键级联

# PROBLEM: Orphaned records on deletion
has_many :comments

# SOLUTION: Dependent handling
has_many :comments, dependent: :destroy  # For callbacks
# OR database-level:
add_foreign_key :comments, :posts, on_delete: :cascade

多态关联

# PROBLEM: No referential integrity for polymorphics
belongs_to :commentable, polymorphic: true

# SOLUTION: Validate type + consider alternatives
ALLOWED_TYPES = %w[Post Article].freeze
validates :commentable_type, inclusion: { in: ALLOWED_TYPES }

# Better: separate tables instead of polymorphic
belongs_to :post, optional: true
belongs_to :article, optional: true
validates :post_id, presence: true, unless: :article_id?

孤立记录检测查询

# Find orphaned comments (missing parent)
Comment.left_joins(:post).where(posts: { id: nil })

# Find missing foreign keys
ActiveRecord::Base.connection.tables.each do |table|
  columns = ActiveRecord::Base.connection.columns(table)
  columns.select { |c| c.name.end_with?('_id') }.each do |col|
    # Check if FK exists
  end
end

审查清单

  • 所有 uniqueness 验证都配置了唯一索引吗?
  • 所有 presence 验证都配置了 NOT NULL 约束吗?
  • 所有 _id 列都有外键吗?
  • 是否为删除操作定义了级联规则?
  • 多态类型是否经过验证?
  • 是否能避免产生孤立记录?

输出格式

## Constraints Review: [PASS/WARN/FAIL]

### Missing Database Constraints
- [model]: [validation without DB enforcement]

### Foreign Key Issues
- [table]: [missing FK on _id column]

### Race Condition Risks
- [code location]: [check-then-insert pattern]

### Orphan Risks
- [association]: [could create orphans]

### Recommendations
1. [Prioritized fixes]

评论

支持 Markdown