no-db-constraints

Detect and remove database-level constraints from Laravel migrations and plan files.

2|Updated Nov 16, 2024
One-click install
npx skills add https://github.com/onairmarc/dotfiles --skill no-db-constraints
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: no-db-constraints
Source: https://github.com/onairmarc/dotfiles/tree/main/.agents/skills/no-db-constraints
Command: npx skills add https://github.com/onairmarc/dotfiles --skill no-db-constraints

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This skill helps Laravel teams remove database-level foreign key constraints and unique constraints, replacing them with application-level enforcement to keep business rules in code rather than in the database.

Core Features & Use Cases

  • Remove all database-level foreign keys and constraints declared in migrations, including ->foreign(), ->constrained(), and FOREIGN KEY references.
  • Remove database-level unique constraints and unique indexes, with guidance to replace with model-level checks and appropriate plain indexes when beneficial.
  • Implement plan-file and migration-based workflows: edit plan files directly for planned changes and create drop migrations for already-run constraints; add boot() checks or Rule classes to enforce rules in the application.
  • Add boot() checks in relevant Eloquent models for creation and updating to enforce uniqueness, using exists() to ensure no duplicates, and excluding the current model on updates.
  • Optional dedicated Rule classes for complex constraints to be reused.

Quick Start

Identify migrations and plan files with constraints, then apply drop migrations or plan edits and add boot() checks in the relevant models.

Frequently Asked Questions about no-db-constraints

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I remove foreign key constraints in Laravel migrations and enforce them in the application?

To remove foreign key constraints in Laravel, you create drop migrations for already-run constraints or edit plan files directly. You then enforce these rules at the application level using Eloquent boot() hooks or dedicated Rule classes.

What is the best way to replace database unique constraints with model-level checks in Laravel?

The best way to replace database unique constraints is to drop the unique indexes via migrations and implement model-level checks. You add boot() hooks for creating and updating events, using exists() to verify uniqueness while excluding the current model.

Can I use Laravel boot() hooks to enforce uniqueness without database constraints?

Yes, you can use Laravel boot() hooks to enforce uniqueness without database constraints. By adding creating and updating event listeners, you perform fast exists() checks to prevent duplicates, ensuring business rules remain in code rather than the database.

How do I handle drop migrations for existing foreign keys and unique indexes in Laravel?

Handling drop migrations involves generating new migrations that specifically target and remove existing foreign keys and unique indexes. You can replace dropped unique constraints with plain indexes when beneficial for query performance while maintaining application checks.

Does removing database-level constraints and moving logic to models affect Laravel plan file workflows?

Removing database-level constraints directly affects plan file workflows by allowing you to edit planned migration files to strip constraints before execution. For already-run migrations, you must generate separate drop migrations to alter the database schema safely.

When should I use dedicated Rule classes instead of boot() hooks for application-level constraints in Laravel?

You should use dedicated Rule classes instead of boot() hooks when dealing with complex constraints that need to be reused across different parts of your Laravel application. Boot() hooks are better suited for standard creation and updating uniqueness checks on a single model.