Published Wednesday, August 29, 2018 by mjac
Topics: SQL, Perl

Auto-generate a Database Schema Diagram

When building a web application, it's handy to generates a database schema diagram on the fly.

The approach is useful for any stack. This example creates an endpoint in a Catalyst Framework app with a DBIx::Class ORM model.


sub dbdiagram :Local :Args(0) {
    my ($self, $c) = @_;
    return $c->detach('/error404') unless $c->debug;

    my $translator = SQL::Translator->new(
        parser => 'SQL::Translator::Parser::DBIx::Class',
        data   => $c->model('Ezn')->schema,
        producer => 'Diagram',
        producer_args => {
            output_type => 'png',
            title       => "My clever database diagram",
            add_color   => 1,
            font_size   => 'large',
        },
    ) or die SQL::Translator->error;

    $c->res->content_type('image/png');
    $c->res->body($translator->translate);
}

Don't Forget to restrict endpoint to development environment only!

Example database diagram

See also: