-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ee1021e
Showing
10 changed files
with
1,275 additions
and
0 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,169 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
share/python-wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
*.py,cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
cover/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
db.sqlite3-journal | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
.pybuilder/ | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# IPython | ||
profile_default/ | ||
ipython_config.py | ||
|
||
# pyenv | ||
# For a library or package, you might want to ignore these files since the code is | ||
# intended to run in multiple environments; otherwise, check them in: | ||
# .python-version | ||
|
||
# pipenv | ||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. | ||
# However, in case of collaboration, if having platform-specific dependencies or dependencies | ||
# having no cross-platform support, pipenv may install dependencies that don't work, or not | ||
# install all needed dependencies. | ||
#Pipfile.lock | ||
|
||
# poetry | ||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. | ||
# This is especially recommended for binary packages to ensure reproducibility, and is more | ||
# commonly ignored for libraries. | ||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control | ||
#poetry.lock | ||
|
||
# pdm | ||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. | ||
#pdm.lock | ||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it | ||
# in version control. | ||
# https://pdm.fming.dev/#use-with-ide | ||
.pdm.toml | ||
|
||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm | ||
__pypackages__/ | ||
|
||
# Celery stuff | ||
celerybeat-schedule | ||
celerybeat.pid | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.dmypy.json | ||
dmypy.json | ||
|
||
# Pyre type checker | ||
.pyre/ | ||
|
||
# pytype static type analyzer | ||
.pytype/ | ||
|
||
# Cython debug symbols | ||
cython_debug/ | ||
|
||
# PyCharm | ||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can | ||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore | ||
# and can be added to the global gitignore or merged into this file. For a more nuclear | ||
# option (not recommended) you can uncomment the following to ignore the entire idea folder. | ||
#.idea/ | ||
|
||
# Configuration file | ||
config.json | ||
|
||
# UML diagram | ||
.puml | ||
|
||
# OS specific | ||
.DS_Store |
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,13 @@ | ||
{ | ||
"python.testing.pytestArgs": [ | ||
"-s", | ||
"--cov=.", | ||
"tests" | ||
], | ||
"python.testing.unittestEnabled": false, | ||
"python.testing.pytestEnabled": true, | ||
"[python]": { | ||
"editor.defaultFormatter": "ms-python.black-formatter" | ||
}, | ||
"python.formatting.provider": "none" | ||
} |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Francesco Lumachi <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,24 @@ | ||
# salesforce-erd | ||
A tool to build your Salesforce instance Entity-Relationship Diagram. | ||
|
||
## How to use | ||
This tool is designed as a script to produce a [plantuml](https://plantuml.com/) text diagram based on Salesforce metadata. To achieve an actual drawing (an svg/png/pdf file) use the produced txt file as input for [plantuml tool](https://plantuml.com/starting). | ||
|
||
## How to run | ||
1. This project use [poetry](https://python-poetry.org/) to organize dependencies. Install it in your system before use. | ||
2. Duplicate `config.json.template` and rename it in `config.json`. Then fill the requested variables: | ||
a. `user`, `password`, `token` to connect to your Salesforce instance, | ||
b. `diagram_name` is the name that you want to give to the UML diagram, | ||
c. `tables` is the subset of entities that you want in your diagram (the full Salesforce instance it will have easily too much entities to be shown). | ||
3. Run `poetry run python build_erd.py` to execute the script. The first run will be longer because [poetry](https://python-poetry.org/) will create a dedicated environment with the necessary dependencies. Follow the provided instruction in order to build your diagram. | ||
4. Output will be written into `salesforce_erd.puml` file. From here, you can invoke [plantuml](https://plantuml.com/) to transform the diagram in a visual format. | ||
|
||
### Example | ||
Refer to [plantuml](https://plantuml.com/) documentation, but you probably need to tweak some option, like the maximum file size: | ||
```bash | ||
export PLANTUML_LIMIT_SIZE=32767 | ||
plantuml -tsvg salesforce_erd.puml | ||
``` | ||
|
||
## Contributing | ||
This tool is just a POC and needs lot of improvements in testing and usability. If this software do not satisfy you "as-is", consider improve it and send a pull request. Thank you! |
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,128 @@ | ||
import click | ||
import pandas as pd | ||
|
||
from salesforce_erd import ( | ||
fields_of, | ||
filter_tables, | ||
get_instance, | ||
read_config, | ||
show_tables, | ||
) | ||
|
||
|
||
@click.command() | ||
@click.option( | ||
"--config-file", | ||
"-c", | ||
default="config.json", | ||
help="Path to the JSON configuration file", | ||
) | ||
def build_erd(config_file): | ||
conf = read_config(config_file) | ||
|
||
# Read Salesforce metadata | ||
sf = get_instance(conf.get("user"), conf.get("password"), conf.get("token")) | ||
tables_meta = show_tables(sf) | ||
|
||
# Filter tables | ||
tables = conf.get("tables", []) | ||
if len(tables) > 0: | ||
tables_meta = filter_tables(tables_meta, names=tables) | ||
|
||
# Generate PlantUML text for class diagram | ||
plantuml_code = write_header(conf.get("diagram_name")) | ||
|
||
for table in tables_meta.name: | ||
# Get entity attributes | ||
columns_meta = fields_of(table, sf, metadata=True) | ||
columns_meta.soapType = columns_meta.soapType.str[4:] | ||
|
||
# Extract relations | ||
relation_meta = get_relations(columns_meta) | ||
|
||
# Entity | ||
plantuml_code += write_entity(table, columns_meta, relation_meta) | ||
|
||
# Relationship | ||
plantuml_code += write_relationship(tables, table, relation_meta) | ||
|
||
# Close E-R loop | ||
plantuml_code += "\n" | ||
|
||
# Footer | ||
plantuml_code += "@enduml" | ||
|
||
# Write output to file | ||
with open("salesforce_erd.puml", "w") as file: | ||
file.write(plantuml_code) | ||
|
||
|
||
def write_relationship(tables, table, relation_meta) -> str: | ||
plantuml_code = "" | ||
relation_meta = ( | ||
relation_meta.groupby("referenceTo")["relationshipName"] | ||
.agg(list) | ||
.str.join(", ") | ||
.reset_index() | ||
) # Aggregate relations targetting same entity | ||
for _, relation in relation_meta.explode("referenceTo").iterrows(): | ||
# Drop recursive relation and only specified table | ||
if (table != relation["referenceTo"]) and ( | ||
relation["referenceTo"] in tables if len(tables) > 0 else True | ||
): | ||
plantuml_code += f"{table} -left-> {relation['referenceTo']}\n" | ||
return plantuml_code | ||
|
||
|
||
def write_entity(table, columns_meta, relation_meta) -> str: | ||
plantuml_code = f"class {table} {{\n" | ||
|
||
# Attributes loop | ||
for _, attribute in columns_meta.sort_values(by="name").iterrows(): | ||
method_or_field = "{method} #" if attribute["calculated"] else "{field} +" | ||
bold_open_if_ID = "<b>" if attribute["soapType"] == "ID" else "" | ||
attribute_type = ( | ||
attribute["soapType"] | ||
if attribute["type"].lower() == attribute["soapType"].lower() | ||
else f"{attribute['type']}<{attribute['soapType']}>" | ||
) | ||
bold_close_if_ID = "</b>" if attribute["soapType"] == "ID" else "" | ||
references = relation_meta.loc[ | ||
relation_meta["name"] == attribute["name"], "referenceTo" | ||
] | ||
if len(references) == 0: | ||
references = ["No reference found."] | ||
reference_to = ( | ||
":\n" + ", \n".join(map(lambda r: f"<b>{r}</b>", references)) | ||
if attribute["type"].lower() == "reference" | ||
else "" | ||
) | ||
plantuml_code += f" {method_or_field}{bold_open_if_ID}{attribute['name']}: {attribute_type}{bold_close_if_ID}{reference_to}\n" | ||
|
||
plantuml_code += f"}}\n" | ||
return plantuml_code | ||
|
||
|
||
def get_relations(columns_meta) -> pd.DataFrame: | ||
relation_meta = columns_meta.loc[ | ||
columns_meta.referenceTo.apply(len) > 0, | ||
["name", "relationshipName", "referenceTo"], | ||
] | ||
# Drop relationship with no name | ||
relation_meta = relation_meta.dropna(subset="relationshipName") | ||
# Flatten polymorphic relations | ||
relation_meta = relation_meta.explode("referenceTo") | ||
return relation_meta | ||
|
||
|
||
def write_header(diagram_name) -> str: | ||
plantuml_code = ( | ||
f"@startuml {diagram_name} - {pd.Timestamp.now().isoformat()[:10]}\n" | ||
) | ||
plantuml_code += "skinparam linetype ortho\n" # °90 degree angle only for arrows | ||
plantuml_code += "hide empty methods\n" | ||
return plantuml_code | ||
|
||
|
||
if __name__ == "__main__": | ||
build_erd() |
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,7 @@ | ||
{ | ||
"user": "", | ||
"password": "", | ||
"token": "", | ||
"diagram_name": "", | ||
"tables": [] | ||
} |
Oops, something went wrong.