Skip to content

Commit

Permalink
readme updates
Browse files Browse the repository at this point in the history
  • Loading branch information
tedivm committed Dec 29, 2023
1 parent bcba5ad commit d231a33
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 9 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/ruff.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Ruff Linting

on:
push:
pull_request:

jobs:
black:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- uses: actions/setup-python@v4
with:
python-version-file: .python-version

- name: Install Dependencies
run: make install

- name: Test Formatting
run: make ruff_check
163 changes: 160 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,172 @@
# paracelsus
# Paracelsus

Paracelsus generates Entity Relationship Diagrams by reading your SQLAlchemy models.

* ERDs can be injected into documentation as [Mermaid Diagrams](https://mermaid.js.org/).
* Paracelsus can be run in CICD to check that databases are up to date.
* ERDs can be created as files in either [Dot](https://graphviz.org/doc/info/lang.html) or Mermaid format.
* DOT files can be used to generate SVG or PNG files, or edited in [GraphViz](https://graphviz.org/) or other editors.

## Installation

## Usage

### Installation

The paracelsus package should be installed in the same environment as your code, as it will be reading your SQLAlchemy base class to generate the diagrams.

```bash
pip install paracelsus
```

## CLI
### Basic CLI Usage

Paracelsus is primarily a CLI application.


```bash
paracelsus --help
```

It has three commands:

* `version` outputs the version of the currently installed `paracelsus` cli.
* `graph` generates a graph and outputs it to `stdout`.
* `inject` inserts the graph into a markdown file.

### Importing Models

SQLAlchemy models have to be imported before they are put into the model registry inside of the base class. This is similar to how [Alembic](https://alembic.sqlalchemy.org/en/latest/) needs models to be imported in order to generate migrations.

The `--import-module` flag can be used to import any python module, which presumably will include one or more SQLAlchemy models inside of it.

```bash
paracelsus graph example_app.models.base:Base \
--import-module "example_app.models.users" \
--import-module "example_app.models.posts" \
--import-module "example_app.models.comments"
```

The `:*` modify can be used to specify that a wild card import should be used. Make sure to wrap the module name in quotes when using this to prevent shell expansion.

```bash
paracelsus graph example_app.models.base:Base --import-module "example_app.models:*"
```

This is equivalent to running this style of python import:

```python
from example_app.models import *
```


### Generate Mermaid Diagrams


> paracelsus graph example_app.models.base:Base --import-module "example_app.models:*"
```text
erDiagram
users {
CHAR(32) id PK
DATETIME created
VARCHAR(100) display_name "nullable"
}
posts {
CHAR(32) id PK
CHAR(32) author FK
TEXT content "nullable"
DATETIME created
BOOLEAN live "nullable"
}
comments {
CHAR(32) id PK
CHAR(32) author FK
CHAR(32) post FK "nullable"
TEXT content "nullable"
DATETIME created
BOOLEAN live "nullable"
}
users ||--o{ posts : author
posts ||--o{ comments : post
users ||--o{ comments : author
```

When run through a Mermaid viewer, such as the ones installed in the markdown viewers of many version control systems, this will turn into a graphic.

```mermaid
erDiagram
users {
CHAR(32) id PK
DATETIME created
VARCHAR(100) display_name "nullable"
}
posts {
CHAR(32) id PK
CHAR(32) author FK
TEXT content "nullable"
DATETIME created
BOOLEAN live "nullable"
}
comments {
CHAR(32) id PK
CHAR(32) author FK
CHAR(32) post FK "nullable"
TEXT content "nullable"
DATETIME created
BOOLEAN live "nullable"
}
users ||--o{ posts : author
posts ||--o{ comments : post
users ||--o{ comments : author
```

### Inject Mermaid Diagrams

Mermaid Diagrams and Markdown work extremely well together, and it's common to place diagrams inside of project documentation. Paracelsus can be used to inject diagrams directly into markdown configuration. It does so by looking for specific tags and placing a code block inside of them, replacing any existing content between the tags.



```markdown
## Schema
<!-- BEGIN_SQLALCHEMY_DOCS -->

<!-- END_SQLALCHEMY_DOCS -->
```

> paracelsus inject db/README.md example_app.models.base:Base --import-module "example_app.models:*"

The `--check` flag can be used to see if the command would make any changes. If the file is already up to date then it will return a status code of `0`, otherwise it will return `1` if changes are needed. This is useful in CI/CD or precommit hook to enforce that documentation is always current.

> paracelsus inject db/README.md example_app.models.base:Base --import-module "example_app.models:*" --check
### Creating Images

GraphViz has a command line tool named [dot](https://graphviz.org/doc/info/command.html) that can be used to turn `dot` graphs into images.

To create an SVG file:

> paracelsus graph example_app.models.base:Base --import-module "example_app.models:*" --format dot | dot -Tsvg > output.svg
To create a PNG file:

> paracelsus graph example_app.models.base:Base --import-module "example_app.models:*" --format dot | dot -Tpng > output.png
![Alt text](./docs/example.png "a title")


## Sponsorship

This project is developed by [Robert Hafner](https://blog.tedivm.com) If you find this project useful please consider sponsoring me using Github!

<center>

[![Github Sponsorship](https://raw.githubusercontent.com/mechPenSketch/mechPenSketch/master/img/github_sponsor_btn.svg)](https://github.com/sponsors/tedivm)

</center>
Binary file added docs/example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ pre-commit:
#

.PHONY: chores
chores: black_fixes dapperdata_fixes tomlsort_fixes
chores: ruff_fixes black_fixes dapperdata_fixes tomlsort_fixes

.PHONY: ruff_fix
.PHONY: ruff_fixes
ruff_fix:
$(PYTHON) -m ruff check --fix
$(PYTHON) -m ruff . --fix

.PHONY: black_fixes
black_fixes:
Expand Down
5 changes: 2 additions & 3 deletions paracelsus/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
TYPE_CHECKING = False
if TYPE_CHECKING:
from typing import Tuple, Union

VERSION_TUPLE = Tuple[Union[int, str], ...]
else:
VERSION_TUPLE = object
Expand All @@ -13,5 +12,5 @@
__version_tuple__: VERSION_TUPLE
version_tuple: VERSION_TUPLE

__version__ = version = "0.1.dev11+g5585946.d20231229"
__version_tuple__ = version_tuple = (0, 1, "dev11", "g5585946.d20231229")
__version__ = version = '0.1.dev12+gbcba5ad.d20231229'
__version_tuple__ = version_tuple = (0, 1, 'dev12', 'gbcba5ad.d20231229')

0 comments on commit d231a33

Please sign in to comment.