{"library":"migra","title":"migra: PostgreSQL Schema Migration","description":"migra is a Python library that functions like `diff` for PostgreSQL schemas. It compares two PostgreSQL databases (or SQLAlchemy metadata objects) and generates the SQL statements necessary to migrate one to the other, making schema management and deployment safer and more explicit. The current version is 3.0.1663481299, with releases following active development.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install migra"],"cli":{"name":"migra","version":"usage: migra [-h] [--unsafe] [--schema SCHEMA]"}},"imports":["from migra import Migration","from migra import create"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import os\nfrom migra import Migration\nfrom sqlalchemy import create_engine, MetaData, Table, Column, Integer, String\n\n# Use environment variables for PostgreSQL connection strings for safety\n# Ensure POSTGRES_SOURCE_URL and POSTGRES_TARGET_URL are set to real PostgreSQL databases.\n# For a demo, you can point them to two different databases on the same host,\n# or create a temporary 'source' schema and an empty 'target' schema.\nsource_url = os.environ.get('POSTGRES_SOURCE_URL', 'postgresql+psycopg2://user:pass@localhost:5432/source_db')\ntarget_url = os.environ.get('POSTGRES_TARGET_URL', 'postgresql+psycopg2://user:pass@localhost:5432/target_db')\n\n# Establish SQLAlchemy engines\nsource_engine = create_engine(source_url)\ntarget_engine = create_engine(target_url)\n\n# Define a simple schema for the \"source\" database (what we want the target to look like)\nsource_metadata = MetaData()\nTable('users', source_metadata,\n      Column('id', Integer, primary_key=True),\n      Column('name', String(50), nullable=False),\n      Column('email', String(100), unique=True))\n\n# Apply the source schema to the source database (if not already there)\nprint(\"Ensuring source schema exists in source_db...\")\nwith source_engine.connect() as conn:\n    source_metadata.create_all(conn)\n    conn.commit()\n\n# The target database is assumed to be empty or have an older schema.\n# migra will generate SQL to make target look like source.\n\n# Create a Migration object to compare the live schemas\nm = Migration(source=source_engine, target=target_engine)\n\n# Get the DDL statements to transform target to source\nsql_statements = m.statements\n\nprint(\"\\nGenerated SQL statements to migrate target_db to source_db schema:\")\nif sql_statements:\n    for stmt in sql_statements:\n        print(stmt)\n    # To apply the migration to the target database (UNCOMMENT WITH EXTREME CAUTION!)\n    # print(\"\\nApplying migration to target database...\")\n    # with target_engine.connect() as conn:\n    #    m.apply(conn)\n    #    conn.commit()\n    # print(\"Migration applied successfully.\")\nelse:\n    print(\"No migration statements needed (schemas are identical or target is already ahead).\")","lang":"python","description":"This quickstart compares a defined SQLAlchemy `MetaData` schema with a live (potentially empty) target PostgreSQL database. It generates the DDL needed to make the target schema match the source. Remember to set `POSTGRES_SOURCE_URL` and `POSTGRES_TARGET_URL` environment variables to your actual PostgreSQL connection strings. Always review the generated SQL before applying it to a production database.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}