comdab 0.3.0__tar.gz → 0.4.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.
Files changed (65) hide show
  1. {comdab-0.3.0 → comdab-0.4.0}/.github/workflows/test.yml +7 -6
  2. {comdab-0.3.0 → comdab-0.4.0}/.pre-commit-config.yaml +3 -3
  3. comdab-0.4.0/.python-version +1 -0
  4. comdab-0.4.0/.readthedocs.yaml +16 -0
  5. {comdab-0.3.0 → comdab-0.4.0}/CHANGELOG.md +26 -0
  6. {comdab-0.3.0 → comdab-0.4.0}/PKG-INFO +4 -4
  7. {comdab-0.3.0 → comdab-0.4.0}/README.md +1 -1
  8. {comdab-0.3.0 → comdab-0.4.0}/docs/conf.py +20 -1
  9. {comdab-0.3.0 → comdab-0.4.0}/docs/index.rst +56 -13
  10. comdab-0.4.0/docs/reference.rst +39 -0
  11. {comdab-0.3.0 → comdab-0.4.0}/pyproject.toml +18 -5
  12. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/__init__.py +12 -1
  13. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/exceptions.py +4 -2
  14. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/main.py +66 -5
  15. comdab-0.4.0/src/comdab/migrate.py +560 -0
  16. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/models/__init__.py +19 -11
  17. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/models/column.py +1 -1
  18. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/models/constraint.py +20 -12
  19. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/models/function.py +1 -1
  20. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/models/index.py +1 -1
  21. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/models/schema.py +1 -1
  22. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/models/table.py +1 -1
  23. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/models/type.py +1 -2
  24. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/models/view.py +1 -1
  25. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/path.py +27 -1
  26. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/report.py +4 -6
  27. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/specific/postgresql/functions.py +1 -1
  28. {comdab-0.3.0 → comdab-0.4.0}/tests/_mock_module.py +1 -1
  29. comdab-0.4.0/tests/test_main.py +169 -0
  30. comdab-0.4.0/tests/test_migrate.py +607 -0
  31. {comdab-0.3.0 → comdab-0.4.0}/tests/test_models.py +13 -7
  32. {comdab-0.3.0 → comdab-0.4.0}/tests/test_specific/test_postgresql.py +29 -12
  33. comdab-0.4.0/uv.lock +642 -0
  34. comdab-0.3.0/.python-version +0 -1
  35. comdab-0.3.0/.readthedocs.yaml +0 -9
  36. comdab-0.3.0/docs/reference.rst +0 -20
  37. comdab-0.3.0/tests/test_main.py +0 -67
  38. comdab-0.3.0/uv.lock +0 -544
  39. {comdab-0.3.0 → comdab-0.4.0}/.github/workflows/release.yml +0 -0
  40. {comdab-0.3.0 → comdab-0.4.0}/.gitignore +0 -0
  41. {comdab-0.3.0 → comdab-0.4.0}/LICENSE +0 -0
  42. {comdab-0.3.0 → comdab-0.4.0}/docs/Makefile +0 -0
  43. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/build.py +0 -0
  44. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/compare.py +0 -0
  45. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/models/base.py +0 -0
  46. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/models/custom_type.py +0 -0
  47. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/models/sequence.py +0 -0
  48. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/models/trigger.py +0 -0
  49. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/py.typed +0 -0
  50. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/source.py +0 -0
  51. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/specific/__init__.py +0 -0
  52. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/specific/postgresql/__init__.py +0 -0
  53. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/specific/postgresql/build.py +0 -0
  54. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/specific/postgresql/custom_types.py +0 -0
  55. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/specific/postgresql/exclude_constraints.py +0 -0
  56. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/specific/postgresql/sequences.py +0 -0
  57. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/specific/postgresql/triggers.py +0 -0
  58. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/specific/postgresql/views.py +0 -0
  59. {comdab-0.3.0 → comdab-0.4.0}/src/comdab/utils.py +0 -0
  60. {comdab-0.3.0 → comdab-0.4.0}/tests/__init__.py +0 -0
  61. {comdab-0.3.0 → comdab-0.4.0}/tests/conftest.py +0 -0
  62. {comdab-0.3.0 → comdab-0.4.0}/tests/test_build.py +0 -0
  63. {comdab-0.3.0 → comdab-0.4.0}/tests/test_compare.py +0 -0
  64. {comdab-0.3.0 → comdab-0.4.0}/tests/test_specific/__init__.py +0 -0
  65. {comdab-0.3.0 → comdab-0.4.0}/tests/test_utils.py +0 -0
