postgresqlmavenliquibasetestcontainersmermaid

How to generate mermaid ERD diagram from existing database using maven?


I want to add mermaid ERD diagram to the project documentation but it needs to update automatically whenever schema changes.

Database schema is defined using liquibase and I am using maven for build. During build and test docker image with database (postgresql) is started and all liquibase changes are applied there, so it should be possible to generate ERD from running database or from liquibase script.

I know, there are tools like Mermerd or SchemaCrawler but I cannot find any maven plugin that can integrate them.

Here is how I am starting the dockerised database during tests:

            <plugin>
                <groupId>org.codehaus.gmaven</groupId>
                <artifactId>groovy-maven-plugin</artifactId>
                <version>${maven-groovy-plugin.version}</version>

                <dependencies>
                    <dependency>
                        <groupId>org.testcontainers</groupId>
                        <artifactId>postgresql</artifactId>
                        <version>${testcontainers.version}</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <phase>generate-sources</phase>
                        <configuration>
                            <source>db = new org.testcontainers.containers.PostgreSQLContainer(
                                    "postgres:15.1")
                                    .withUsername("${db.username}")
                                    .withDatabaseName("postgres")
                                    .withPassword("${db.password}");

                            db.start();

                            project.properties.setProperty('db.url', db.getJdbcUrl());</source>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

Solution

  • Thanks to suggestion from @bilak I added schemacrawler maven plugin with following configuration

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.gmaven</groupId>
                <artifactId>groovy-maven-plugin</artifactId>
                <!-- plugin configuration from question -->
            </plugin>
            <plugin>
                <groupId>us.fatehi</groupId>
                <artifactId>schemacrawler-maven-plugin</artifactId>
                <version>16.21.2.2</version>
                <configuration>
                    <url>${db.url}</url>
                    <user>${db.username}</user>
                    <password>${db.password}</password>
                    <command>schema</command>
                    <infolevel>maximum</infolevel>
                    <title>Database Schema</title>
                    <!-- <schemas>PUBLIC.BOOKS</schemas> -->
                    <outputfile>database-schema.svg</outputfile>
                    <outputformat>svg</outputformat>
                    <config>../schemacrawler.config.properties</config>
                    <loadrowcounts>true</loadrowcounts>
                    <loglevel>OFF</loglevel>
                </configuration>
                <dependencies>
                    <!-- If you do not have Graphviz installed, provide a dependency
                      on
                    graphviz-java -->
                    <dependency>
                        <groupId>guru.nidi</groupId>
                        <artifactId>graphviz-java</artifactId>
                        <version>0.18.1</version>
                    </dependency>
                    <dependency>
                        <groupId>org.graalvm.js</groupId>
                        <artifactId>js</artifactId>
                        <version>22.1.0</version>
                    </dependency>
                    <!-- IMPORTANT: Make sure you have a dependency on the JDBC driver.
                    In this example, we are using SQLite as our database server. -->
                    <dependency>
                        <groupId>org.postgresql</groupId>
                        <artifactId>postgresql</artifactId>
                        <version>42.6.0</version>
                    </dependency>
                    <!-- IMPORTANT: Make sure you have a dependency on the SchemaCrawler
                    plugin. In this example, we are using SQLite as our database server. -->
                    <dependency>
                        <groupId>us.fatehi</groupId>
                        <artifactId>schemacrawler-postgresql</artifactId>
                        <version>16.21.2</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>generate-schema</id>
                        <goals>
                            <goal>schemacrawler</goal>
                        </goals>
                        <phase>generate-sources</phase>
                    </execution>
                </executions>
            </plugin>
            <!-- other plugins -->
        </plugins>
    </build>
    

    It's important that the schemacrawler plugin goes after the groovy-maven-plugin It generates diagram in SVG or PNG format - still not mermaid, but always something.