In schema-less databases like Cloud Datastore, database structures are defined inside Python code models (using GAE's NDB library). As code bases grow, understanding relationships and attributes across entities becomes difficult. Creating visual UML class diagrams manually is error-prone. A robust solution uses static analysis tools that parse Python source files and generate Graphviz class diagrams automatically.
By writing Python tools that inspect NDB property objects, we can generate dynamic database schemas for documentation.
1. Programmatic Inspection of NDB Classes
We write a script that imports our database models, parses their property attributes, and outputs Graphviz DOT syntax:
# generate_schema_uml.py
from google.cloud import ndb
def export_ndb_to_dot(model_classes):
dot_lines = ["digraph NDB_Schema {", " node [shape=record, fontname=Helvetica];"]
for cls in model_classes:
name = cls.__name__
properties = []
# Loop through NDB properties
for prop_name, prop in cls._properties.items():
prop_type = type(prop).__name__
properties.append(f"{prop_name}: {prop_type}")
fields_str = "\n".join(properties)
dot_lines.append(f' {name} [label="{{{name}|{fields_str}}}"];')
dot_lines.append("}")
return "\n".join(dot_lines)
2. Executing Schema Compilation in CI/CD
The compiled DOT strings are written to text files and processed using Graphviz CLI commands (dot -Tpng schema.dot -o schema.png), keeping repository documentation updated with every schema commit.