@@ -9,22 +9,23 @@ jobs:
9
9
  strategy:
10
10
  matrix:
11
11
  python-version:
12
- - "3.12"
13
12
  - "3.13"
14
- - "3.14.0-beta.3"
13
+ - "3.14"
14
+ - "3.15.0-beta.1"
15
15
 
16
16
  steps:
17
- - uses: actions/checkout@v4
17
+ - uses: actions/checkout@v6
18
18
 
19
19
  - name: Set up Python ${{ matrix.python-version }}
20
- uses: actions/setup-python@v5
20
+ uses: actions/setup-python@v6
21
21
  with:
22
22
  python-version: ${{ matrix.python-version }}
23
23
 
24
24
  - name: Install uv
25
- uses: astral-sh/setup-uv@v6
25
+ uses: astral-sh/setup-uv@v8.1.0
26
26
  with:
27
- version: "0.7.13"
27
+ version: "0.11.8"
28
+ python-version: ${{ matrix.python-version }}
28
29
  enable-cache: true
29
30
 
30
31
  - name: Install the project
@@ -1,6 +1,6 @@
1
1
  repos:
2
2
  - repo: https://github.com/pre-commit/pre-commit-hooks
3
- rev: v3.2.0
3
+ rev: v6.0.0
4
4
  hooks:
5
5
  - id: trailing-whitespace
6
6
  - id: end-of-file-fixer
@@ -8,14 +8,14 @@ repos:
8
8
  - id: check-added-large-files
9
9
 
10
10
  - repo: https://github.com/astral-sh/ruff-pre-commit
11
- rev: v0.11.11
11
+ rev: v0.15.9
12
12
  hooks:
13
13
  - id: ruff
14
14
  args: ["--fix"]
15
15
  - id: ruff-format
16
16
 
17
17
  - repo: https://github.com/RobertCraigie/pyright-python
18
- rev: v1.1.402
18
+ rev: v1.1.408
19
19
  hooks:
20
20
  - id: pyright
21
21
  hook-stage: pre-push
@@ -0,0 +1 @@
1
+ 3.14
@@ -0,0 +1,16 @@
1
+ version: 2
2
+
3
+ build:
4
+ os: ubuntu-lts-latest
5
+ tools:
6
+ python: "3.14"
7
+
8
+ sphinx:
9
+ configuration: docs/conf.py
10
+
11
+ python:
12
+ install:
13
+ - method: uv
14
+ command: sync
15
+ groups:
16
+ - docs
@@ -5,6 +5,32 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.4.0] - 2026-05-31
9
+
10
+ ### Added
11
+
12
+ - New migration generation mechanism, based on new `generate_migrations`
13
+ and `generate_migrations_from_reports` top-level functions, and new
14
+ `MigrationGeneratorPort` / `PartialMigrationGeneratorPort` classes
15
+
16
+ ### Changed
17
+
18
+ - Bump minimal Python version to 3.13, test with Python 3.15
19
+ - Improve documentation style and API Reference page
20
+ - Bump package versions in uv.lock, other maintenance upgrades
21
+
22
+ ### Fixed
23
+
24
+ - Fix `ComdabConstraint` annotations, using a new `ComdabConstraintType` alias
25
+ for more precise tagged union typing
26
+ - Various documentation fixes
27
+
28
+ ## [0.3.1] - 2026-04-07
29
+
30
+ ### Added
31
+
32
+ - PostgreSQL: detect & compare procedures, in addition of regular functions
33
+
8
34
  ## [0.3.0] - 2025-08-12
