Skip to main content

Alembic extension that adds support for arbitrary user-defined objects like views or functions in autogenerate command.

Project description

Alembic Dumb DDL

tests codecov

A plugin for Alembic DB migration tool that adds support for arbitrary user-defined objects like views, functions, triggers, etc. in autogenerate command.

Alembic DDDL does not compare the objects in the code with their state in the database. Instead, it only tracks if the source code of the script has changed, compared to the previous revision.

Installation

You can install Alembic Dumb DDL from pip:

pip install alembic-dddl

Quick start

Step 1: save your DDL script in a file, and make sure that it overwrites the entities, not just creates them (e.g. start with DROP ... IF EXISTS or a similar construct for your DBMS).

-- myapp/scripts/last_month_orders.sql

DROP VIEW IF EXISTS last_month_orders;

CREATE VIEW last_month_orders AS
    SELECT *
    FROM orders
    WHERE order_date > current_date - interval '30 days';

Step 2: Wrap your script in a DDL class:

# myapp/models.py

from alembic_dddl import DDL
from pathlib import Path

SCRIPTS = Path(__file__).parent / "scripts"


def load_sql(filename: str) -> str:
    """Helper function to load the contents of a file from a `scripts` directory"""
    return (SCRIPTS / filename).read_text()


my_ddl = DDL(
    # will be used in revision filename
    name="last_month_orders",
    # DDL script SQL code, will be used in the upgrade command
    sql=load_sql("last_month_orders.sql"),
    # Cleanup SQL code, will be used in the first downgrade command
    down_sql="DROP VIEW IF EXISTS last_month_orders;",
)

Step 3: Register your script in alembic's env.py:

# migrations/env.py

from myapp.models import my_ddl
from alembic_dddl import register_ddl

register_ddl(my_ddl)  # also supports a list

# ...
# the rest of the env.py file

From now on the alembic autogenerate command will keep track of last_month_orders.sql, and if it changes — automatically add update code to your migration scripts to update your entities.

Run the migration:

$ alembic revision --autogenerate -m "last_month_orders"
...
INFO  [alembic_dddl.dddl] Detected new DDL "last_month_orders"
  Generating myapp/migrations/versions/2024_01_08_0955-0c897e9399a9_last_month_orders.py ...  done

The generated revision script:

# migrations/versions/2024_01_08_0955-0c897e9399a9_last_month_orders.py
...

def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.run_ddl_script("2024_01_08_0955_last_month_orders_0c897e9399a9.sql")
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.execute("DROP VIEW IF EXISTS last_month_orders;")
    # ### end Alembic commands ###

For more info see tutorial or take a look at the Example Project.

Why do it this way?

Managing your custom entities with Alembic DDDL has several benefits:

  1. The DDL scripts are defined in one place in the source code, any change to them is reflected in git history through direct diffs.
  2. Any kind of SQL script and any DBMS is supported because the plugin does not interact with the database.
  3. The migrations for your DDL scripts are fully autogenerated, they are also clean and concise.

Further reading

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

alembic_dddl-0.1.2.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

alembic_dddl-0.1.2-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file alembic_dddl-0.1.2.tar.gz.

File metadata

  • Download URL: alembic_dddl-0.1.2.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.13 Darwin/23.2.0

File hashes

Hashes for alembic_dddl-0.1.2.tar.gz
Algorithm Hash digest
SHA256 23a9cb8029ed0aab1fcc85d08c08c055830d6c722347eb22256351b4f54deb66
MD5 71c9d27f362aab054dde258674bd93db
BLAKE2b-256 54523e66d0688ee06b55154e9f1631e596c85490d1ed8095220d47bbeb10f66e

See more details on using hashes here.

File details

Details for the file alembic_dddl-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: alembic_dddl-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.13 Darwin/23.2.0

File hashes

Hashes for alembic_dddl-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8fcc72fc8f5c35057a7738d22bd2d52a60ad8405b0b43c910d62c89bc26b4f5c
MD5 93c667a19f55ea7e5d8c33989e988bb8
BLAKE2b-256 00d658bb3c87c39cf80f12f969c5b207a7969b20af026bf7ae942811d8c8d21b

See more details on using hashes here.

Supported by

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