Writing a custom SchemaSpy command for Laravel 4

This week I had to write my first custom command for Laravel 4. In Nestor-QA, Peter and I thought it would be useful to have the database schema being automatically generated with SchemaSpy in our Jenkins box.

Thanks to Artisan this task is much simpler than I thought. The following command creates the schemaspy command.

php artisan command:make SchemaSpyCommand --command=schemaspy

This will create the file app/commands/SchemaSpyCommand.php. And all I had to do was just fill in the options and write the exec command as the Laravel 4 docs explain.

$this->info('Creating SchemaSpy');

$jar = $this->option("jar");
$dbtype = $this->option("dbtype");
$output = $this->option("output");

$commandLine = sprintf("java -jar %s -u none -t %s -o %s", $jar, $dbtype, $output);

$this->info(sprintf("Command line: [%s]", $commandLine));

exec($commandLine);

That’s how my final command looks. Now the final step is integrate it into the application by adding the line below to app/start/artisan.php.

Artisan::add(new SchemaSpyCommand);

And that’s it, running php artisan schemaspy –jar=/opt/schemaspy/schemaSpy_5.0.0.jar –dbtype=app/database/sqlite.properties –output=database-schema creates the database schema docs in the database-schema directory.

Check this gist for the final code.

Happy coding!

Categories: Blog

Tags: Php