-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Issue 821] Create database ERD diagrams from SQLAlchemy models (#824)
[Issue 821] Create database ERD diagrams from SQLAlchemy models --------- Co-authored-by: nava-platform-bot <[email protected]>
- Loading branch information
1 parent
32f6656
commit 735e75c
Showing
9 changed files
with
182 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Update database ERD diagrams so that they remain up to date with the application | ||
name: Update Database ERD Diagrams | ||
|
||
on: | ||
pull_request: | ||
paths: | ||
- api/src/db/models/** | ||
- api/bin/create_erds.py | ||
- Makefile | ||
- .github/workflows/ci-erd-diagrams.yml | ||
|
||
defaults: | ||
run: | ||
working-directory: ./api | ||
|
||
# Only trigger one update of the ERD diagrams at a time on the branch. | ||
# If new commits are pushed to the branch, cancel in progress runs and start | ||
# a new one. | ||
concurrency: | ||
group: ${{ github.head_ref }} | ||
cancel-in-progress: true | ||
|
||
|
||
jobs: | ||
update-openapi-docs: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
# Checkout the feature branch associated with the pull request | ||
ref: ${{ github.head_ref }} | ||
|
||
- name: Update OpenAPI spec | ||
run: make create-erds | ||
|
||
- name: Push changes | ||
run: | | ||
git config user.name nava-platform-bot | ||
git config user.email [email protected] | ||
git add --all | ||
# Commit changes (if no changes then no-op) | ||
git diff-index --quiet HEAD || git commit -m "Update database ERD diagrams" | ||
git push |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Generate database schema diagrams from our SQLAlchemy models | ||
import codecs | ||
import logging | ||
import os | ||
import pathlib | ||
from typing import Any | ||
|
||
import pydot | ||
import sadisplay | ||
|
||
import src.logging | ||
from src.db.models import opportunity_models | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
# Construct the path to the folder this file is within | ||
# This gets an absolute path so that where you run the script from won't matter | ||
# and should always resolve to the app/erds folder | ||
ERD_FOLDER = pathlib.Path(__file__).parent.resolve() | ||
|
||
# If we want to generate separate files for more specific groups, we can set that up here | ||
ALL_MODULES = [opportunity_models] | ||
|
||
|
||
def create_erds(modules: Any, file_name: str) -> None: | ||
logger.info("Generating ERD diagrams for %s", file_name) | ||
|
||
items = [] | ||
for module in modules: | ||
items.extend([getattr(module, attr) for attr in dir(module)]) | ||
|
||
description = sadisplay.describe( | ||
items, | ||
show_methods=True, | ||
show_properties=True, | ||
show_indexes=True, | ||
) | ||
|
||
dot_file_name = ERD_FOLDER / f"{file_name}.dot" | ||
|
||
# We create a temporary .dot file which we then convert to a png | ||
with codecs.open(str(dot_file_name), "w", encoding="utf8") as f: | ||
f.write(sadisplay.dot(description)) | ||
|
||
(graph,) = pydot.graph_from_dot_file(dot_file_name) | ||
|
||
png_file_path = ERD_FOLDER / f"{file_name}.png" | ||
logger.info("Creating ERD diagram at %s", png_file_path) | ||
graph.write_png(png_file_path) | ||
|
||
# remove the temporary .dot file | ||
os.remove(dot_file_name) | ||
|
||
|
||
def main() -> None: | ||
with src.logging.init(__package__): | ||
logger.info("Generating ERD diagrams") | ||
|
||
create_erds(ALL_MODULES, "full-schema") |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Overview | ||
This folder contains ERD diagrams representing our database schema for both our postgres DB | ||
|
||
Diagrams can be manually generated by running `make create-erds` from the api folder. | ||
|
||
# Dependencies | ||
If running outside of Docker, you must install `graphviz` (`brew install graphviz`) for this to work, this should be automatically installed as part of the Dockerfile inside Docker. | ||
|
||
# Caveats | ||
The diagrams generated are based on our SQLAlchemy models, and not the database itself, so there are a few differences. | ||
|
||
* Fields that we name different in-code will have a different name | ||
* The table names use the class name | ||
* Property fields are SQLAlchemy only and generally represent relationships (ie. values fetched via a foreign key `join`) | ||
|
||
# Files | ||
![Postgres ERD](full-schema.png) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.