agentleFS
vicleung01vicleung01/openocta-db-ops-skills/mysql-schema-linter/SKILL.md

mysql-schema-linter

Skill8 starsChanged 6 months agoApache-2.0View on GitHub

Automated MySQL schema review tool. Scans all tables for common issues: missing primary key, wrong storage engine, non-utf8mb4 charset, redundant indexes, foreign key constraints, auto-increment overflow risk, and unpartitioned large tables. Outputs prioritized findings with fix SQL.

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 2 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Before production deployment of new schema

    line 31
  2. Distinctive:

    After bulk DDL operations

    line 32

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

MySQL Schema Linter

Overview

Schema issues are silent killers — they don't crash the database immediately, but cause performance degradation, replication lag, and operational headaches over time. This skill performs automated schema review across 9 dimensions:

  1. ❌ Missing Primary Key
  2. ❌ Wrong Storage Engine (non-InnoDB)
  3. ❌ Non-utf8mb4 Charset
  4. ❌ Auto-increment approaching INT limit
  5. ❌ Duplicate/Redundant Indexes
  6. ❌ Foreign Key Constraints
  7. ❌ Large tables without partitioning
  8. ❌ Tables without any index
  9. ❌ Tables with NULLable columns that should be NOT NULL

When to Use

  • Periodic schema audit (monthly/quarterly)
  • Before production deployment of new schema
  • After bulk DDL operations
  • Pre-migration assessment
  • Developer requests schema review

Required MCP Server

ToolPurpose
db_queryQuery information_schema
db_list_databasesGet database list
db_describe_tableCheck table structure

Required Privileges

  • SELECT on information_schema.*

Diagnostic Workflow

Step 1: Get All User Databases

SELECT SCHEMA_NAME FROM information_schema.SCHEMATA
WHERE SCHEMA_NAME NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys')
ORDER BY SCHEMA_NAME;

Step 2: Full Schema Scan (Single Query)

-- Comprehensive schema analysis in one pass
SELECT
  t.TABLE_SCHEMA AS db_name,
  t.TABLE_NAME AS table_name,
  t.ENGINE AS engine,
  t.TABLE_ROWS AS row_count,
  t.TABLE_COLLATION AS collation,
  t.DATA_LENGTH,
  t.INDEX_LENGTH,
  t.CREATE_OPTIONS,
  t.AUTO_INCREMENT,
  -- Check: has PK?
  CASE WHEN pk.TABLE_NAME IS NOT NULL THEN 'YES' ELSE 'NO' END AS has_pk,
  -- Check: index count
  idx_count.cnt AS index_count
FROM information_schema.TABLES t
LEFT JOIN (
  SELECT TABLE_SCHEMA, TABLE_NAME
  FROM information_schema.TABLE_CONSTRAINTS
  WHERE CONSTRAINT_TYPE = 'PRIMARY KEY'
) pk ON t.TABLE_SCHEMA = pk.TABLE_SCHEMA AND t.TABLE_NAME = pk.TABLE_NAME
LEFT JOIN (
  SELECT TABLE_SCHEMA, TABLE_NAME, COUNT(*) AS cnt
  FROM information_schema.STATISTICS
  GROUP BY TABLE_SCHEMA, TABLE_NAME
) idx_count ON t.TABLE_SCHEMA = idx_count.TABLE_SCHEMA AND t.TABLE_NAME = idx_count.TABLE_NAME
WHERE t.TABLE_SCHEMA NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys')
  AND t.TABLE_TYPE = 'BASE TABLE'
ORDER BY t.TABLE_SCHEMA, t.TABLE_NAME;

Step 3: Check for Issues

-- 1. Missing Primary Key
SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_ROWS
FROM information_schema.TABLES t
WHERE t.TABLE_SCHEMA NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys')
  AND t.TABLE_TYPE = 'BASE TABLE'
  AND NOT EXISTS (
    SELECT 1 FROM information_schema.TABLE_CONSTRAINTS tc
    WHERE tc.TABLE_SCHEMA = t.TABLE_SCHEMA AND tc.TABLE_NAME = t.TABLE_NAME
    AND tc.CONSTRAINT_TYPE = 'PRIMARY KEY'
  )
ORDER BY TABLE_ROWS DESC;

