# Safe Migration & Slow-Query Fixer

> Gate Laravel migrations for production safety (MySQL/PostgreSQL locking, expand-contract, BLOCK/REVIEW/PASS) and find/fix N+1 and slow queries with measured before/after numbers.

Safe Migration & Slow-Query Fixer is a free AI agent skill on Agensi by Maulana Ruhaedi Putra, in Data & Databases. It loads into your AI agent through the Agensi MCP server, always in its latest approved version.

- Price: Free
- Creator: [Maulana Ruhaedi Putra](https://www.agensi.io/creators/maulana-ruhaedi-putra)
- Category: [Data & Databases](https://www.agensi.io/skills/data-databases)
- Version: 1.0
- Last updated: 2026-11-30
- Security: scan passed (2026-11-30); every new listing is also reviewed by a person
- Listing: https://www.agensi.io/skills/safe-migration-slow-query-fixer
- Tags: database, laravel, mysql, postgresql, performance, query-optimization

## About this skill

Database migrations on production tables often cause downtime through locks, while N+1 queries and missing indexes degrade performance as data grows. This skill provides a rigorous safety gate for Laravel migrations and a measurement-first workflow for fixing slow queries.

### What it does
- **Migration gating** classifies schema changes as PASS, REVIEW, or BLOCK based on production safety rules for MySQL and PostgreSQL.
- **Lock prevention** enforces fail-fast lock timeouts and engine-specific safe operations like `CONCURRENTLY` or `ALGORITHM=INPLACE`.
- **N+1 detection** identifies redundant queries using `Model::preventLazyLoading` and query log analysis.
- **Performance measurement** requires before/after benchmarks for every fix, including query counts and execution time in milliseconds.
- **Index optimization** analyzes `EXPLAIN` plans to recommend composite, covering, or partial indexes based on real query shapes.

### How it works
1. **Engine detection** identifies the database type and version from `config/database.php` to branch rule logic.
2. **Pre-flight audit** records table sizes and runs `migrate --pretend` to inspect the raw SQL before execution.
3. **Safety validation** checks the migration against a matrix of destructive operations and locking behaviors.
4. **Performance fix** applies eager loading or index changes and verifies the result with at least three benchmark runs.

### Frameworks & tools
This skill is built for Laravel applications using MySQL, MariaDB, or PostgreSQL. It utilizes Eloquent, Laravel Telescope, and the standard database migration system.

### Why this beats prompting it yourself
Generic prompts often miss engine-specific locking nuances like how PostgreSQL handles foreign keys versus MySQL. This skill enforces a strict "no numbers, no fix" policy that prevents developers from shipping unverified optimizations or hiding N+1 issues behind a cache.

### Use cases
- Reviewing a migration that adds a column to a table with millions of rows.
- Fixing an API endpoint that gets slower as the number of related records grows.
- Reverting a migration that lacks a proper `down()` method or contains destructive SQL.
- Optimizing complex queries by analyzing and improving the `EXPLAIN` execution plan.

### Known limitations
Does not support live production profiling or load testing. Measurements on local environments may differ from production due to hardware and data volume variance.

## How to use this skill

1. Connect your AI agent to Agensi once: add the MCP server https://mcp.agensi.io/mcp/auth and sign in with your Agensi account (OAuth, no API key). Steps for Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and other agents: https://www.agensi.io/docs/mcp-setup
2. Ask your agent: "Use the Safe Migration & Slow-Query Fixer skill from Agensi" and describe your task.
3. Your agent loads the skill from Agensi when it needs it. Nothing is downloaded.

## Frequently asked questions

### How does this skill prevent production downtime during migrations?

This skill acts as an automated safety gate that analyzes Laravel migrations for potential locking issues and provides a measurement-driven workflow to eliminate N+1 queries and slow database execution. It ensures that schema changes won't lock large tables and that performance improvements are verified with actual benchmark data.

### Which database engines and frameworks are supported?

The skill is purpose-built for Laravel applications running on MySQL, MariaDB, or PostgreSQL. It integrates directly with Eloquent and Laravel's migration system, leveraging engine-specific rules for each database type to handle locking behaviors correctly.

### How does this differ from just asking an AI to optimize a query?

Unlike a standard LLM prompt, this skill understands the nuanced differences between database engines, such as how PostgreSQL handles CONCURRENTLY indexes versus MySQL's ALGORITHM=INPLACE. It also enforces a 'measurement-first' policy, refusing to finalize a fix unless it can prove a performance gain through before-and-after benchmarks.

### What is included in the purchase of this skill?

You will receive the AI agent skill package including the safety rule matrix, the benchmark workflow logic, and the pre-flight audit protocols. It includes everything needed to integrate the agent into your CI/CD pipeline or local development environment.

### Does the skill automatically execute migrations on my production database?

While the skill provides highly accurate safety classifications, it is intended to support and inform your engineering team rather than replace them. It marks migrations as PASS, REVIEW, or BLOCK, allowing your developers to make the final decision on high-risk operations.

### Are there any limitations when testing performance locally?

The skill performs a pre-flight audit using 'migrate --pretend' to inspect raw SQL and checks table metadata to predict locking behavior. However, because it operates in your development or staging environment, it cannot account for real-time production traffic spikes or unique server hardware configurations.

---

Source: [Agensi](https://www.agensi.io), the marketplace of skills for AI agents. Any MCP-compatible agent (Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and more) connects at https://mcp.agensi.io/mcp/auth and signs in with an Agensi account; setup for every agent: https://www.agensi.io/docs/mcp-setup. Append .md to any skill, guide or docs address on agensi.io for its markdown version.
