mcpField
C
constraints-reviewer

constraints-reviewer

github.com/majesticlabs-devTesting & quality5036 installsUpdated 7w ago
Repository

About constraints-reviewer

A database review skill for checking constraints, foreign keys, and referential integrity, with examples focused on Rails and ActiveRecord. It helps reviewers identify integrity gaps such as missing database enforcement and orphaned records.

Key features

  1. 01Compare model validations with DB constraints
  2. 02Check foreign keys and deletion cascades
  3. 03Review polymorphic association safeguards
  4. 04Find orphaned records
  5. 05Structured pass/warn/fail report

Use cases

  1. 01Review Rails schema integrity
  2. 02Find validations lacking database enforcement
  3. 03Check deletion behavior for related records

Install

Copy the command and run it in a terminal.

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

After installing, type/constraints-reviewerin Claude Code to confirm it loaded.

Details

From the project README

Constraints & Referential Integrity Review

Review database constraints, foreign keys, and data integrity rules.

Database-Level Enforcement

# 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

Race Condition Prevention

# 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

Missing Constraints Checklist

ValidationRequired DB Constraint
presence: trueNOT NULL
uniqueness: trueUnique index
belongs_toForeign key
enumCheck constraint
numericality: { in: }Check constraint

Foreign Key Cascades

# 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

Polymorphic Associations

# 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?

Orphan Detection Queries

# 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

Review Checklist

  • All uniqueness validations have unique indexes?
  • All presence validations have NOT NULL constraints?
  • All _id columns have foreign keys?
  • Cascade rules defined for deletions?
  • Polymorphic types validated?
  • No orphaned records possible?

Output Format

## 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]

Comments

Markdown supported