9
35
 
10
36
  ### Added
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: comdab
3
- Version: 0.3.0
3
+ Version: 0.4.0
4
4
  Summary: Compare Database Schemas
5
5
  Project-URL: Homepage, https://github.com/loic-simon/comdab
6
6
  Project-URL: Documentation, https://comdab.readthedocs.io/en/latest
@@ -11,12 +11,12 @@ License-File: LICENSE
11
11
  Classifier: Development Status :: 4 - Beta
12
12
  Classifier: Intended Audience :: Developers
13
13
  Classifier: Programming Language :: Python :: 3
14
- Classifier: Programming Language :: Python :: 3.12
15
14
  Classifier: Programming Language :: Python :: 3.13
16
15
  Classifier: Programming Language :: Python :: 3.14
16
+ Classifier: Programming Language :: Python :: 3.15
17
17
  Classifier: Topic :: Database
18
18
  Classifier: Typing :: Typed
19
- Requires-Python: >=3.12
19
+ Requires-Python: >=3.13
20
20
  Requires-Dist: pydantic>=2.0
21
21
  Requires-Dist: sqlalchemy>=2.0
22
22
  Description-Content-Type: text/markdown
@@ -35,7 +35,7 @@ slight changes in function or triggers definitions...
35
35
 
36
36
  > [!WARNING]
37
37
  >
38
- > _comdab_ is still in development, only tested with PostgreSQL 14 to date.
38
+ > _comdab_ is still in development, only tested with PostgreSQL 14 — 18 to date.
39
39
  >
40
40
  > All feedback and contributions are welcome!
41
41
 
@@ -12,7 +12,7 @@ slight changes in function or triggers definitions...
12
12
 
13
13
  > [!WARNING]
14
14
  >
15
- > _comdab_ is still in development, only tested with PostgreSQL 14 to date.
15
+ > _comdab_ is still in development, only tested with PostgreSQL 14 — 18 to date.
16
16
  >
17
17
  > All feedback and contributions are welcome!
18
18
 
@@ -36,7 +36,18 @@ exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
36
36
  # -- Options for HTML output -------------------------------------------------
37
37
  # https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
38
38
 
39
- html_theme = "pyramid"
39
+ html_theme = "shibuya"
40
+ html_theme_options = {
41
+ "accent_color": "indigo",
42
+ "nav_links_align": "right",
43
+ "show_ai_links": False,
44
+ "github_url": "https://github.com/loic-simon/comdab",
45
+ }
46
+ html_context = {
47
+ "source_type": "github",
48
+ "source_user": "loic-simon",
49
+ "source_repo": "comdab",
50
+ }
40
51
 
41
52
 
42
53
  # -- Extensions options ------------------------------------------------------
@@ -45,3 +56,11 @@ intersphinx_mapping = {
45
56
  "python": ("https://docs.python.org/3/", None),
46
57
  "sqlalchemy": ("https://docs.sqlalchemy.org/en/stable/", None),
47
58
  }
59
+ autodoc_default_options = {
60
+ "members": True,
61
+ "undoc-members": True,
62
+ "member-order": "bysource",
63
+ "exclude-members": "__init__,Path,model_config",
64
+ "no-index-entry": True,
65
+ }
66
+ autodoc_class_signature = "separated"
@@ -4,16 +4,18 @@ comdab -- Compare Database Schemas
4
4
  .. toctree::
5
5
  :hidden:
6
6
 
7
+ Get started <self>
7
8
  reference
8
9
 
9
10
 
10
11
  *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...
