Skip to content

Database Migrations

CLI

mountaineer.migrations.cli.handle_generate async

handle_generate(message=None)

Creates a new migration definition file, comparing the previous version (if it exists) with the current schema.

mountaineer.migrations.cli.handle_apply async

handle_apply()

Applies all migrations that have not been applied to the database.

mountaineer.migrations.cli.handle_rollback async

handle_rollback()

Rolls back the last migration that was applied to the database.

Migrations

mountaineer.migrations.migration.MigrationRevisionBase

Base class for all revisions. Both the "up" and the "down" also accepts all dependency injection values.

up_revision instance-attribute

up_revision

down_revision instance-attribute

down_revision

handle_up async

handle_up()

Internal method to handle the up migration.

handle_down async

handle_down()

Internal method to handle the down migration.

up abstractmethod async

up(migrator)

Perform the migration "up" action. Clients should place their migration logic here.

down abstractmethod async

down(migrator)

Perform the migration "down" action. Clients should place their migration logic here.

mountaineer.migrations.migrator.Migrator

Migrator(db_session)

Main interface for client migrations. Mountaineer provides a simple shim on top of common database migration options within migrator.actor. This lets you add columns, drop columns, migrate types, and the like. For more complex migrations, you can use the migrator.db_session to run raw SQL queries within the current migration transaction.

actor instance-attribute

actor = DatabaseActions(
    dry_run=False, db_session=db_session
)

db_session instance-attribute

db_session = db_session

new_migrator async classmethod

new_migrator(db_engine)

init_db async

init_db()

Initialize our migration management table if it doesn't already exist within the attached postgres database. This will be a no-op if the table already exists.

Client callers should call this method once before running any migrations.

set_active_revision async

set_active_revision(value)

get_active_revision async

get_active_revision()

mountaineer.migrations.migrator.NoCommitAsyncSession

NoCommitAsyncSession(*args, **kwargs)

Bases: AsyncSession

To be safe in case of an error and rollback, migrations must be run in an isolated transaction and shouldn't be committed. This class is a simple wrapper around AsyncSession that will prevent commits.

okay_to_commit instance-attribute

okay_to_commit = False

commit async

commit()

Actions

mountaineer.migrations.actions.DatabaseActions

DatabaseActions(dry_run=True, db_session=None)

Track the actions that need to be executed to the database. Provides a shallow, typed ORM on top of the raw SQL commands that we'll execute through sqlalchemy.

This class manually builds up the SQL strings that will be executed against postgres. We intentionally avoid using the ORM or variable-insertion modes here because most table-schema operations don't permit parameters to specify top-level SQL syntax. To keep things consistent, we'll use the same SQL string interpolation for all operations.

dry_run instance-attribute

dry_run = dry_run

dry_run_actions instance-attribute

dry_run_actions = []

db_session instance-attribute

db_session = db_session

prod_sqls instance-attribute

prod_sqls = []

add_table async

add_table(table_name)

drop_table async

drop_table(table_name)

add_column async

add_column(
    table_name,
    column_name,
    explicit_data_type=None,
    explicit_data_is_list=False,
    custom_data_type=None,
)

drop_column async

drop_column(table_name, column_name)

rename_column async

rename_column(table_name, old_column_name, new_column_name)

modify_column_type async

modify_column_type(
    table_name,
    column_name,
    explicit_data_type=None,
    explicit_data_is_list=False,
    custom_data_type=None,
)

add_constraint async

add_constraint(
    table_name,
    columns,
    constraint,
    constraint_name,
    constraint_args=None,
)

drop_constraint async

drop_constraint(table_name, constraint_name)

add_not_null async

add_not_null(table_name, column_name)

drop_not_null async

drop_not_null(table_name, column_name)

add_type async

add_type(type_name, values)

add_type_values async

add_type_values(type_name, values)

drop_type_values async

drop_type_values(type_name, values, target_columns)

Dropping values from an existing type isn't natively supported by Postgres. We work around this limitation by specifying the "target_columns" that already reference the enum type that we want to drop.

PARAMETER DESCRIPTION
target_columns

Specified tuples of (table_name, column_name) pairs that should be migrated to the new enum value.

TYPE: list[tuple[str, str]]

drop_type async

drop_type(type_name)

add_comment

add_comment(text)

mountaineer.migrations.actions.ColumnType

