[【]日本語](README_ja.md)
The SI-Toolkit for DB Administrator is a tool for DB migration, Java Persistence API (JPA) reverse engineering, and DB design document generation.
Key features include :
- Runs a SQL DDL/DML script to create a DB object using Flyway (schema versioning with https://flywaydb.org/)).
- Hibernate Tools (Use https://hibernate.org/tools/) to generate JPA entity java source files from the database schema.
- Schemaspy (Use http://schemaspy.org/) to generate database design documents containing table lists and ER diagrams from the actual DB metadata.
DBAdministrator requires the following software :
- JDK11
- Docker or DBMS
- Maven
If Docker is not available for any reason, see xxx.
To see how DBAdministrator works, run the sample project.
You can run the sample project with the following commands :
git clone https://github.com/sitoolkit/sit-dba.git
cd sit-dba
# windows
mvnw
# macOS
./mvnw
This maven build results in the following artifacts :
- DB schema
- JPA entities
- DB design documentation
To view them, run the following command :
# Windows
## Open phpPgAdmin
start http://localhost
## Open directory which JPA Entity java files are generated in.
start sit-dba-entity¥target¥generated-sources¥hibernate-tools¥
## Open DB design document
start target¥schemaspy¥index.html
# macOS
## Open phpPgAdmin
open http://localhost
## Open directory which JPA Entity java files are generated in.
open sit-dba-entity/target/generated-sources/hibernate-tools
## Open DB design document
open target/schemaspy/index.html
The build process for the above Maven commands includes four phases :
- DB Migration
- Reverse Engineering
- Testing and Packaging
- Generating DB Design Documents
During the DB migration phase, SQL scripts are executed by Flyway. The script files are located in the sit-dba-migration/src/main/resources/db/migration directory, which is defined as the location of the Flyway's resources.
The parameters for connecting the DB are defined in the root pom. xml and passed to Flyway in the Maven build process.
<properties>
<db.name>postgres</db.name>
<db.host>localhost</db.host>
<db.port>5432</db.port>
<db.jdbc.url>jdbc:postgresql://${db.host}:${db.port}/${db.name}?currentSchema=${db.schema}</db.jdbc.url>
<db.username>postgres</db.username>
<db.password>postgres</db.password>
<db.schema>sit_db</db.schema>
</properties>
These parameters are used in all phases.
During the reverse engineering phase, Hibernate Tools generates java files that define JPA entity classes based on DB schema information (tables and relationships). The output directory is the additional java source directory for the sit-dba-entity/target/generated-sources/hibernate-tools, sit-dba-entity project.
During the testing and packaging phase, Maven runs JUnit tests for sit-dba-sample-spring.
In DB Design Document Generation, document files (html, css, javascript) are generated by Schemaspy and implemented as a docker-compose service.
There are three Maven projects in the DBAdministrator :
- sit-dba-migration
- sit-dba-entity
- sit-dba-sample-spring
This project is for maintaining DDL/DML scripts to create DB schemas for applications. This project is packaged in an executable jar that supports Flyway CLI (https://flywaydb.org/documentation/commandline/)).
java -jar sit-db-migration-xxx-SNAPSHOT.jar --config=flyway.properties
The flyway. properties specification is [here] (defined in https://flywaydb.org/documentation/configfiles)).
This project is packaged into a generic library jar that contains JPA entities and is used in the application java project.
This project uses Spring Boot and Spring Data Spring Boot and Spring Data JPA.
The Maven archetype allows you to use sit-dba as your project. The basic use of sit-dba is as follows :
- Generate the project.
- Modify the DB connection parameters.
- Modify the DDL/DML script.
- Modify the target table generated for the JPA entity.
You can generate a new project using the following maven new project :
(Replace xxx
, yyy
with groupId and artifactId.)
mvn archetype:generate -B \
-DarchetypeGroupId=io.sitoolkit.dba \
-DarchetypeArtifactId=sit-dba-archetype \
-DarchetypeVersion=1.1.0 \
-DgroupId=xxx \
-DartifactId=yyy
After the project is generated, change the DB connection parameter in pom. xml of the root project to the development environment.
Modify the DDL/DML script to create the DB schema in the yyy-migration/src/main/resources/db/migration directory. The name of the script file follows the Flyway rule .
You can run these script files with the following commands :
# Widows
mvnw -f yyy-migration -P migrate
# macOS
./mvnw -f yyy-migration -P migrate
Yyy - Modify the entity / hibernate. reveng. xml file to add target tables to be generated for JPA entities.
<hibernate-reverse-engineering>
<! -- - Since this is a sample, delete it after modifying the DB schema -->
<table-filter match-name="person" package="io.sitoolkit.dba.domain.person"></table-filter>
<! -- - Add target tables and their entity packages -->
<table-filter match-name="YOUR TABLE" package="YOUR PACKAGE OF ENTITY CLASS"></table-filter>
</hibernate-reverse-engineering>
After modification, you can generate entities using the command.
# Windows
mvnw -f yyy-entity -P reveng
# macOS
./mvnw -f yyy-entity -P reveng