12
+ all differences (missing columns, different nullabilities or defaults,
13
+ slight changes in function or triggers definitions...) and turn these
14
+ reports into model migrations.
13
15
 
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**.
16
+ *comdab* migration generation supercharges migration tools like
17
+ `Alembic`_ to create migrations that, when applied to an existing database,
18
+ produce **the exact same schema** than a new database created from scratch.
17
19
 
18
20
  Indeed, while migration tools can auto-detect model changes and write
19
21
  automatically the migrations to apply to pre-existing databases, it does not
@@ -32,7 +34,7 @@ the database, and for most of schema introspection.
32
34
 
33
35
  .. warning::
34
36
 
35
- *comdab* is still in development, only tested with PostgreSQL 14 to date.
37
+ *comdab* is still in development, only tested with PostgreSQL 14 — 18 to date.
36
38
 
37
39
  All feedback and contributions are welcome!
38
40
 
@@ -40,7 +42,7 @@ the database, and for most of schema introspection.
40
42
  Requirements
41
43
  ------------
42
44
 
43
- * Python >= 3.11
45
+ * Python >= 3.13
44
46
  * `sqlalchemy <https://pypi.org/project/sqlalchemy>`_ >= 2.0
45
47
  * `pydantic <https://pypi.org/project/pydantic>`_ >= 2.0
46
48
 
@@ -56,7 +58,10 @@ Installation
56
58
  Usage
57
59
  -----
58
60
 
59
- *comdab* highest-level function is :func:`.compare_databases`, which needs
61
+ Comparing database schemas
62
+ **************************
63
+
64
+ *comdab* main reporting function is :func:`.compare_databases`, which needs
60
65
  already established `SQLAlchemy connections`_ to the two databases to compare:
61
66
 
62
67
  .. code-block:: python
@@ -68,12 +73,12 @@ already established `SQLAlchemy connections`_ to the two databases to compare:
68
73
  engine_2 = create_engine("postgresql://user:pass@host/bar")
69
74
 
70
75
  with engine_1.connect() as left_conn, engine_2.connect() as right_conn:
71
- reports = compare_databases(left_conn, right_conn)
76
+ reports = compare_databases(left_conn, right_conn)
72
77
 
73
78
  if reports:
74
- print("❌ Database schemas are different:", reports)
79
+ print("❌ Database schemas are different:", reports)
75
80
  else:
76
- print("✅ Database schemas are the same!")
81
+ print("✅ Database schemas are the same!")
77
82
 
78
83
  These connections will, of course, only be used for read-only database
79
84
  introspection.
@@ -96,6 +101,43 @@ has to be created first in a fresh database:
96
101
  reports = compare_databases(left_connection, right_connection)
97
102
 
98
103
 
104
+ Generating migrations
105
+ *********************
106
+
107
+ To generate migrations, a user-defined :class:`.MigrationGeneratorPort` /
108
+ :class:`.PartialMigrationGeneratorPort` subclass is additionally needed:
109
+
110
+ .. code-block:: python
111
+
112
+ from comdab import generate_migrations, PartialMigrationGeneratorPort
113
+ from comdab.models import ComdabTable
114
+ from sqlalchemy import create_engine
115
+
116
+ class MyMigrationGenerator(PartialMigrationGeneratorPort):
117
+ def __init__(self) -> None:
118
+ self.sql_text = ""
119
+
120
+ def create_table(self, *, table: ComdabTable) -> None:
121
+ self.sql_text += f"CREATE TABLE {table.name} [...];\n"
122
+
123
+ def drop_table(self, *, table: ComdabTable) -> None:
124
+ self.sql_text += f"DROP TABLE {table.name};\n"
125
+
126
+ ...
127
+
128
+ generator = MyMigrationGenerator()
129
+ engine_1 = create_engine("postgresql://user:pass@host/source")
130
+ engine_2 = create_engine("postgresql://user:pass@host/target")
131
+
132
+ with engine_1.connect() as source_conn, engine_2.connect() as target_conn:
133
+ generate_migrations(source_conn, target_conn, generator)
134
+
135
+ print(generator.sql_text)
136
+
137
+ An `Alembic`_-based migration generator implementation might be included
138
+ in a future *comdab* release.
139
+
140
+
99
141
  Supported database features
