comdab 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- comdab-0.1.0/.github/workflows/release.yml +36 -0
- comdab-0.1.0/.github/workflows/test.yml +34 -0
- comdab-0.1.0/.gitignore +174 -0
- comdab-0.1.0/.pre-commit-config.yaml +21 -0
- comdab-0.1.0/.python-version +1 -0
- comdab-0.1.0/LICENSE +21 -0
- comdab-0.1.0/PKG-INFO +87 -0
- comdab-0.1.0/README.md +77 -0
- comdab-0.1.0/docs/Makefile +20 -0
- comdab-0.1.0/docs/conf.py +39 -0
- comdab-0.1.0/docs/index.rst +170 -0
- comdab-0.1.0/docs/reference.rst +20 -0
- comdab-0.1.0/pyproject.toml +29 -0
- comdab-0.1.0/src/comdab/__init__.py +25 -0
- comdab-0.1.0/src/comdab/build.py +238 -0
- comdab-0.1.0/src/comdab/compare.py +182 -0
- comdab-0.1.0/src/comdab/exceptions.py +18 -0
- comdab-0.1.0/src/comdab/main.py +99 -0
- comdab-0.1.0/src/comdab/models/__init__.py +34 -0
- comdab-0.1.0/src/comdab/models/base.py +5 -0
- comdab-0.1.0/src/comdab/models/column.py +31 -0
- comdab-0.1.0/src/comdab/models/constraint.py +101 -0
- comdab-0.1.0/src/comdab/models/function.py +22 -0
- comdab-0.1.0/src/comdab/models/index.py +24 -0
- comdab-0.1.0/src/comdab/models/schema.py +33 -0
- comdab-0.1.0/src/comdab/models/sequence.py +32 -0
- comdab-0.1.0/src/comdab/models/table.py +37 -0
- comdab-0.1.0/src/comdab/models/trigger.py +22 -0
- comdab-0.1.0/src/comdab/models/type.py +150 -0
- comdab-0.1.0/src/comdab/models/view.py +24 -0
- comdab-0.1.0/src/comdab/path.py +161 -0
- comdab-0.1.0/src/comdab/py.typed +0 -0
- comdab-0.1.0/src/comdab/report.py +37 -0
- comdab-0.1.0/src/comdab/source.py +11 -0
- comdab-0.1.0/src/comdab/specific/__init__.py +0 -0
- comdab-0.1.0/src/comdab/specific/postgresql/__init__.py +0 -0
- comdab-0.1.0/src/comdab/specific/postgresql/build.py +100 -0
- comdab-0.1.0/src/comdab/specific/postgresql/exclude_constraints.py +82 -0
- comdab-0.1.0/src/comdab/specific/postgresql/functions.py +39 -0
- comdab-0.1.0/src/comdab/specific/postgresql/sequences.py +53 -0
- comdab-0.1.0/src/comdab/specific/postgresql/triggers.py +45 -0
- comdab-0.1.0/src/comdab/specific/postgresql/views.py +40 -0
- comdab-0.1.0/src/comdab/utils.py +43 -0
- comdab-0.1.0/tests/__init__.py +0 -0
- comdab-0.1.0/tests/_mock_module.py +7 -0
- comdab-0.1.0/tests/conftest.py +43 -0
- comdab-0.1.0/tests/test_build.py +553 -0
- comdab-0.1.0/tests/test_compare.py +367 -0
- comdab-0.1.0/tests/test_main.py +67 -0
- comdab-0.1.0/tests/test_models.py +134 -0
- comdab-0.1.0/tests/test_specific/__init__.py +0 -0
- comdab-0.1.0/tests/test_specific/test_postgresql.py +846 -0
- comdab-0.1.0/tests/test_utils.py +33 -0
- comdab-0.1.0/uv.lock +543 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
name: Upload Python Package
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
|
|
7
|
+
permissions:
|
|
8
|
+
contents: read
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
pypi-publish:
|
|
12
|
+
name: Upload release to PyPI
|
|
13
|
+
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
|
|
16
|
+
environment:
|
|
17
|
+
name: pypi
|
|
18
|
+
url: https://pypi.org/p/comdab
|
|
19
|
+
|
|
20
|
+
permissions:
|
|
21
|
+
id-token: write
|
|
22
|
+
|
|
23
|
+
steps:
|
|
24
|
+
- uses: actions/checkout@v4
|
|
25
|
+
|
|
26
|
+
- name: Install uv
|
|
27
|
+
uses: astral-sh/setup-uv@v6
|
|
28
|
+
with:
|
|
29
|
+
version: "0.7.13"
|
|
30
|
+
enable-cache: true
|
|
31
|
+
|
|
32
|
+
- name: "Build wheel"
|
|
33
|
+
run: uv build
|
|
34
|
+
|
|
35
|
+
- name: Publish package distributions to PyPI
|
|
36
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
name: Run Unit Tests
|
|
2
|
+
|
|
3
|
+
on: [push]
|
|
4
|
+
|
|
5
|
+
jobs:
|
|
6
|
+
build:
|
|
7
|
+
runs-on: ubuntu-latest
|
|
8
|
+
|
|
9
|
+
strategy:
|
|
10
|
+
matrix:
|
|
11
|
+
python-version:
|
|
12
|
+
- "3.12"
|
|
13
|
+
- "3.13"
|
|
14
|
+
- "3.14.0-beta.3"
|
|
15
|
+
|
|
16
|
+
steps:
|
|
17
|
+
- uses: actions/checkout@v4
|
|
18
|
+
|
|
19
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
20
|
+
uses: actions/setup-python@v5
|
|
21
|
+
with:
|
|
22
|
+
python-version: ${{ matrix.python-version }}
|
|
23
|
+
|
|
24
|
+
- name: Install uv
|
|
25
|
+
uses: astral-sh/setup-uv@v6
|
|
26
|
+
with:
|
|
27
|
+
version: "0.7.13"
|
|
28
|
+
enable-cache: true
|
|
29
|
+
|
|
30
|
+
- name: Install the project
|
|
31
|
+
run: uv sync --locked --dev
|
|
32
|
+
|
|
33
|
+
- name: Run tests
|
|
34
|
+
run: uv run pytest tests
|
comdab-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py,cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
#uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
#poetry.lock
|
|
109
|
+
|
|
110
|
+
# pdm
|
|
111
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
112
|
+
#pdm.lock
|
|
113
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
114
|
+
# in version control.
|
|
115
|
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
|
116
|
+
.pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
121
|
+
__pypackages__/
|
|
122
|
+
|
|
123
|
+
# Celery stuff
|
|
124
|
+
celerybeat-schedule
|
|
125
|
+
celerybeat.pid
|
|
126
|
+
|
|
127
|
+
# SageMath parsed files
|
|
128
|
+
*.sage.py
|
|
129
|
+
|
|
130
|
+
# Environments
|
|
131
|
+
.env
|
|
132
|
+
.venv
|
|
133
|
+
env/
|
|
134
|
+
venv/
|
|
135
|
+
ENV/
|
|
136
|
+
env.bak/
|
|
137
|
+
venv.bak/
|
|
138
|
+
|
|
139
|
+
# Spyder project settings
|
|
140
|
+
.spyderproject
|
|
141
|
+
.spyproject
|
|
142
|
+
|
|
143
|
+
# Rope project settings
|
|
144
|
+
.ropeproject
|
|
145
|
+
|
|
146
|
+
# mkdocs documentation
|
|
147
|
+
/site
|
|
148
|
+
|
|
149
|
+
# mypy
|
|
150
|
+
.mypy_cache/
|
|
151
|
+
.dmypy.json
|
|
152
|
+
dmypy.json
|
|
153
|
+
|
|
154
|
+
# Pyre type checker
|
|
155
|
+
.pyre/
|
|
156
|
+
|
|
157
|
+
# pytype static type analyzer
|
|
158
|
+
.pytype/
|
|
159
|
+
|
|
160
|
+
# Cython debug symbols
|
|
161
|
+
cython_debug/
|
|
162
|
+
|
|
163
|
+
# PyCharm
|
|
164
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
165
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
166
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
167
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
168
|
+
#.idea/
|
|
169
|
+
|
|
170
|
+
# Ruff stuff:
|
|
171
|
+
.ruff_cache/
|
|
172
|
+
|
|
173
|
+
# PyPI configuration file
|
|
174
|
+
.pypirc
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
repos:
|
|
2
|
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
|
3
|
+
rev: v3.2.0
|
|
4
|
+
hooks:
|
|
5
|
+
- id: trailing-whitespace
|
|
6
|
+
- id: end-of-file-fixer
|
|
7
|
+
- id: check-yaml
|
|
8
|
+
- id: check-added-large-files
|
|
9
|
+
|
|
10
|
+
- repo: https://github.com/astral-sh/ruff-pre-commit
|
|
11
|
+
rev: v0.11.11
|
|
12
|
+
hooks:
|
|
13
|
+
- id: ruff
|
|
14
|
+
args: ["--fix"]
|
|
15
|
+
- id: ruff-format
|
|
16
|
+
|
|
17
|
+
- repo: https://github.com/RobertCraigie/pyright-python
|
|
18
|
+
rev: v1.1.402
|
|
19
|
+
hooks:
|
|
20
|
+
- id: pyright
|
|
21
|
+
hook-stage: pre-push
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.13
|
comdab-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Loïc Simon
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
comdab-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: comdab
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Compare Database Schemas
|
|
5
|
+
License-File: LICENSE
|
|
6
|
+
Requires-Python: >=3.12
|
|
7
|
+
Requires-Dist: pydantic>=2.0
|
|
8
|
+
Requires-Dist: sqlalchemy>=2.0
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
|
|
11
|
+
# comdab — Compare Database Schemas
|
|
12
|
+
|
|
13
|
+
_comdab_ allows you to compare in depth two database schemas to find
|
|
14
|
+
all differences: missing columns, different nullabilities or defaults,
|
|
15
|
+
slight changes in function or triggers definitions...
|
|
16
|
+
|
|
17
|
+
> [!WARNING]
|
|
18
|
+
>
|
|
19
|
+
> _comdab_ is still in development, only tested with PostgreSQL 14 to date.
|
|
20
|
+
>
|
|
21
|
+
> All feedback and contributions are welcome!
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
Use [pip](https://pip.pypa.io) to install comdab:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install comdab
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Requirements
|
|
32
|
+
|
|
33
|
+
- Python `>= 3.12`
|
|
34
|
+
- [sqlalchemy](https://pypi.org/project/sqlalchemy) `>= 2.0`
|
|
35
|
+
- [pydantic](https://pypi.org/project/pydantic) `>= 2.0`
|
|
36
|
+
|
|
37
|
+
## Goals
|
|
38
|
+
|
|
39
|
+
_comdab_ is especially useful in combination with a migration tool like
|
|
40
|
+
[Alembic](https://alembic.sqlalchemy.org), to make sure that applying a
|
|
41
|
+
migration to an existing database
|
|
42
|
+
and creating a new database from scratch produce **the exact same schema**.
|
|
43
|
+
|
|
44
|
+
Indeed, while migration tools can auto-detect model changes and write
|
|
45
|
+
automatically the migrations to apply to pre-existing databases, it does not
|
|
46
|
+
cover the whole schema complexity, and does not prevent human errors (like
|
|
47
|
+
modifying the model without re-generating migrations, or manually editing
|
|
48
|
+
migrations in a slightly wrong way...)
|
|
49
|
+
|
|
50
|
+
This may cause dangerous and hard to spot bugs, especially if your unit tests
|
|
51
|
+
and CI run on a fresh database created from the Python-written model, and not
|
|
52
|
+
on pre-existing databases with the new migration applied.
|
|
53
|
+
|
|
54
|
+
By running _comdab_, you can ensure the two are the nearly-exact same.
|
|
55
|
+
|
|
56
|
+
_comdab_ stands on the shoulders of the [SQLAlchemy](https://sqlalchemy.org)
|
|
57
|
+
library, used to connect to the database and for most of schema introspection.
|
|
58
|
+
|
|
59
|
+
## Usage
|
|
60
|
+
|
|
61
|
+
```py
|
|
62
|
+
from comdab import compare_databases
|
|
63
|
+
from sqlalchemy import create_engine
|
|
64
|
+
|
|
65
|
+
engine_1 = create_engine("postgresql://user:pass@host/foo")
|
|
66
|
+
engine_2 = create_engine("postgresql://user:pass@host/bar")
|
|
67
|
+
|
|
68
|
+
with engine_1.connect() as left_conn, engine_2.connect() as right_conn:
|
|
69
|
+
reports = compare_databases(left_conn, right_conn)
|
|
70
|
+
|
|
71
|
+
if reports:
|
|
72
|
+
print("❌ Database schemas are different:", reports)
|
|
73
|
+
else:
|
|
74
|
+
print("✅ Database schemas are the same!")
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
See documentation for configuration and other details.
|
|
78
|
+
|
|
79
|
+
## Contributing
|
|
80
|
+
|
|
81
|
+
Issues and pull requests are welcome!
|
|
82
|
+
|
|
83
|
+
## License
|
|
84
|
+
|
|
85
|
+
This work is shared under [the MIT license](LICENSE);
|
|
86
|
+
|
|
87
|
+
@ 2025 Loïc Simon ([loic.simon@espci.org](mailto:loic.simon@espci.org))
|
comdab-0.1.0/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# comdab — Compare Database Schemas
|
|
2
|
+
|
|
3
|
+
_comdab_ allows you to compare in depth two database schemas to find
|
|
4
|
+
all differences: missing columns, different nullabilities or defaults,
|
|
5
|
+
slight changes in function or triggers definitions...
|
|
6
|
+
|
|
7
|
+
> [!WARNING]
|
|
8
|
+
>
|
|
9
|
+
> _comdab_ is still in development, only tested with PostgreSQL 14 to date.
|
|
10
|
+
>
|
|
11
|
+
> All feedback and contributions are welcome!
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
Use [pip](https://pip.pypa.io) to install comdab:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pip install comdab
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### Requirements
|
|
22
|
+
|
|
23
|
+
- Python `>= 3.12`
|
|
24
|
+
- [sqlalchemy](https://pypi.org/project/sqlalchemy) `>= 2.0`
|
|
25
|
+
- [pydantic](https://pypi.org/project/pydantic) `>= 2.0`
|
|
26
|
+
|
|
27
|
+
## Goals
|
|
28
|
+
|
|
29
|
+
_comdab_ is especially useful in combination with a migration tool like
|
|
30
|
+
[Alembic](https://alembic.sqlalchemy.org), to make sure that applying a
|
|
31
|
+
migration to an existing database
|
|
32
|
+
and creating a new database from scratch produce **the exact same schema**.
|
|
33
|
+
|
|
34
|
+
Indeed, while migration tools can auto-detect model changes and write
|
|
35
|
+
automatically the migrations to apply to pre-existing databases, it does not
|
|
36
|
+
cover the whole schema complexity, and does not prevent human errors (like
|
|
37
|
+
modifying the model without re-generating migrations, or manually editing
|
|
38
|
+
migrations in a slightly wrong way...)
|
|
39
|
+
|
|
40
|
+
This may cause dangerous and hard to spot bugs, especially if your unit tests
|
|
41
|
+
and CI run on a fresh database created from the Python-written model, and not
|
|
42
|
+
on pre-existing databases with the new migration applied.
|
|
43
|
+
|
|
44
|
+
By running _comdab_, you can ensure the two are the nearly-exact same.
|
|
45
|
+
|
|
46
|
+
_comdab_ stands on the shoulders of the [SQLAlchemy](https://sqlalchemy.org)
|
|
47
|
+
library, used to connect to the database and for most of schema introspection.
|
|
48
|
+
|
|
49
|
+
## Usage
|
|
50
|
+
|
|
51
|
+
```py
|
|
52
|
+
from comdab import compare_databases
|
|
53
|
+
from sqlalchemy import create_engine
|
|
54
|
+
|
|
55
|
+
engine_1 = create_engine("postgresql://user:pass@host/foo")
|
|
56
|
+
engine_2 = create_engine("postgresql://user:pass@host/bar")
|
|
57
|
+
|
|
58
|
+
with engine_1.connect() as left_conn, engine_2.connect() as right_conn:
|
|
59
|
+
reports = compare_databases(left_conn, right_conn)
|
|
60
|
+
|
|
61
|
+
if reports:
|
|
62
|
+
print("❌ Database schemas are different:", reports)
|
|
63
|
+
else:
|
|
64
|
+
print("✅ Database schemas are the same!")
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
See documentation for configuration and other details.
|
|
68
|
+
|
|
69
|
+
## Contributing
|
|
70
|
+
|
|
71
|
+
Issues and pull requests are welcome!
|
|
72
|
+
|
|
73
|
+
## License
|
|
74
|
+
|
|
75
|
+
This work is shared under [the MIT license](LICENSE);
|
|
76
|
+
|
|
77
|
+
@ 2025 Loïc Simon ([loic.simon@espci.org](mailto:loic.simon@espci.org))
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Minimal makefile for Sphinx documentation
|
|
2
|
+
#
|
|
3
|
+
|
|
4
|
+
# You can set these variables from the command line, and also
|
|
5
|
+
# from the environment for the first two.
|
|
6
|
+
SPHINXOPTS ?=
|
|
7
|
+
SPHINXBUILD ?= sphinx-build
|
|
8
|
+
SOURCEDIR = .
|
|
9
|
+
BUILDDIR = _build
|
|
10
|
+
|
|
11
|
+
# Put it first so that "make" without argument is like "make help".
|
|
12
|
+
help:
|
|
13
|
+
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
|
|
14
|
+
|
|
15
|
+
.PHONY: help Makefile
|
|
16
|
+
|
|
17
|
+
# Catch-all target: route all unknown targets to Sphinx using the new
|
|
18
|
+
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
|
|
19
|
+
%: Makefile
|
|
20
|
+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Configuration file for the Sphinx documentation builder.
|
|
2
|
+
#
|
|
3
|
+
# For the full list of built-in configuration values, see the documentation:
|
|
4
|
+
# https://www.sphinx-doc.org/en/master/usage/configuration.html
|
|
5
|
+
|
|
6
|
+
# -- Project information -----------------------------------------------------
|
|
7
|
+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
|
|
8
|
+
|
|
9
|
+
project = "comdab"
|
|
10
|
+
copyright = "2025, Loïc Simon"
|
|
11
|
+
author = "Loïc Simon"
|
|
12
|
+
release = "0.1.0"
|
|
13
|
+
|
|
14
|
+
# -- General configuration ---------------------------------------------------
|
|
15
|
+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
|
|
16
|
+
|
|
17
|
+
extensions = [
|
|
18
|
+
"sphinx.ext.autodoc",
|
|
19
|
+
"sphinx.ext.napoleon",
|
|
20
|
+
"sphinx.ext.intersphinx",
|
|
21
|
+
"sphinx.ext.viewcode",
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
templates_path = ["_templates"]
|
|
25
|
+
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
# -- Options for HTML output -------------------------------------------------
|
|
29
|
+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
|
|
30
|
+
|
|
31
|
+
html_theme = "pyramid"
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
# -- Extensions options ------------------------------------------------------
|
|
35
|
+
|
|
36
|
+
intersphinx_mapping = {
|
|
37
|
+
"python": ("https://docs.python.org/3/", None),
|
|
38
|
+
"sqlalchemy": ("https://docs.sqlalchemy.org/en/stable/", None),
|
|
39
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
comdab -- Compare Database Schemas
|
|
2
|
+
==================================
|
|
3
|
+
|
|
4
|
+
.. toctree::
|
|
5
|
+
:hidden:
|
|
6
|
+
|
|
7
|
+
reference
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
*comdab* allows you to compare in depth two database schemas to find
|
|
11
|
+
all differences: missing columns, different nullabilities or defaults,
|
|
12
|
+
slight changes in function or triggers definitions...
|
|
13
|
+
|
|
14
|
+
It is especially useful in combination with a migration tool like
|
|
15
|
+
`Alembic`_, to make sure that applying a migration to an existing database
|
|
16
|
+
and creating a new database from scratch produce **the exact same schema**.
|
|
17
|
+
|
|
18
|
+
Indeed, while migration tools can auto-detect model changes and write
|
|
19
|
+
automatically the migrations to apply to pre-existing databases, it does not
|
|
20
|
+
cover the whole schema complexity, and does not prevent human errors (like
|
|
21
|
+
modifying the model without re-generating migrations, or manually editing
|
|
22
|
+
migrations in a slightly wrong way...)
|
|
23
|
+
|
|
24
|
+
This may cause dangerous and hard to spot bugs, especially if your unit tests
|
|
25
|
+
and CI run on a fresh database created from the Python-written model, and not
|
|
26
|
+
on pre-existing databases with the new migration applied.
|
|
27
|
+
|
|
28
|
+
By running *comdab*, you can ensure the two are the nearly-exact same.
|
|
29
|
+
|
|
30
|
+
*comdab* is based on the wonderful `SQLAlchemy <sqla_>`_ library to connect to
|
|
31
|
+
the database, and for most of schema introspection.
|
|
32
|
+
|
|
33
|
+
.. warning::
|
|
34
|
+
|
|
35
|
+
*comdab* is still in development, only tested with PostgreSQL 14 to date.
|
|
36
|
+
|
|
37
|
+
All feedback and contributions are welcome!
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
Requirements
|
|
41
|
+
------------
|
|
42
|
+
|
|
43
|
+
* Python >= 3.11
|
|
44
|
+
* `sqlalchemy <https://pypi.org/project/sqlalchemy>`_ >= 2.0
|
|
45
|
+
* `pydantic <https://pypi.org/project/pydantic>`_ >= 2.0
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
Installation
|
|
49
|
+
------------
|
|
50
|
+
|
|
51
|
+
.. code-block::bash
|
|
52
|
+
|
|
53
|
+
pip install comdab
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
Usage
|
|
57
|
+
-----
|
|
58
|
+
|
|
59
|
+
*comdab* highest-level function is :func:`.compare_databases`, which needs
|
|
60
|
+
already established `SQLAlchemy connections`_ to the two databases to compare:
|
|
61
|
+
|
|
62
|
+
.. code-block:: python
|
|
63
|
+
|
|
64
|
+
from comdab import compare_databases
|
|
65
|
+
from sqlalchemy import create_engine
|
|
66
|
+
|
|
67
|
+
engine_1 = create_engine("postgresql://user:pass@host/foo")
|
|
68
|
+
engine_2 = create_engine("postgresql://user:pass@host/bar")
|
|
69
|
+
|
|
70
|
+
with engine_1.connect() as left_conn, engine_2.connect() as right_conn:
|
|
71
|
+
reports = compare_databases(left_conn, right_conn)
|
|
72
|
+
|
|
73
|
+
if reports:
|
|
74
|
+
print("❌ Database schemas are different:", reports)
|
|
75
|
+
else:
|
|
76
|
+
print("✅ Database schemas are the same!")
|
|
77
|
+
|
|
78
|
+
These connections will, of course, only be used for read-only database
|
|
79
|
+
introspection.
|
|
80
|
+
|
|
81
|
+
To compare an existing database with a Python-defined schema, the latter
|
|
82
|
+
has to be created first in a fresh database:
|
|
83
|
+
|
|
84
|
+
.. code-block:: python
|
|
85
|
+
|
|
86
|
+
from comdab import compare_databases
|
|
87
|
+
from sqlalchemy import create_engine
|
|
88
|
+
|
|
89
|
+
from my_app import BaseModel
|
|
90
|
+
|
|
91
|
+
engine_1 = create_engine("postgresql://user:password@host/foo")
|
|
92
|
+
engine_2 = create_engine("postgresql://user:password@host/tmp_db_just_created")
|
|
93
|
+
|
|
94
|
+
with engine_1.connect() as left_connection, engine_2.connect() as right_connection:
|
|
95
|
+
BaseModel.metadata.create_all(bind=right_connection)
|
|
96
|
+
reports = compare_databases(left_connection, right_connection)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
Supported database features
|
|
100
|
+
***************************
|
|
101
|
+
|
|
102
|
+
*comdab* heavily relies on SQLAlchemy `schema reflection`_ capabilities to
|
|
103
|
+
build its internal schema representation, but extends it (using custom queries)
|
|
104
|
+
to retrieve objects not natively handled by SQLALchemy.
|
|
105
|
+
|
|
106
|
+
Top-level schema fields
|
|
107
|
+
|
|
108
|
+
=========== ========== ====================================================
|
|
109
|
+
Object Support Notes
|
|
110
|
+
=========== ========== ====================================================
|
|
111
|
+
Tables Yes
|
|
112
|
+
Views Partial PostgreSQL only (temporary + materialized)
|
|
113
|
+
Sequences Partial PostgreSQL only
|
|
114
|
+
Functions Partial PostgreSQL only (compares reconstructed definitions)
|
|
115
|
+
----------- ---------- ----------------------------------------------------
|
|
116
|
+
Columns Yes
|
|
117
|
+
Constraints Partial All databases: PK, FK, unique & check constraints;
|
|
118
|
+
PostgreSQL only: exclude constraints
|
|
119
|
+
Indexes Yes
|
|
120
|
+
Triggers Partial PostgreSQL only (compares reconstructed definitions)
|
|
121
|
+
----------- ---------- ----------------------------------------------------
|
|
122
|
+
Column type Partial All SQLALchemy common types;
|
|
123
|
+
PostgreSQL only: HSTORE, ranges, multiranges
|
|
124
|
+
Owners No No notion of users/roles/permissions...
|
|
125
|
+
Comments No
|
|
126
|
+
=========== ========== ====================================================
|
|
127
|
+
|
|
128
|
+
Each object can hold non-standard / dialect-specific data in a ``extra``
|
|
129
|
+
dictionary, that will be compared too (unless specifically ignored, see below).
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
Ignore rules
|
|
133
|
+
************
|
|
134
|
+
|
|
135
|
+
.. _ignorerules:
|
|
136
|
+
|
|
137
|
+
*comdab* comes with a powerful system to allow some specific differences
|
|
138
|
+
between the two schemas. :func:`.build_comdab_schema` and
|
|
139
|
+
:func:`.compare_databases` functions take a ``rules`` argument, where you
|
|
140
|
+
can specifies *paths* to be ignored or reported as warnings.
|
|
141
|
+
|
|
142
|
+
These are a mapping of model path (build from :attr:`comdab.ROOT`) to
|
|
143
|
+
directives (either ``"ignore"``, ``warning`` or ``error``), where a path
|
|
144
|
+
may override a more general exclusion:
|
|
145
|
+
|
|
146
|
+
.. code-block:: python
|
|
147
|
+
|
|
148
|
+
rules={
|
|
149
|
+
# Do not compare functions
|
|
150
|
+
ROOT.functions: "ignore",
|
|
151
|
+
# Report differences as warnings for all tables starting with "_"
|
|
152
|
+
ROOT.tables["_.*"]: "warning",
|
|
153
|
+
# Except for triggers, that are really importants
|
|
154
|
+
ROOT.tables["_.*"].triggers: "error",
|
|
155
|
+
# Allow all left tables to have extra columns
|
|
156
|
+
ROOT.tables[...].columns.left_only: "ignore", # [...] means [".*"]
|
|
157
|
+
# But warn the other way around
|
|
158
|
+
ROOT.tables[...].columns.right_only: "warning",
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
Rules can use regular expressions, but two rules *cannot* match the same tables:
|
|
162
|
+
if you want to say "every tables but ``foo``", use ``(?!foo$).*``.
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
.. links
|
|
166
|
+
|
|
167
|
+
.. _Alembic: https://alembic.sqlalchemy.org
|
|
168
|
+
.. _sqla: https://sqlalchemy.org
|
|
169
|
+
.. _SQLAlchemy connections: https://docs.sqlalchemy.org/en/stable/code/connections.html
|
|
170
|
+
.. _schema reflection: https://docs.sqlalchemy.org/en/stable/code/reflecion.html
|