Skip to content

Extend autogenerate outside upgrade() and downgrade() #686

@olirice

Description

@olirice

Following along with ReplaceableObject cookbook

https://alembic.sqlalchemy.org/en/latest/cookbook.html#create-initial-migrations

we end up with a migration like:

from alembic import op
import sqlalchemy as sa
from foo import ReplaceableObject

some_sp = ReplaceableObject(
    "to_upper(some_text text)",
    " RETURNS text AS $$ SELECT upper(text) $$ LANGUAGE sql;")

def upgrade():
    op.create_sp(some_sp)


def downgrade():
    op.drop_sp(some_sp)

and can refer back to that for a replace operation in a later migration with

op.replace_sp(some_updated_sp, replaces="28af9800143f.some_sp")

In order for some_sp to be importable for use in the replaces parameter, it must be defined outside of the upgrade and downgrade functions.

The autogenerate api

@renderers.dispatch_for(ReplaceFunctionOp)

can be used to render ReplaceableObjects within an upgrade or downgrade function but I haven't found the appropriate place to hook to render ReplaceableObject in the migration's header.

In other words, can easily autogenerate

def upgrade():
    some_sp = ReplaceableObject(
        "to_upper(some_text text)",
        " RETURNS text AS $$ SELECT upper(text) $$ LANGUAGE sql;")

    op.create_sp(some_sp)
...

but it isn't clear how to generate

some_sp = ReplaceableObject(
        "to_upper(some_text text)",
        " RETURNS text AS $$ SELECT upper(text) $$ LANGUAGE sql;")

def upgrade():    
    op.create_sp(some_sp)
...

Is that the expected approach? If not, I'd really appreciate a nudge in the right direction

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionusage and API questions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions