Skip to content

mongrator.planner

mongrator.planner

plan_up(files, applied, target=None)

Compute which migrations need to be applied in the forward direction.

Parameters:

Name Type Description Default
files list[MigrationFile]

All known migration files, in chronological order.

required
applied set[MigrationId]

Set of migration IDs already recorded in the tracking collection.

required
target MigrationId | None

If given, apply only up to and including this migration ID. Raises MigrationNotFoundError if target is not in files.

None

Returns:

Type Description
MigrationPlan

A MigrationPlan with to_apply (pending) and to_skip (already applied).

Source code in src/mongrator/planner.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def plan_up(
    files: list[MigrationFile],
    applied: set[MigrationId],
    target: MigrationId | None = None,
) -> MigrationPlan:
    """Compute which migrations need to be applied in the forward direction.

    Args:
        files:   All known migration files, in chronological order.
        applied: Set of migration IDs already recorded in the tracking collection.
        target:  If given, apply only up to and including this migration ID.
                 Raises MigrationNotFoundError if target is not in files.

    Returns:
        A MigrationPlan with to_apply (pending) and to_skip (already applied).
    """
    if target is not None:
        ids = {f.id for f in files}
        if target not in ids:
            raise MigrationNotFoundError(target)

    to_apply: list[MigrationFile] = []
    to_skip: list[MigrationFile] = []

    for f in files:
        if f.id in applied:
            to_skip.append(f)
        else:
            to_apply.append(f)
        if target is not None and f.id == target:
            break

    return MigrationPlan(to_apply=to_apply, to_skip=to_skip)

plan_down(files, applied, steps=1)

Compute which migrations to roll back.

Rolls back the most recently applied migrations, up to steps of them. Migrations are identified by their position in the applied set intersected with the ordered file list (file order is the canonical order).

Parameters:

Name Type Description Default
files list[MigrationFile]

All known migration files, in chronological order.

required
applied set[MigrationId]

Set of migration IDs already recorded in the tracking collection.

required
steps int

Number of most-recent applied migrations to roll back.

1

Returns:

Type Description
MigrationPlan

A MigrationPlan where to_apply contains the migrations to roll back

MigrationPlan

(in reverse order) and to_skip contains those left untouched.

Source code in src/mongrator/planner.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def plan_down(
    files: list[MigrationFile],
    applied: set[MigrationId],
    steps: int = 1,
) -> MigrationPlan:
    """Compute which migrations to roll back.

    Rolls back the most recently applied migrations, up to `steps` of them.
    Migrations are identified by their position in the applied set intersected
    with the ordered file list (file order is the canonical order).

    Args:
        files:   All known migration files, in chronological order.
        applied: Set of migration IDs already recorded in the tracking collection.
        steps:   Number of most-recent applied migrations to roll back.

    Returns:
        A MigrationPlan where to_apply contains the migrations to roll back
        (in reverse order) and to_skip contains those left untouched.
    """
    if steps < 1:
        raise ValueError(f"steps must be >= 1, got {steps}")

    applied_in_order = [f for f in files if f.id in applied]
    to_rollback = list(reversed(applied_in_order[-steps:]))
    rollback_ids = {f.id for f in to_rollback}
    to_skip = [f for f in files if f.id in applied and f.id not in rollback_ids]

    return MigrationPlan(to_apply=to_rollback, to_skip=to_skip)