{"library":"sqlalchemy-diff","title":"SQLAlchemy Diff","type":"library","description":"SQLAlchemy-diff is a Python library that provides a tool for comparing database schemas using SQLAlchemy. It helps identify differences between two database schemas, reporting on tables, columns, primary keys, foreign keys, indexes, unique constraints, check constraints, and enums. The current version is 1.1.1, and it maintains a release cadence of updates for compatibility and bug fixes, with major versions introducing breaking changes as seen with v1.0.0.","language":"python","status":"active","last_verified":"Thu Apr 16","install":{"commands":["pip install sqlalchemy-diff"],"cli":{"name":"sqlalchemy-diff","version":"sh: 1: sqlalchemy-diff: not found"}},"imports":["from sqlalchemy import create_engine","from sqlalchemydiff.comparer import Comparer"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":null,"github":"https://github.com/gianchub/sqlalchemy-diff","docs":null,"changelog":null,"pypi":"https://pypi.org/project/sqlalchemy-diff/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"from sqlalchemy import create_engine, Column, Integer, String, MetaData, Table\nfrom sqlalchemydiff.comparer import Comparer\n\n# Define schema for database one (source)\nmetadata_one = MetaData()\nTable(\n    'users',\n    metadata_one,\n    Column('id', Integer, primary_key=True),\n    Column('name', String(50)),\n    Column('email', String(100), unique=True)\n)\n\n# Define schema for database two (target) with a difference\nmetadata_two = MetaData()\nTable(\n    'users',\n    metadata_two,\n    Column('id', Integer, primary_key=True),\n    Column('name', String(50)),\n    Column('address', String(200)) # Changed column\n)\n\n# Create in-memory SQLite engines for demonstration\nengine_one = create_engine('sqlite:///:memory:')\nengine_two = create_engine('sqlite:///:memory:')\n\n# Create tables in memory\nmetadata_one.create_all(engine_one)\nmetadata_two.create_all(engine_two)\n\n# Compare the schemas\ncomparer = Comparer(engine_one, engine_two)\nresult = comparer.compare(one_alias='Source DB', two_alias='Target DB')\n\nif result.is_match:\n    print('Schemas are identical!')\nelse:\n    print('Schemas differ! Differences:')\n    for error in result.errors:\n        print(f\"  - {error.description} (Table: {error.table_name}, Column: {error.column_name or 'N/A'}) [Severity: {error.severity}]\")\n\n# Example using from_params for automatic engine disposal\n# (Requires actual database URIs to connect)\n# try:\n#     comparer_from_params = Comparer.from_params(\n#         'sqlite:///:memory:',\n#         'sqlite:///:memory:',\n#         dispose_engines=True\n#     )\n#     result_from_params = comparer_from_params.compare()\n#     print('\\nComparison using from_params:')\n#     if result_from_params.is_match:\n#         print('Schemas are identical (from_params)!')\n#     else:\n#         print('Schemas differ (from_params)!')\n#         for error in result_from_params.errors:\n#             print(f\"  - {error.description}\")\n# except Exception as e:\n#     print(f\"Error with from_params example: {e}\")","lang":"python","description":"This quickstart demonstrates how to compare two SQLAlchemy database schemas using in-memory SQLite databases. It initializes two `MetaData` objects, creates corresponding tables in two separate engines, and then uses the `Comparer` class to identify differences. The output will detail discrepancies found, such as a changed column name. It also comments on `Comparer.from_params` for convenience with automatic engine disposal.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}