Comprehensive Supabase database management skill for creating migrations, managing RLS policies, optimizing performance, and maintaining database security...
Manage Supabase databases with best practices for migrations, Row Level Security (RLS), performance optimization, and security auditing. This skill ensures all database operations follow the project's conventions and Supabase best practices.
Use this skill for ANY Supabase database operations, including:
The skill automatically:
Create migrations following the project's {timestamp}_{descriptive_name} convention.
Common tasks:
Process:
mcp__supabase__apply_migrationReference: See references/migration_best_practices.md for detailed patterns and examples.
Ensure all tables have proper RLS policies to protect data.
Common tasks:
Process:
mcp__supabase__list_tablesReference: See references/rls_patterns.md for common RLS patterns and security best practices.
Proactively identify and fix security vulnerabilities.
Common tasks:
Process:
mcp__supabase__get_advisors for securityCurrent project issues detected:
player_engagement, daily_challenges, mystery_rewards_logdaily_puzzle_leaderboard, bot_words_for_reviewOptimize database performance with proper indexes and query patterns.
Common tasks:
Process:
mcp__supabase__get_advisors for performanceReference: See references/performance_indexes.md for index patterns and optimization techniques.
Maintain database health and consistency.
Common tasks:
Process:
gen_random_uuid() for UUIDscreated_at, updated_at timestampsUser Request
ā
1. Explore current state (list_tables, list_migrations, execute_sql)
ā
2. Design migration following project patterns
ā
3. Create migration with:
- Schema changes
- Indexes
- RLS policies
- Functions/triggers
- Comments
ā
4. Apply migration (apply_migration)
ā
5. Run advisors (get_advisors for security & performance)
ā
6. Report results and any issues found
User Request for Security Audit
ā
1. Run security advisors (get_advisors type=security)
ā
2. Categorize issues by severity
ā
3. Create migrations to fix each issue:
- Enable RLS on tables
- Add search_path to functions
- Review security definer views
ā
4. Apply fixes
ā
5. Re-run advisors to verify
ā
6. Report results
User Request for Performance Optimization
ā
1. Run performance advisors (get_advisors type=performance)
ā
2. Analyze recommendations
ā
3. Create migrations to add indexes:
- Foreign key indexes
- WHERE clause indexes
- ORDER BY indexes
- Composite indexes
ā
4. Apply migrations
ā
5. Re-run advisors to verify
ā
6. Report improvements
{timestamp}_{descriptive_name}.sql
Examples:
20251228084415_create_daily_puzzle_leaderboard_view20251228090000_enable_rls_on_player_engagement20251228091500_add_indexes_for_game_resultsCREATE TABLE table_name (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
player_id uuid REFERENCES profiles(id) ON DELETE CASCADE,
-- business columns
created_at timestamptz DEFAULT now(),
updated_at timestamptz DEFAULT now()
);
-- Always add indexes for foreign keys
CREATE INDEX table_name_player_id_idx ON table_name(player_id);
-- Always enable RLS
ALTER TABLE table_name ENABLE ROW LEVEL SECURITY;
-- Always add policies
CREATE POLICY "Users can view own data"
ON table_name FOR SELECT
USING (auth.uid() = player_id);
-- Always add update trigger
CREATE TRIGGER update_table_name_updated_at
BEFORE UPDATE ON table_name
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
-- Always add comments
COMMENT ON TABLE table_name IS 'Description of table purpose';
CREATE OR REPLACE FUNCTION function_name()
RETURNS return_type
LANGUAGE plpgsql
SECURITY DEFINER -- Only if needed
SET search_path = public, pg_temp -- ALWAYS set this
AS $$
BEGIN
-- Function logic
END;
$$;
Always use these MCP tools to understand current state before making changes:
mcp__supabase__list_tables - View all tables and their RLS statusmcp__supabase__list_migrations - See existing migrations for naming patternsmcp__supabase__execute_sql - Query database for current statemcp__supabase__apply_migration - Apply new migrationsmcp__supabase__get_advisors - Run security and performance auditsmcp__supabase__search_docs - Look up Supabase documentation when neededBefore completing any database task, verify:
SET search_path = public, pg_tempRequest: "Create a table to track player achievements"
list_tables to understand schema patternsRequest: "Fix RLS on player_engagement table"
get_advisors for securityRequest: "Improve leaderboard query speed"
get_advisors for performanceRequest: "Add premium_expires_at to profiles"
Request: "Run full database security audit"
get_advisors for securityDetailed documentation is available in the references/ directory:
rls_patterns.md - Row Level Security patterns, common policies, security best practicesperformance_indexes.md - Index types, optimization patterns, query tuning, performance monitoringLoad these references as needed for detailed guidance on specific topics.
Always proactively:
If migrations fail:
A successful database operation:
ā Migration applied without errors ā Security advisors show no new issues (or issues are resolved) ā Performance advisors show no critical issues ā RLS is enabled and tested ā Indexes are created for foreign keys and common queries ā Functions have search_path set ā Comments document purpose ā Follows project conventions
This skill ensures your Supabase database remains secure, performant, and maintainable.