iamcal/sql-parser
Fast PHP parser for MySQL 5.7 CREATE TABLE statements. Converts schema SQL into an easy-to-use array describing tables, fields, indexes, and options. Designed to deconstruct valid DDL (not validate it); install via Composer.
Install via Composer: composer require iamcal/sql-parser. Instantly parse a MySQL CREATE TABLE statement by creating a new SQLParser instance and calling parse($sql). Access the structured schema via $parser->tables, which returns an associative array keyed by table name. Your first use case is likely inspecting column metadata — e.g., to validate foreign key constraints — by iterating over $parser->tables['users']['fields'] and checking type, null, unsigned, and length.
schema.sql and staging.sql) into separate $parser1->tables and $parser2->tables, then compare fields, indexes, and props to auto-generate ALTER TABLE statements or flag unsafe changes. This is the library’s original use case (Flickr/Tiny Speck production safety checks).after:pull hooks or GitHub Actions, run an Artisan command that parses .sql schema files and enforces rules (e.g., “all timestamps must be nullable()”, “foreign key columns must be unsigned()”). Fail the build if violations occur.$field['unsigned'], $index['type'], and $props['ENGINE'] to tailor column mappings (e.g., map unsigned integer to BigInteger with unsigned() modifier).lex($sql) for lightweight token extraction — e.g., scan a SQL file for all CREATE INDEX statements by inspecting $parser->tokens for 'CREATE' → 'INDEX' sequences, then reconstruct full DDL using raw substrings./* without */) or strings (') cause parsing to stop silently — not ideal for validation. Set $parser->throw_on_bad_syntax = true; to catch these early with iamcal\SQLParserSyntaxException.'type' => 'PRIMARY', unique keys 'UNIQUE', others 'KEY'. Columns are nested in cols as ['name' => 'user_id'], not strings — double-check array access.'ENGINE', 'CHARSET'), but column attributes like 'unsigned' are lowercase. Normalize keys when comparing across environments.YEAR(2), FLOAT(m, n), partitions, or UNION may result in incomplete field lists. Validate $parser->tables[$table]['fields'] exists and has expected count.foreach. Parse once, cache in APCu/Redis, and reuse results across checks.How can I help you explore Laravel packages today?