Skip to content

prefect.server.database.alembic_commands

alembic_downgrade(revision='-1', dry_run=False)

Run alembic downgrades on Prefect REST API database

Parameters:

Name Type Description Default
revision str

The revision passed to alembic downgrade. Defaults to 'base', downgrading all revisions.

'-1'
dry_run bool

Show what migrations would be made without applying them. Will emit sql statements to stdout.

False
Source code in src/prefect/server/database/alembic_commands.py
67
68
69
70
71
72
73
74
75
76
77
78
79
@with_alembic_lock
def alembic_downgrade(revision: str = "-1", dry_run: bool = False):
    """
    Run alembic downgrades on Prefect REST API database

    Args:
        revision: The revision passed to `alembic downgrade`. Defaults to 'base', downgrading all revisions.
        dry_run: Show what migrations would be made without applying them. Will emit sql statements to stdout.
    """
    # lazy import for performance
    import alembic.command

    alembic.command.downgrade(alembic_config(), revision, sql=dry_run)

alembic_revision(message=None, autogenerate=False, **kwargs)

Create a new revision file for the database.

Parameters:

Name Type Description Default
message Optional[str]

string message to apply to the revision.

None
autogenerate bool

whether or not to autogenerate the script from the database.

False
Source code in src/prefect/server/database/alembic_commands.py
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
@with_alembic_lock
def alembic_revision(
    message: Optional[str] = None, autogenerate: bool = False, **kwargs
):
    """
    Create a new revision file for the database.

    Args:
        message: string message to apply to the revision.
        autogenerate: whether or not to autogenerate the script from the database.
    """
    # lazy import for performance
    import alembic.command

    alembic.command.revision(
        alembic_config(), message=message, autogenerate=autogenerate, **kwargs
    )

alembic_stamp(revision)

Stamp the revision table with the given revision; don't run any migrations

Parameters:

Name Type Description Default
revision

The revision passed to alembic stamp.

required
Source code in src/prefect/server/database/alembic_commands.py
101
102
103
104
105
106
107
108
109
110
111
112
@with_alembic_lock
def alembic_stamp(revision):
    """
    Stamp the revision table with the given revision; don't run any migrations

    Args:
        revision: The revision passed to `alembic stamp`.
    """
    # lazy import for performance
    import alembic.command

    alembic.command.stamp(alembic_config(), revision=revision)

alembic_upgrade(revision='head', dry_run=False)

Run alembic upgrades on Prefect REST API database

Parameters:

Name Type Description Default
revision str

The revision passed to alembic downgrade. Defaults to 'head', upgrading all revisions.

'head'
dry_run bool

Show what migrations would be made without applying them. Will emit sql statements to stdout.

False
Source code in src/prefect/server/database/alembic_commands.py
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
@with_alembic_lock
def alembic_upgrade(revision: str = "head", dry_run: bool = False):
    """
    Run alembic upgrades on Prefect REST API database

    Args:
        revision: The revision passed to `alembic downgrade`. Defaults to 'head', upgrading all revisions.
        dry_run: Show what migrations would be made without applying them. Will emit sql statements to stdout.
    """
    # lazy import for performance
    import alembic.command

    # don't display reflection warnings that pop up during schema migrations
    with warnings.catch_warnings():
        warnings.filterwarnings(
            "ignore",
            message="Skipped unsupported reflection of expression-based index",
            category=SAWarning,
        )
        alembic.command.upgrade(alembic_config(), revision, sql=dry_run)

with_alembic_lock(fn)

Decorator that prevents alembic commands from running concurrently. This is necessary because alembic uses a global configuration object that is not thread-safe.

This issue occurred in https://github.com/PrefectHQ/prefect-dask/pull/50, where dask threads were simultaneously performing alembic upgrades, and causing cryptic KeyError: 'config' when del globals_[attr_name].

Source code in src/prefect/server/database/alembic_commands.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def with_alembic_lock(fn):
    """
    Decorator that prevents alembic commands from running concurrently.
    This is necessary because alembic uses a global configuration object
    that is not thread-safe.

    This issue occurred in https://github.com/PrefectHQ/prefect-dask/pull/50, where
    dask threads were simultaneously performing alembic upgrades, and causing
    cryptic `KeyError: 'config'` when `del globals_[attr_name]`.
    """

    @wraps(fn)
    def wrapper(*args, **kwargs):
        with ALEMBIC_LOCK:
            return fn(*args, **kwargs)

    return wrapper