#!/usr/bin/env node /** * AUTOMATED SQL MIGRATION VALIDATOR * * Validates Supabase migration files for common syntax errors before execution. * Prevents production deployment failures by catching issues early. * * Checks for: * - CREATE INDEX expression syntax (parentheses required) * - Function parameter order (required before optional) * - Proper semicolons and statement endings * - Table and column reference validity * - SQL injection risks in dynamic queries * - RLS policy syntax * - Trigger function definitions * * Created: 2025-12-04 * Last Modified: 2025-12-04 */ const fs = require('fs'); const path = require('path'); const { execSync } = require('child_process'); // ANSI color codes for console output const colors = { reset: '\x1b[0m', red: '\x1b[31m', green: '\x1b[32m', yellow: '\x1b[33m', blue: '\x1b[34m', magenta: '\x1b[35m', cyan: '\x1b[36m', bright: '\x1b[1m' }; // Validation results const results = { passed: 0, failed: 0, warnings: 0, errors: [], warningDetails: [] }; function log(message, color = 'reset') { console.log(`${colors[color]}${message}${colors.reset}`); } function success(message) { log(`✅ ${message}`, 'green'); results.passed++; } function error(message, details = null) { log(`❌ ${message}`, 'red'); results.failed++; results.errors.push({ message, details }); } function warning(message, details = null) { log(`⚠️ ${message}`, 'yellow'); results.warnings++; results.warningDetails.push({ message, details }); } function info(message) { log(`ℹ️ ${message}`, 'blue'); } function section(title) { log(`\n${colors.bright}${colors.cyan}=== ${title} ===${colors.reset}`); } /** * Parse SQL file and extract individual statements */ function parseSQLStatements(sqlContent) { const statements = []; let currentStatement = ''; let inString = false; let stringChar = ''; let inComment = false; let commentType = ''; let parenDepth = 0; for (let i = 0; i < sqlContent.length; i++) { const char = sqlContent[i]; const nextChar = sqlContent[i + 1] || ''; // Handle comments if (!inString && !inComment) { if (char === '-' && nextChar === '-') { inComment = true; commentType = 'line'; continue; } if (char === '/' && nextChar === '*') { inComment = true; commentType = 'block'; i++; // Skip next char continue; } } // Handle exiting comments if (inComment) { if (commentType === 'line' && char === '\n') { inComment = false; commentType = ''; } else if (commentType === 'block' && char === '*' && nextChar === '/') { inComment = false; commentType = ''; i++; // Skip next char } continue; } // Handle strings if (!inComment) { if (!inString && (char === '"' || char === "'")) { inString = true; stringChar = char; } else if (inString && char === stringChar && sqlContent[i - 1] !== '\\') { inString = false; stringChar = ''; } } // Track parentheses depth (for function calls, etc.) if (!inString && !inComment) { if (char === '(') parenDepth++; if (char === ')') parenDepth--; } // Add character to current statement if (!inComment) { currentStatement += char; } // Check for statement end (semicolon at top level) if (!inString && !inComment && parenDepth === 0 && char === ';') { const trimmed = currentStatement.trim(); if (trimmed) { statements.push(trimmed); } currentStatement = ''; } } // Add any remaining statement const trimmed = currentStatement.trim(); if (trimmed) { statements.push(trimmed); } return statements; } /** * Validate CREATE INDEX statements */ function validateCreateIndex(statement, lineNumber) { // Check for CREATE INDEX with expressions const indexRegex = /CREATE\s+(?:UNIQUE\s+)?INDEX\s+(?:\w+\s+)?ON\s+(\w+)\s*\(/i; const match = statement.match(indexRegex); if (match) { const tableName = match[1]; const indexContent = statement.substring(match[0].length - 1); // Include opening paren // Check for CASE expressions without parentheses const caseRegex = /\bCASE\b[^()]*?\bWHEN\b[^()]*?\bTHEN\b[^()]*?(?:\bELSE\b[^()]*?)?\bEND\b/gi; let matchCase; while ((matchCase = caseRegex.exec(indexContent)) !== null) { // Check if this CASE is properly parenthesized const start = matchCase.index; let parenCount = 0; let foundOpening = false; // Look backwards for opening parenthesis for (let i = start - 1; i >= 0; i--) { if (indexContent[i] === ')') parenCount++; if (indexContent[i] === '(') { parenCount--; if (parenCount < 0) { foundOpening = true; break; } } } if (!foundOpening) { error(`CREATE INDEX on table '${tableName}' contains unparenthesized CASE expression`, { statement: statement.substring(0, 100) + '...', line: lineNumber, suggestion: 'Wrap CASE expressions in parentheses: (CASE ... END)' }); } } // Check for function calls without proper parentheses const funcRegex = /\b\w+\s*\([^()]*\([^)]*\)[^)]*\)/g; if (funcRegex.test(indexContent)) { warning(`CREATE INDEX contains nested function calls which may cause issues`, { statement: statement.substring(0, 100) + '...', line: lineNumber }); } } } /** * Validate function definitions */ function validateFunctionDefinition(statement, lineNumber) { const funcRegex = /CREATE\s+(?:OR\s+REPLACE\s+)?FUNCTION\s+(\w+)\s*\(([\s\S]*?)\)\s*RETURNS/i; const match = statement.match(funcRegex); if (match) { const funcName = match[1]; const params = match[2]; // Check parameter order (required before optional) const paramList = params.split(',').map(p => p.trim()); let foundOptional = false; for (const param of paramList) { if (param === '') continue; // Empty parameter const hasDefault = param.includes('DEFAULT') || param.includes(':=') || param.includes('='); if (hasDefault) { foundOptional = true; } else if (foundOptional) { error(`Function '${funcName}' has required parameter after optional parameter`, { statement: `CREATE FUNCTION ${funcName}(${params.substring(0, 100)}...)`, line: lineNumber, suggestion: 'Move all required parameters before optional ones (those with DEFAULT)' }); break; } } // Check for potential SQL injection in dynamic queries if (statement.includes('EXECUTE') || statement.includes('format(')) { const injectionPatterns = [ /EXECUTE\s+[^']*\$[\d]+/i, // Using parameters in EXECUTE without quotes /format\s*\(\s*[^']*\$[\d]+/i // format() with unquoted parameters ]; for (const pattern of injectionPatterns) { if (pattern.test(statement)) { warning(`Function '${funcName}' may have SQL injection risk in dynamic query`, { statement: statement.substring(0, 100) + '...', line: lineNumber, suggestion: 'Use quote_ident() or quote_literal() for dynamic identifiers/values' }); } } } } } /** * Validate RLS policies */ function validateRLSPolicies(statement, lineNumber) { const policyRegex = /CREATE\s+POLICY\s+["']?(\w+)["']?\s+ON\s+(\w+)/i; const match = statement.match(policyRegex); if (match) { const policyName = match[1]; const tableName = match[2]; // Check for USING clause in policies if (!statement.includes('USING') && !statement.includes('WITH CHECK')) { warning(`RLS Policy '${policyName}' on table '${tableName}' missing USING or WITH CHECK clause`, { statement: statement.substring(0, 100) + '...', line: lineNumber }); } // Check for auth.uid() usage without NULL checks if (statement.includes('auth.uid()') && !statement.includes('auth.uid() IS NOT NULL')) { warning(`RLS Policy '${policyName}' uses auth.uid() without NULL check`, { statement: statement.substring(0, 100) + '...', line: lineNumber, suggestion: 'Consider adding: AND auth.uid() IS NOT NULL' }); } } } /** * Validate general SQL syntax */ function validateGeneralSyntax(statement, lineNumber) { // Check for missing semicolons (though we parse by semicolon, some might be missing) if (!statement.trim().endsWith(';') && !statement.trim().toUpperCase().startsWith('COMMENT ON')) { warning('Statement may be missing semicolon', { statement: statement.substring(0, 50) + '...', line: lineNumber }); } // Check for obvious typos in SQL keywords (case-sensitive since SQL is typically uppercase) const typos = [ { pattern: /\bSELET\b/, correct: 'SELECT' }, { pattern: /\bUPDAET\b/, correct: 'UPDATE' }, { pattern: /\bDELEET\b/, correct: 'DELETE' }, { pattern: /\bINSER\b/, correct: 'INSERT' }, { pattern: /\bCREAT\b/, correct: 'CREATE' }, { pattern: /\bDRO\b/, correct: 'DROP' }, { pattern: /\bWERE\b/, correct: 'WHERE' }, { pattern: /\bFORM\b/, correct: 'FROM' }, { pattern: /\bJOING\b/, correct: 'JOIN' }, { pattern: /\bORDRE?\b/, correct: 'ORDER' }, { pattern: /\bGROP?\b/, correct: 'GROUP' } ]; for (const typo of typos) { if (typo.pattern.test(statement)) { error(`Possible typo: found "${statement.match(typo.pattern)[0]}" should be "${typo.correct}"`, { statement: statement.substring(0, 50) + '...', line: lineNumber }); } } // Check for potentially problematic unquoted identifiers // Focus on identifiers that are commonly problematic in PostgreSQL/Supabase context const riskyIdentifiers = ['user', 'order', 'group', 'table', 'index', 'function', 'comment']; // Look for identifiers that appear to be used as column/table names (after FROM, JOIN, ON, etc.) const identifierContextRegex = /(?:\b(?:FROM|JOIN|UPDATE|INTO|ON|REFERENCES)\s+|\.)\b([a-z_][a-z0-9_]*)\b/gi; let match; while ((match = identifierContextRegex.exec(statement)) !== null) { const identifier = match[1].toLowerCase(); if (riskyIdentifiers.includes(identifier)) { // Additional check: make sure it's not part of a longer qualified name or in a function call const beforeMatch = statement.substring(Math.max(0, match.index - 20), match.index); const afterMatch = statement.substring(match.index + match[0].length, match.index + match[0].length + 20); // Skip if it's clearly part of a function call or qualified name if (!beforeMatch.includes('(') && !afterMatch.includes(')') && !beforeMatch.includes('.')) { warning(`Potentially problematic unquoted identifier "${match[1]}"`, { statement: statement.substring(0, 50) + '...', line: lineNumber, suggestion: 'Consider quoting: "${match[1]}"' }); } } } } /** * Validate a single migration file */ function validateMigrationFile(filePath) { section(`Validating: ${path.basename(filePath)}`); if (!fs.existsSync(filePath)) { error(`Migration file not found: ${filePath}`); return; } const sqlContent = fs.readFileSync(filePath, 'utf8'); const statements = parseSQLStatements(sqlContent); info(`Found ${statements.length} SQL statements to validate`); statements.forEach((statement, index) => { const lineNumber = sqlContent.substring(0, sqlContent.indexOf(statement)).split('\n').length; // Run all validations validateCreateIndex(statement, lineNumber); validateFunctionDefinition(statement, lineNumber); validateRLSPolicies(statement, lineNumber); validateGeneralSyntax(statement, lineNumber); }); success(`Validation completed for ${path.basename(filePath)}`); } /** * Try to execute migration against test database */ function testMigrationExecution(filePath) { section('Testing Migration Execution'); info('Note: Dry-run execution testing requires manual setup.'); info('To test execution, run:'); info(` npx supabase db reset # Reset local DB`); info(` npx supabase db push # Apply migrations`); info(` # Check for errors, then:`); info(` npx supabase db reset # Reset again to clean state`); success('Manual execution testing instructions provided'); } /** * Main validation function */ function validateMigrations() { const migrationsDir = path.join(process.cwd(), 'supabase', 'migrations'); if (!fs.existsSync(migrationsDir)) { error(`Migrations directory not found: ${migrationsDir}`); return; } // Get all SQL files in migrations directory const migrationFiles = fs.readdirSync(migrationsDir) .filter(file => file.endsWith('.sql')) .sort() .map(file => path.join(migrationsDir, file)); if (migrationFiles.length === 0) { warning('No migration files found to validate'); return; } info(`Found ${migrationFiles.length} migration files`); // Validate each migration file for (const filePath of migrationFiles) { validateMigrationFile(filePath); } // Test the most recent migration if (migrationFiles.length > 0) { const latestMigration = migrationFiles[migrationFiles.length - 1]; testMigrationExecution(latestMigration); } } function generateReport() { section('Validation Report'); const total = results.passed + results.failed + results.warnings; const passRate = total > 0 ? ((results.passed / total) * 100).toFixed(1) : 0; log(`\n📊 VALIDATION SUMMARY:`); log(`✅ Passed: ${results.passed}`, 'green'); log(`❌ Failed: ${results.failed}`, 'red'); log(`⚠️ Warnings: ${results.warnings}`, 'yellow'); log(`📈 Success Rate: ${passRate}%`, results.failed === 0 ? 'green' : 'red'); if (results.errors.length > 0) { log(`\n🚨 CRITICAL ERRORS FOUND:`); results.errors.forEach((err, index) => { log(`${index + 1}. ${err.message}`, 'red'); if (err.details) { log(` ${JSON.stringify(err.details, null, 2)}`, 'red'); } }); } if (results.warningDetails.length > 0) { log(`\n⚠️ WARNINGS:`); results.warningDetails.forEach((warn, index) => { log(`${index + 1}. ${warn.message}`, 'yellow'); if (warn.details) { log(` ${JSON.stringify(warn.details, null, 2)}`, 'yellow'); } }); } if (results.failed === 0) { log(`\n🎉 ALL VALIDATIONS PASSED!`, 'green'); log(`Migrations are ready for deployment.`, 'green'); } else { log(`\n❌ VALIDATION FAILED`, 'red'); log(`Please fix all critical errors before deploying migrations.`, 'red'); } return results.failed === 0; } // Main execution async function main() { const args = process.argv.slice(2); log(`${colors.bright}${colors.magenta}`); log(`╔══════════════════════════════════════════════════════════════╗`); log(`║ ║`); log(`║ 🛡️ SQL MIGRATION VALIDATOR 🛡️ ║`); log(`║ Preventing Production Disasters Since 2025 ║`); log(`║ ║`); log(`╚══════════════════════════════════════════════════════════════╝`); log(`${colors.reset}`); // Check for specific file argument if (args.length > 0 && args[0].endsWith('.sql')) { const filePath = args[0]; info(`Validating specific file: ${filePath}`); try { validateMigrationFile(filePath); testMigrationExecution(filePath); const success = generateReport(); process.exit(success ? 0 : 1); } catch (err) { error(`Validation failed: ${err.message}`); process.exit(1); } } else { // Validate all migrations info('Starting comprehensive SQL migration validation...'); try { validateMigrations(); const success = generateReport(); // Exit with appropriate code process.exit(success ? 0 : 1); } catch (err) { error(`Validation failed with error: ${err.message}`); console.error(err); process.exit(1); } } } // Export for testing module.exports = { validateMigrationFile, validateCreateIndex, validateFunctionDefinition, validateRLSPolicies, parseSQLStatements, generateReport }; // Run if called directly if (require.main === module) { main(); }