100
142
  ***************************
101
143
 
@@ -137,8 +179,9 @@ Ignore rules
137
179
  .. _ignorerules:
138
180
 
139
181
  *comdab* comes with a powerful system to allow some specific differences
140
- between the two schemas. :func:`.build_comdab_schema` and
141
- :func:`.compare_databases` functions take a ``rules`` argument, where you
182
+ between the two schemas. :func:`.generate_migrations`,
183
+ :func:`.build_comdab_schema` and :func:`.compare_databases`
184
+ functions take a ``rules`` argument, where you
142
185
  can specifies *paths* to be ignored or reported as warnings.
143
186
 
144
187
  These are a mapping of model path (build from :attr:`comdab.ROOT`) to
@@ -0,0 +1,39 @@
1
+ :tocdepth: 3
2
+
3
+ API Reference
4
+ =============
5
+
6
+ Entry points
7
+ ------------
8
+
9
+ .. automodule:: comdab
10
+ :members:
11
+ :exclude-members: PartialMigrationGeneratorPort
12
+
13
+ .. class:: comdab.PartialMigrationGeneratorPort
14
+
15
+ Like :class:`.MigrationGeneratorPort`, but does not require all migration methods to be implemented.
16
+
17
+ By default, :exc:`NotImplementedError` will be raised if a missing method is called;
18
+ to silently do nothing instead, use ``class MyGenerator(PartialMigrationGeneratorPort, strict=False)``.
19
+
20
+
21
+ Reports
22
+ -------
23
+
24
+ .. automodule:: comdab.report
25
+ :members:
26
+
27
+
28
+ Models
29
+ ------
30
+
31
+ .. automodule:: comdab.models
32
+ :members:
33
+
34
+
35
+ Exceptions
36
+ ----------
37
+
38
+ .. automodule:: comdab.exceptions
39
+ :members:
@@ -1,18 +1,18 @@
1
1
  [project]
2
2
  name = "comdab"
3
- version = "0.3.0"
3
+ version = "0.4.0"
4
4
  description = "Compare Database Schemas"
5
5
  readme = "README.md"
6
- requires-python = ">=3.12"
6
+ requires-python = ">=3.13"
7
7
  dependencies = ["pydantic>=2.0", "sqlalchemy>=2.0"]
8
8
  authors = [{ name = "Loïc Simon", email = "loic.simon@espci.org" }]
9
9
  classifiers = [
10
10
  "Development Status :: 4 - Beta",
11
11
  "Intended Audience :: Developers",
12
12
  "Programming Language :: Python :: 3",
13
- "Programming Language :: Python :: 3.12",
14
13
  "Programming Language :: Python :: 3.13",
15
14
  "Programming Language :: Python :: 3.14",
15
+ "Programming Language :: Python :: 3.15",
16
16
  "Typing :: Typed",
17
17
  "Topic :: Database"
18
18
  ]
@@ -27,20 +27,33 @@ Issues = "https://github.com/loic-simon/comdab/issues"
27
27
  requires = ["hatchling >= 1.26"]
28
28
  build-backend = "hatchling.build"
29
29
 