Bases: StrEnum

__str__ class-attribute instance-attribute

__str__ = __str__

SMALLINT class-attribute instance-attribute

SMALLINT = 'smallint'

INTEGER class-attribute instance-attribute

INTEGER = 'integer'

BIGINT class-attribute instance-attribute

BIGINT = 'bigint'

DECIMAL class-attribute instance-attribute

DECIMAL = 'decimal'

NUMERIC class-attribute instance-attribute

NUMERIC = 'numeric'

REAL class-attribute instance-attribute

REAL = 'real'

DOUBLE_PRECISION class-attribute instance-attribute

DOUBLE_PRECISION = 'double precision'

SERIAL class-attribute instance-attribute

SERIAL = 'serial'

BIGSERIAL class-attribute instance-attribute

BIGSERIAL = 'bigserial'

MONEY class-attribute instance-attribute

MONEY = 'money'

CHAR class-attribute instance-attribute

CHAR = 'char'

VARCHAR class-attribute instance-attribute

VARCHAR = 'character varying'

TEXT class-attribute instance-attribute

TEXT = 'text'

BYTEA class-attribute instance-attribute

BYTEA = 'bytea'

DATE class-attribute instance-attribute

DATE = 'date'

TIME class-attribute instance-attribute

TIME = 'time'

TIME_WITH_TIME_ZONE class-attribute instance-attribute

TIME_WITH_TIME_ZONE = 'time with time zone'

TIMESTAMP class-attribute instance-attribute

TIMESTAMP = 'timestamp'

TIMESTAMP_WITH_TIME_ZONE class-attribute instance-attribute

TIMESTAMP_WITH_TIME_ZONE = 'timestamp with time zone'

INTERVAL class-attribute instance-attribute

INTERVAL = 'interval'

BOOLEAN class-attribute instance-attribute

BOOLEAN = 'boolean'

POINT class-attribute instance-attribute

POINT = 'point'

LINE class-attribute instance-attribute

LINE = 'line'

LSEG class-attribute instance-attribute

LSEG = 'lseg'

BOX class-attribute instance-attribute

BOX = 'box'

PATH class-attribute instance-attribute

PATH = 'path'

POLYGON class-attribute instance-attribute

POLYGON = 'polygon'

CIRCLE class-attribute instance-attribute

CIRCLE = 'circle'

CIDR class-attribute instance-attribute

CIDR = 'cidr'

INET class-attribute instance-attribute

INET = 'inet'

MACADDR class-attribute instance-attribute

MACADDR = 'macaddr'

MACADDR8 class-attribute instance-attribute

MACADDR8 = 'macaddr8'

BIT class-attribute instance-attribute

BIT = 'bit'

BIT_VARYING class-attribute instance-attribute

BIT_VARYING = 'bit varying'

TSVECTOR class-attribute instance-attribute

TSVECTOR = 'tsvector'

TSQUERY class-attribute instance-attribute

TSQUERY = 'tsquery'

UUID class-attribute instance-attribute

UUID = 'uuid'

XML class-attribute instance-attribute

XML = 'xml'

JSON class-attribute instance-attribute

JSON = 'json'

JSONB class-attribute instance-attribute

JSONB = 'jsonb'

INT4RANGE class-attribute instance-attribute

INT4RANGE = 'int4range'

NUMRANGE class-attribute instance-attribute

NUMRANGE = 'numrange'

TSRANGE class-attribute instance-attribute

TSRANGE = 'tsrange'

TSTZRANGE class-attribute instance-attribute

TSTZRANGE = 'tstzrange'

DATERANGE class-attribute instance-attribute

DATERANGE = 'daterange'

OID class-attribute instance-attribute

OID = 'oid'

__new__

__new__(*values)

values must already be of type str

mountaineer.migrations.actions.ConstraintType

Bases: StrEnum

__str__ class-attribute instance-attribute

__str__ = __str__

PRIMARY_KEY class-attribute instance-attribute

PRIMARY_KEY = 'PRIMARY KEY'

FOREIGN_KEY class-attribute instance-attribute

FOREIGN_KEY = 'FOREIGN KEY'

UNIQUE class-attribute instance-attribute

UNIQUE = 'UNIQUE'

CHECK class-attribute instance-attribute

CHECK = 'CHECK'

__new__

__new__(*values)

values must already be of type str