Generate SVG Entity Relationship Diagrams from PostgreSQL database dumps
Project description
Python ERD Generator
A Python application that parses SQL dump files and generates Entity Relationship Diagrams (ERDs) using Graphviz.
Features
- Parse PostgreSQL dump files to extract table definitions and relationships
- Generate interactive SVG Entity Relationship Diagrams
- Automatic color coding for tables with accessible color palette
- Support for complex SQL structures including foreign keys, constraints, and various data types
- Table filtering to exclude temporary/utility tables
- Comprehensive test suite with >90% code coverage
Installation
- Install pypgsvg:
pip install .
- Ensure Graphviz is installed on your system:
- macOS:
brew install graphviz - Ubuntu/Debian:
sudo apt-get install graphviz - Windows: Download from https://graphviz.org/download/
- macOS:
Usage
Basic Usage
Generate an ERD from your SQL dump file:
pypgsvg your_database.sql
This will create an SVG file with the same name as your input file (e.g., your_database_erd.svg).
Example Output
Here's an example of the generated ERD from a sample database schema with users, posts, and comments:
Simple Blog Schema - showing basic relationships between users, posts, and comments
For more complex databases, pypgsvg can handle extensive schemas with many tables and relationships:
Complex Database Schema - demonstrating pypgsvg's ability to visualize large, real-world database structures
View the interactive SVG diagrams:
The diagram shows:
- Tables as nodes with their column definitions
- Foreign key relationships as directed edges between tables
- Automatic color coding for visual distinction
- Accessible color palette with proper contrast for readability
Usage
Specify a custom output filename:
pypgsvg your_database_dump.sql --output custom_diagram.svg
View the diagram immediately after generation:
pypgsvg your_database_dump.sql --view
Python API Usage
For programmatic use:
from src.pypgsvg import parse_sql_dump, generate_erd_with_graphviz
# Load SQL dump
with open("your_database_dump.sql", "r", encoding='utf-8') as file:
sql_content = file.read()
# Parse tables and relationships
tables, foreign_keys, errors = parse_sql_dump(sql_content)
# Generate ERD
if not errors:
generate_erd_with_graphviz(tables, foreign_keys, "database_diagram")
print("ERD generated successfully!")
else:
print("Parsing errors found:", errors)
Testing
Run the complete test suite with coverage:
# Run all tests with coverage
pytest
# Run specific test categories
pytest -m unit # Unit tests only
pytest -m integration # Integration tests only
# Run with verbose output
pytest -v
# Generate HTML coverage report
pytest --cov-report=html
open htmlcov/index.html # View coverage report
Project Structure
├── src/
│ └── create_graph.py # Main application code
├── tests/
│ ├── conftest.py # Test fixtures and configuration
│ ├── test_utils.py # Tests for utility functions
│ ├── test_parser.py # Tests for SQL parsing
│ ├── test_erd_generation.py # Tests for ERD generation
│ └── test_integration.py # Integration tests
├── requirements.txt # Python dependencies
├── pyproject.toml # pytest configuration
└── README.md # This file
Configuration
Table Exclusion
The application automatically excludes tables matching certain patterns (defined in should_exclude_table):
- Views (
vw_) - Backup tables (
bk) - Temporary fix tables (
fix) - Duplicate tables (
dups,duplicates) - Match tables (
matches) - Version logs (
versionlog) - Old tables (
old) - Member data (
memberdata)
Color Palette
The ERD uses an accessible color palette with automatic contrast calculation for text readability following WCAG guidelines.
Supported SQL Features
CREATE TABLEstatements with various column typesCREATE TABLE IF NOT EXISTSALTER TABLE ... ADD CONSTRAINT ... FOREIGN KEY- Quoted identifiers
- Complex data types (numeric, timestamp, jsonb, etc.)
- Multiple constraint variations
Error Handling
The application includes comprehensive error handling for:
- Malformed SQL syntax
- Missing table references in foreign keys
- Unicode encoding issues
- File reading errors
Contributing
- Follow PEP 8 style guidelines
- Write tests for new functionality
- Maintain >90% test coverage
- Use type hints where appropriate
- Update documentation as needed
Dependencies
graphviz>=0.20.1- For generating diagramspytest>=7.4.0- Testing frameworkpytest-cov>=4.1.0- Coverage reportingpytest-mock>=3.11.0- Mocking utilities
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pypgsvg-1.0.3.tar.gz.
File metadata
- Download URL: pypgsvg-1.0.3.tar.gz
- Upload date:
- Size: 93.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dbe1bca0b6b65b82098ce9c21fd74416083473c7cffd48c21706e4c7413a051a
|
|
| MD5 |
01e8f34b6714df9ad6efd0d966da7e05
|
|
| BLAKE2b-256 |
f827a6687280328bbe4ca5db28eb4d1e44b580e1bd80a6352accb732838705ae
|
Provenance
The following attestation bundles were made for pypgsvg-1.0.3.tar.gz:
Publisher:
publish.yml on blackburnd/pypgsvg
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pypgsvg-1.0.3.tar.gz -
Subject digest:
dbe1bca0b6b65b82098ce9c21fd74416083473c7cffd48c21706e4c7413a051a - Sigstore transparency entry: 252002781
- Sigstore integration time:
-
Permalink:
blackburnd/pypgsvg@d1cdce8ea1e5115dc1a3b26c5ce87c034d1f6d50 -
Branch / Tag:
refs/tags/v1.0.3 - Owner: https://github.com/blackburnd
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d1cdce8ea1e5115dc1a3b26c5ce87c034d1f6d50 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pypgsvg-1.0.3-py3-none-any.whl.
File metadata
- Download URL: pypgsvg-1.0.3-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
69a8d208947b418975f68779bfb5e92959c19a18431c440d8b83fb28ac34c0de
|
|
| MD5 |
58c3bb593b71135aecaf49a6e61466d1
|
|
| BLAKE2b-256 |
f947a225bcf25d4a7fd6927f631a94eac990cca00fd0ab307f297a72e8a2a2ba
|
Provenance
The following attestation bundles were made for pypgsvg-1.0.3-py3-none-any.whl:
Publisher:
publish.yml on blackburnd/pypgsvg
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pypgsvg-1.0.3-py3-none-any.whl -
Subject digest:
69a8d208947b418975f68779bfb5e92959c19a18431c440d8b83fb28ac34c0de - Sigstore transparency entry: 252002786
- Sigstore integration time:
-
Permalink:
blackburnd/pypgsvg@d1cdce8ea1e5115dc1a3b26c5ce87c034d1f6d50 -
Branch / Tag:
refs/tags/v1.0.3 - Owner: https://github.com/blackburnd
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d1cdce8ea1e5115dc1a3b26c5ce87c034d1f6d50 -
Trigger Event:
push
-
Statement type: