Skip to content

Database

Thin wrappers around Alembic's revision, upgrade, and downgrade commands, scoped to the current Application's environment and alembic.ini. These back the grace generate migration and grace db up/grace db down commands — see Database Management.

grace.database

generate_migration

generate_migration(app, message)
Source code in grace/database.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def generate_migration(app: Application, message: str):
    if not app.has_database:
        raise ConfigError(
            "This project has no database configured. "
            "Run 'grace generate database' to add one."
        )

    try:
        alembic_cfg = Config("alembic.ini")
        alembic_cfg.config_ini_section = app.environment

        revision(alembic_cfg, message=message, autogenerate=True, sql=False)
    except CommandError as e:
        fatal(f"Error creating migration: {e}")

up_migration

up_migration(app, revision_='head')
Source code in grace/database.py
27
28
29
30
31
32
33
def up_migration(app: Application, revision_: str = "head"):
    info(f"Upgrading revision {revision_}")

    alembic_cfg = Config("alembic.ini")
    alembic_cfg.config_ini_section = app.environment

    upgrade(alembic_cfg, revision=revision_)

down_migration

down_migration(app, revision_='head')
Source code in grace/database.py
36
37
38
39
40
41
42
def down_migration(app: Application, revision_: str = "head"):
    info(f"Downgrading revision {revision_}")

    alembic_cfg = Config("alembic.ini")
    alembic_cfg.config_ini_section = app.environment

    downgrade(alembic_cfg, revision=revision_)