Artisan command
The sql:transpile command transpiles SQL from the CLI. It reads from an argument, a file, or standard input.
Usage
php artisan sql:transpile "SELECT IFNULL(a, 1) FROM t LIMIT 5" --from=mysql --to=sqlsrv --pretty
php artisan sql:transpile --file=dump.sql --from=pgsql --to=mysql
cat query.sql | php artisan sql:transpile --from=mysql --to=postgres
php artisan sql:transpile "SELECT 1" --ast
Options
| Option |
Description |
sql (argument) |
SQL to transpile; omitted when using --file or stdin |
--from= |
Dialect the SQL is written in (default: configured dialect) |
--to= |
Dialect to write (default: same as --from) |
--file= |
Read SQL from a file |
--pretty |
Format output over several lines |
--ast |
Print the parsed expression tree as JSON instead of SQL |
Multiple statements are printed joined with ; and newlines.
Examples
Migrate a dump:
php artisan sql:transpile --file=database/dump-mysql.sql --from=mysql --to=postgres > dump-pg.sql
Inspect the AST while debugging:
php artisan sql:transpile "SELECT a, COUNT(*) FROM t GROUP BY a" --ast | head -n 40
Use in pipes:
echo "SELECT TOP 5 a FROM t" | php artisan sql:transpile --from=tsql --to=mysql
# SELECT a FROM t LIMIT 5