sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

feat(mysql): Add database analyzer for MySQL

Open kyleconroy opened this issue 3 weeks ago • 0 comments

Summary

  • Adds a MySQL database analyzer that validates queries against a real MySQL database during code generation
  • Creates managed databases (sqlc_managed_{hash}) based on migration content hash for caching
  • Validates query syntax using PrepareContext against real MySQL
  • Fixes several MySQL test cases to use correct MySQL syntax instead of PostgreSQL syntax

Implementation Details

The MySQL analyzer (internal/engine/dolphin/analyzer/analyze.go) follows the same pattern as the SQLite analyzer:

  • For managed databases: creates a temporary database with schema based on migration hash
  • For non-managed databases: connects directly to the configured URI
  • Uses PrepareContext to validate queries against the real database
  • Returns parameter info but defers column inference to the catalog (MySQL prepared statements don't provide reliable column metadata with NULL parameters)

Test Fixes

Fixed MySQL test cases that were using PostgreSQL-specific syntax:

  • Changed $1/$2 placeholders to ? (MySQL syntax)
  • Added AS count aliases to count(*) expressions in CTEs
  • Fixed column references (author_idid, authors.parent_ida.parent_id)
  • Removed PostgreSQL-specific public. schema prefix

Tests restricted to base context (incompatible with real MySQL):

  • mysql_vector - requires MySQL HeatWave (DISTANCE/STRING_TO_VECTOR functions)
  • vet_explain - requires MySQL env vars for explain rules
  • show_warnings - SHOW WARNINGS not supported in prepared statements
  • select_subquery_no_alias - MySQL requires derived tables to have aliases
  • valid_group_by_reference - MySQL's ONLY_FULL_GROUP_BY mode incompatibility
  • insert_select_invalid, invalid_group_by_reference, invalid_table_alias - expected error messages differ

Test plan

  • [x] All existing tests pass (go test ./...)
  • [x] All endtoend tests pass (go test --tags=examples -timeout 20m ./internal/endtoend/...)
  • [x] MySQL analyzer creates managed databases and validates queries

🤖 Generated with Claude Code

kyleconroy avatar Nov 29 '25 22:11 kyleconroy