You started looking at a couple dozen JPA entities and decided to take a look at the RDBMS Entity Relationship diagram. You asked your peers and there is none.
This blog post helps you to document your relational database using SchemaSpy in different ways. Via command line, using a Maven plugin, or using Docker so that you don’t have to install SchemaSpy required software.
Java 5+, Graphviz, Maven 2.2.1+ if you would like to generate the entity relationship diagram as part of your build pipeline.
Docker host if you would like to generate the entity relationship diagram using the Docker image this blog post covers. No need to install Java, SchemaSpy, or Graphviz.
For all options, you still need the JDBC driver relevant to your relational database. This post uses postgresql-42.2.16.jar.
Let’s assume you downloaded SchemaSpy to your working directory and the JDBC driver to <working-dir>/jdbc.
Database type (db2, derby, firebird, h2, informix, mariadb, mssql, mysql, ora, orathin, pgsql) A few more if you run java -jar schemaspy-6.1.0.jar -dbhelp | grep "\-t"
-host
The hostname or IP to connect to
-port
The port the RDBMS listens on
-db
The database name
-u
The database user name with read access
-p
The database password
-schemas
The database schema
You might get a console output like:
You should be able to open the report found at ./output/public/index.html. But not the tables relationships because Graphviz is not installed.
SchemaSpy 6.1.0 removed the need for Graphviz by using a new command argument -vizjs.
which now ends successfully:
Browsing ./output/public/index.html shows:
db_dvdrental Tables
db_dvdrental Relationships
Now that you have the static HTML files documenting your relational database, you can share it with your team if you host the output folder ./output in Apache or Nginx. You could host HTML files in an AWS S3 bucket using s3_website.
4. MAVEN PLUGIN
You could also integrate documenting your relational database using Maven in your CI pipeline. Let’s look at this pom.xml file:
And then run:
schemaspy-maven-plugin uses the legacy SchemaSpy 5.0.0. This version requires Graphviz for the Relationships tab to display the relations between the database tables.
db_dvdrental Tables
You could host the site report in Apache or Nginx via their webdav protocol support.
5. DOCKER IMAGE
If your organization is invested in Docker, you could use this image to prevent installing the requirements.
This means the JDBC driver has to be copied in ~/Downloads/schemaspy/jdbc prior to running this container.
Your relational database documentation will be generated in ~/Downloads/schemaspy similar to that of SchemaSpy 6.1.0 discussed earlier.
I had to use --net=host so that the SchemaSpyDocker container could connect to a PostgresDocker container listening on port 5432, and mapped to the Docker host port 5432.
You could also modify the Dockerfile to host the resulting static HTML files in Apache, Nginx, or in an AWS S3 bucket to share the relational database documentation with your team.
6. CONCLUSION
SchemaSpy is a free, Java-based, and easy to use software to document your relational databases.
Documenting your relational databases might increase developer productivity, especially when ramping up new hires.
You can generate relational database ER models with SchemaSpy, Maven, or Docker, depending on the infrastructure your organization is invested in.
Thanks for reading and as always, feedback is very much appreciated. If you found this post helpful and would like to receive updates when content like this gets published, sign up to the newsletter.
7. SOURCE CODE
Accompanying source code for this blog post can be found at:
Orlando L Otero is a Sr Software Engineer Consultant focusing on integration, modernization, cloud adoption and migration, microservices, API design and implementation, and agile delivery.