-- 2. Non-InnoDB Engine
SELECT TABLE_SCHEMA, TABLE_NAME, ENGINE
FROM information_schema.TABLES
WHERE ENGINE != 'InnoDB' AND ENGINE IS NOT NULL
  AND TABLE_SCHEMA NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys')
  AND TABLE_TYPE = 'BASE TABLE';

-- 3. Non-utf8mb4 Charset
SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_COLLATION
FROM information_schema.TABLES
WHERE TABLE_SCHEMA NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys')
  AND TABLE_TYPE = 'BASE TABLE'
  AND TABLE_COLLATION NOT LIKE 'utf8mb4%'
  AND TABLE_COLLATION IS NOT NULL;

-- 4. Redundant Indexes (same leading column)
SELECT a.TABLE_SCHEMA, a.TABLE_NAME,
  a.INDEX_NAME AS idx1, b.INDEX_NAME AS idx2,
  a.COLUMN_NAME AS shared_prefix
FROM information_schema.STATISTICS a
JOIN information_schema.STATISTICS b
  ON a.TABLE_SCHEMA = b.TABLE_SCHEMA AND a.TABLE_NAME = b.TABLE_NAME
  AND a.SEQ_IN_INDEX = 1 AND b.SEQ_IN_INDEX = 1
  AND a.COLUMN_NAME = b.COLUMN_NAME
  AND a.INDEX_NAME < b.INDEX_NAME
WHERE a.TABLE_SCHEMA NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys');

-- 5. Foreign Keys
SELECT TABLE_SCHEMA, TABLE_NAME, CONSTRAINT_NAME,
  REFERENCED_TABLE_NAME, REFERENCED_TABLE_SCHEMA
FROM information_schema.KEY_COLUMN_USAGE
WHERE REFERENCED_TABLE_NAME IS NOT NULL
  AND TABLE_SCHEMA NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys');

-- 6. Auto-increment near INT limit (2^31 - 1 = 2,147,483,647)
SELECT TABLE_SCHEMA, TABLE_NAME, AUTO_INCREMENT
FROM information_schema.TABLES
WHERE AUTO_INCREMENT > 2000000000
  AND TABLE_SCHEMA NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys');

Output Template

📐 MySQL Schema Lint Report
═══════════════════════════
📋 Overview
  Instance: {host}:{port}
  Databases: {count} | Tables: {count}
  Issues Found: {total} ({critical} Critical, {warning} Warning, {info} Info)

🔴 CRITICAL — Missing Primary Key ({count} tables)
  Top 5 largest tables without PK:
  | Database | Table | Rows |
  |----------|-------|-------|
  | ...      | ...   | ...   |

  Fix: ALTER TABLE {schema}.{table} ADD PRIMARY KEY (id);
  ⚠️ Large tables: use pt-online-schema-change or gh-ost

🟡 WARNING — Non-utf8mb4 Charset ({count} tables)
  | Collation | Count |
  |-----------|-------|
  | utf8_bin  | {n}   |
  | latin1    | {n}   |

  Fix: ALTER TABLE {table} CONVERT TO CHARACTER SET utf8mb4;

🟡 WARNING — Redundant Indexes ({count} pairs)
  | Table | Index 1 | Index 2 | Shared Column |
  |-------|---------|---------|---------------|

  Fix: DROP INDEX {redundant_idx} ON {table};

ℹ️ INFO — Foreign Keys ({count} tables)
  ℹ️ FK constraints can cause cascading locks.
  Review if application-level integrity is preferred.

📊 Summary
  Total Tables: {n}
  Clean Tables: {n} ({pct}%)
  Issues: {n} Critical, {n} Warning, {n} Info

Example Usage

User: "Lint the schema of all databases"

AI Response:

📐 Schema Lint Report

82 databases, 8,791 tables scanned

🔴 CRITICAL: 144 tables missing Primary Key
  Top: dm_meta_*.syncer_checkpoint (DM checkpoint tables)
  → Low risk, but recommend adding PK for new tables

🟡 WARNING: 3,447 tables non-utf8mb4
  utf8_bin: 3,421 | utf8_general_ci: 26
  → Plan charset migration during maintenance window

ℹ️ INFO: 7 foreign key constraints found

📊 Clean: 5,193/8,791 (59%)

Fix Scripts (Copy-Paste Ready)

Fix 1: Add Primary Key (Online)

-- Small tables: direct ALTER
ALTER TABLE {schema}.{table} ADD PRIMARY KEY (id);

-- Large tables: use online DDL (MySQL 5.6+)
ALTER TABLE {schema}.{table} ADD PRIMARY KEY (id),
  ALGORITHM=INPLACE, LOCK=NONE;

-- Very large tables: use pt-online-schema-change
-- pt-online-schema-change --alter "ADD PRIMARY KEY (id)" \
--   D={schema},t={table} h={host},P={port},u={user},p={password} \
--   --max-load=Threads_running=50 --critical-load=Threads_running=100

-- Or use gh-ost (Facebook's tool, trigger-free)
-- gh-ost --host={host} --database={schema} --table={table} \
--   --alter "ADD PRIMARY KEY (id)" --allow-on-master

Fix 2: Convert to InnoDB Engine

-- Online conversion
ALTER TABLE {schema}.{table} ENGINE=InnoDB, ALGORITHM=INPLACE, LOCK=NONE;

-- Or with pt-osc for large tables:
-- pt-online-schema-change --alter "ENGINE=InnoDB" \
--   D={schema},t={table} h={host},P={port},u={user},p={password}

Fix 3: Convert Charset to utf8mb4

-- Convert table charset (rebuilds table)
ALTER TABLE {schema}.{table} CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- Convert specific columns only (faster for large tables)
ALTER TABLE {schema}.{table}
  MODIFY {column} VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- Check impact before converting:
SELECT TABLE_NAME, TABLE_ROWS, DATA_LENGTH/1024/1024 AS data_mb
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = '{schema}' AND TABLE_COLLATION NOT LIKE 'utf8mb4%';

Fix 4: Drop Redundant Index

-- Drop the shorter/redundant index
DROP INDEX {redundant_idx} ON {schema}.{table};

-- Or rename for safety before dropping
ALTER TABLE {schema}.{table} RENAME INDEX {old_name} TO {old_name}_deprecated;
-- Then DROP after verification period

Fix 5: Plan Schema Migration

For large-scale fixes across many tables, generate a migration script:

-- Generate ALTER TABLE statements for all non-utf8mb4 tables
SELECT CONCAT(
  'ALTER TABLE `', TABLE_SCHEMA, '`.`', TABLE_NAME,
  '` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;'
) AS migration_sql
FROM information_schema.TABLES
WHERE TABLE_SCHEMA NOT IN ('mysql','information_schema','performance_schema','sys')
  AND TABLE_TYPE = 'BASE TABLE'
  AND TABLE_COLLATION NOT LIKE 'utf8mb4%'
  AND TABLE_COLLATION IS NOT NULL;

Best Practices

Fix Priority

PriorityIssueRisk
CriticalMissing PKReplication lag, no row identity
CriticalAuto-inc overflowINSERT failures
HighNon-InnoDB engineNo transactions, no crash recovery
HighRedundant indexesWasted disk + slower writes
MediumNon-utf8mb4Emoji/4-byte character data loss
LowForeign keysCascading locks, deployment friction

Safe Schema Changes

-- For large tables, use online DDL (MySQL 5.6+)
ALTER TABLE large_table ADD INDEX idx_name (col1, col2),
  ALGORITHM=INPLACE, LOCK=NONE;

-- Or use pt-online-schema-change
-- pt-online-schema-change --alter "ADD PRIMARY KEY (id)" D=db,t=table

Quick Reference

CheckQuery
Missing PKSELECT ... WHERE NOT EXISTS (PK constraint)
Engine checkSELECT ... WHERE ENGINE != 'InnoDB'
Charset checkSELECT ... WHERE TABLE_COLLATION NOT LIKE 'utf8mb4%'
Redundant indexesSELECT ... FROM STATISTICS a JOIN STATISTICS b ON shared_prefix
Foreign keysSELECT ... FROM KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_NAME IS NOT NULL
Auto-inc limitSELECT ... WHERE AUTO_INCREMENT > 2000000000

Use this skill for automated schema quality review — catch issues before they become incidents.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.