Skip to main content

Compare Database Schemas

Project description

comdab — Compare Database Schemas

PyPI - Python Version PyPI - Version GitHub Actions Workflow Status Read the Docs PyPI - Types

comdab allows you to compare in depth two database schemas to find all differences: missing columns, different nullabilities or defaults, slight changes in function or triggers definitions...

[!WARNING]

comdab is still in development, only tested with PostgreSQL 14 — 18 to date.

All feedback and contributions are welcome!

Installation

Use pip to install comdab:

pip install comdab

Requirements

Goals

comdab allows you to compare in depth two database schemas to find all differences (missing columns, different nullabilities or defaults, slight changes in function or triggers definitions...) and turn these reports into model migrations.

comdab migration generation supercharges migration tools like Alembic to create migrations that, when applied to an existing database, produce the exact same schema than a new database created from scratch.

Indeed, while migration tools can auto-detect model changes and write automatically the migrations to apply to pre-existing databases, it does not cover the whole schema complexity, and does not prevent human errors (like modifying the model without re-generating migrations, or manually editing migrations in a slightly wrong way...)

This may cause dangerous and hard to spot bugs, especially if your unit tests and CI run on a fresh database created from the Python-written model, and not on pre-existing databases with the new migration applied.

By running comdab, you can ensure the two are the nearly-exact same.

comdab is based on the wonderful SQLAlchemy library to connect to the database, and for most of schema introspection.

Usage

Comparing database schemas

comdab main reporting function is .compare_databases, which needs already established SQLAlchemy connections to the two databases to compare:

from comdab import compare_databases
from sqlalchemy import create_engine

engine_1 = create_engine("postgresql://user:pass@host/foo")
engine_2 = create_engine("postgresql://user:pass@host/bar")

with engine_1.connect() as left_conn, engine_2.connect() as right_conn:
    reports = compare_databases(left_conn, right_conn)

if reports:
    print("❌ Database schemas are different:", reports)
else:
    print("✅ Database schemas are the same!")

Generating migrations

To generate migrations, a user-defined MigrationGeneratorPort / PartialMigrationGeneratorPort subclass is additionally needed:

from comdab import generate_migrations, PartialMigrationGeneratorPort
from comdab.models import ComdabTable
from sqlalchemy import create_engine

class MyMigrationGenerator(PartialMigrationGeneratorPort):
    def __init__(self) -> None:
        self.sql_text = ""

    def create_table(self, *, table: ComdabTable) -> None:
        self.sql_text += f"CREATE TABLE {table.name} [...];\n"

    def drop_table(self, *, table: ComdabTable) -> None:
        self.sql_text += f"DROP TABLE {table.name};\n"

    ...

generator = MyMigrationGenerator()
engine_1 = create_engine("postgresql://user:pass@host/source")
engine_2 = create_engine("postgresql://user:pass@host/target")

with engine_1.connect() as source_conn, engine_2.connect() as target_conn:
    generate_migrations(source_conn, target_conn, generator)

print(generator.sql_text)

Advanced usage

See documentation for configuration and other details.

Contributing

Issues and pull requests are welcome!

License

This work is shared under the MIT license.

@ 2025 Loïc Simon (loic.simon@espci.org)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

comdab-0.4.1.tar.gz (76.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

comdab-0.4.1-py3-none-any.whl (32.9 kB view details)

Uploaded Python 3

File details

Details for the file comdab-0.4.1.tar.gz.

File metadata

  • Download URL: comdab-0.4.1.tar.gz
  • Upload date:
  • Size: 76.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for comdab-0.4.1.tar.gz
Algorithm Hash digest
SHA256 f8872df1f29ab199c5a0a99f2d107316a78c0b81470e85855704c62c1ba04285
MD5 7ea3cac16c2571e068082da71611e430
BLAKE2b-256 7480b81b209d8cd062491d46bf0b3cdc16ffdbd51ec8a79f1b7b2a73c42ba07f

See more details on using hashes here.

Provenance

The following attestation bundles were made for comdab-0.4.1.tar.gz:

Publisher: release.yml on loic-simon/comdab

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file comdab-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: comdab-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 32.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for comdab-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6e14b2482f6c23e4b86e258824c9adafa3331afaca4242b8ca15606f010d6b49
MD5 5d8b6afb294d7684dc958884d46f48a2
BLAKE2b-256 faa7b7934d93b0df3a668c3cd6a651eea68fad24543b0efbf575091ff4280c6f

See more details on using hashes here.

Provenance

The following attestation bundles were made for comdab-0.4.1-py3-none-any.whl:

Publisher: release.yml on loic-simon/comdab

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page