30
- [tool.uv]
31
- dev-dependencies = [
30
+ [dependency-groups]
31
+ dev = [
32
32
  "psycopg2>=2.9.10",
33
33
  "pytest>=8.4.0",
34
34
  "ruff>=0.11.11",
35
+ ]
36
+ docs = [
37
+ "shibuya>=2026.5.19",
35
38
  "sphinx>=8.2.3",
36
39
  ]
37
40
 
38
41
  [tool.ruff]
39
42
  line-length = 120
40
43
 
44
+ [tool.ruff.lint]
45
+ select = ["E", "F", "UP", "B", "SIM", "I"]
46
+
41
47
  [tool.pyright]
42
48
  typeCheckingMode = "strict"
49
+ pythonVersion = "3.13"
43
50
  venvPath = "."
44
51
  venv = ".venv"
45
52
  reportUnknownArgumentType = false
46
53
  reportUnknownMemberType = false
54
+
55
+ [tool.pytest]
56
+ filterwarnings = [
57
+ "error",
58
+ "ignore:.+can't locate any SQLAlchemy dialect:sqlalchemy.exc.SAWarning"
59
+ ]
@@ -3,7 +3,14 @@
3
3
  Loïc Simon 2025, MIT License
4
4
  """
5
5
 
6
- from comdab.main import build_comdab_schema, compare_comdab_schemas, compare_databases
6
+ from comdab.main import (
7
+ build_comdab_schema,
8
+ compare_comdab_schemas,
9
+ compare_databases,
10
+ generate_migrations,
11
+ generate_migrations_from_reports,
12
+ )
13
+ from comdab.migrate import MigrationGeneratorPort, PartialMigrationGeneratorPort
7
14
  from comdab.models import ROOT
8
15
 
9
16
  #: The top-level *comdab* path, pointing to a whole :class:`ComdabSchema`.
@@ -18,8 +25,12 @@ ROOT = ROOT # pyright: ignore[reportConstantRedefinition] # Only here for Sphi
18
25
 
19
26
 
20
27
  __all__ = [
28
+ "generate_migrations",
21
29
  "compare_databases",
22
30
  "build_comdab_schema",
23
31
  "compare_comdab_schemas",
32
+ "generate_migrations_from_reports",
24
33
  "ROOT",
34
+ "MigrationGeneratorPort",
35
+ "PartialMigrationGeneratorPort",
25
36
  ]
@@ -5,7 +5,8 @@ class ComdabException(Exception):
5
5
  class UnhandledTypeError(ComdabException):
6
6
  """A SQL type not handled by comdab was encountered.
7
7
 
8
- Consider passing `allow_unknown_types=True`, or opening a feature request!"""
8
+ Consider passing ``allow_unknown_types=True``, or opening a feature request!
9
+ """
9
10
 
10
11
 
11
12
  class OverlappingPathsError(ComdabException):
@@ -15,4 +16,5 @@ class OverlappingPathsError(ComdabException):
15
16
  class ComdabInternalError(ComdabException):
16
17
  """Something unexpected happened!
17
18
 
18
- If it escapes comdab internals, please report a bug."""
19
+ If it escapes comdab internals, please report a bug.
20
+ """
@@ -1,14 +1,59 @@
1
+ from collections.abc import Iterable
2
+
1
3
  from sqlalchemy import Connection
2
4
  from sqlalchemy.dialects import postgresql
3
5
 
4
6
  from comdab.build import ComdabBuilder
5
7
  from comdab.compare import ComdabComparer, ComdabStrategy
8
+ from comdab.migrate import MigrationGeneratorPort, generate_migration
6
9
  from comdab.models.schema import ComdabSchema
7
10
  from comdab.path import ComdabPath
8
11
  from comdab.report import ComdabReport
9
12
  from comdab.source import ComdabSource
10
13
 
11
14
 
15
+ def generate_migrations(
16
+ source_connection: Connection,
17
+ target_connection: Connection,
18
+ generator: MigrationGeneratorPort,
19
+ *,
20
+ source_schema: str = "public",
21
+ target_schema: str = "public",
22
+ rules: dict[ComdabPath, ComdabStrategy] | None = None,
23
+ allow_unknown_types: bool = False,
24
+ ) -> None:
25
+ """Generate migrations from a source schema to a target schema.
26
+
27
+ This is the highest-level, all-in-one *comdab* function.
28
+
29
+ Args:
30
+ source_connection: A SQLAlchemy :class:`connection <sqlalchemy.engine.Connection>` established
31
+ to the database we want to migrate.
32
+ target_connection: A SQLAlchemy :class:`connection <sqlalchemy.engine.Connection>` established
33
+ to a database with the desired state to reach.
34
+ generator: The :class:`MigrationGeneratorPort` to use to generate migrations after the differences are found.
35
+ source_schema: The name of the database schema to compare for the ``source_connection``.
36
+ target_schema: The name of the database schema to compare for the ``target_connection``.
37
+ rules: An optional set of rules to allow some differences between the schemas,
38
+ or report them as warnings. See :ref:`Ignore rules <ignorerules>` for more details.
39
+ allow_unknown_types: If ``True``, SQL data types not handed (yet) by comdab will be
40
+ modeled as "unknown" and compared based on their name only. If ``False`` (the default),
41
+ encountering an unknown name will raise an :exc:`UnhandledTypeError`.
42
+ """
43
+ source = build_comdab_schema(
44
+ source_connection,
45
+ schema=source_schema,
46
+ allow_unknown_types=allow_unknown_types,
47
+ )
48
+ target = build_comdab_schema(
49
+ target_connection,
50
+ schema=target_schema,
51
+ allow_unknown_types=allow_unknown_types,
52
+ )
53
+ reports = compare_comdab_schemas(source, target, rules=rules)
54
+ generate_migrations_from_reports(target, reports, generator)
55
+
56
+
12
57
  def compare_databases(
13
58
  left_connection: Connection,
14
59
  right_connection: Connection,
@@ -20,12 +65,10 @@ def compare_databases(
20
65
  ) -> list[ComdabReport]:
21
66
  """Build and compare two database schemas, from their SQLAlchemy connection.
22
67
 
23
- This is the highest-level, all-in-one *comdab* function.
24
-
25
68
  Args:
26
- left_connection: A SQLAlchemy :class:`~sqlalchemy.Connection<connection>` established
69
+ left_connection: A SQLAlchemy :class:`connection <sqlalchemy.engine.Connection>` established
27
70
  to the first database to compare.
28
- right_connection: A SQLAlchemy :class:`~sqlalchemy.Connection<connection>` established
71
+ right_connection: A SQLAlchemy :class:`connection <sqlalchemy.engine.Connection>` established
29
72
  to the second database to compare.
30
73
  left_schema: The name of the database schema to compare for the ``left_connection``.
31
74
  right_schema: The name of the database schema to compare for the ``right_connection``.
@@ -59,7 +102,7 @@ def build_comdab_schema(
59
102
  This is the internal representation used for comparison by *comdab*.
60
103
 
61
104
  Args:
62
- connection: A SQLAlchemy :class:`~sqlalchemy.Connection<connection>` established
105
+ connection: A SQLAlchemy :class:`connection <sqlalchemy.engine.Connection>` established
63
106
  to the database to reflect.
64
107
  schema: The name of the database schema to reflect.
65
108
  allow_unknown_types: If ``True``, SQL data types not handed (yet) by comdab will be
@@ -97,3 +140,21 @@ def compare_comdab_schemas(
97
140
  """
98
141
  comparer = ComdabComparer(rules=rules)
99
142
  return comparer.compare(left, right)
143
+
144
+
145
+ def generate_migrations_from_reports(
146
+ target_schema: ComdabSchema,
147
+ reports: Iterable[ComdabReport],
148
+ generator: MigrationGeneratorPort,
149
+ ) -> None:
150
+ """Generate migrations from already build *comdab* reports.
151
+
152
+ This is a pure function, needing no connection to a database.
153
+
154
+ Args:
155
+ target_schema: The *comdab*-built schema to reach after migrations.
156
+ reports: The *comdab*-built reports of differences between the source and target schemas.
157
+ generator: The :class:`MigrationGeneratorPort` to use to generate migrations from reports.
158
+ """
159
+ for report in reports: # TODO: ordering...
160
+ generate_migration(target_schema, report, generator)