providers.mongodb.migrations.MigrationManager

Top-level logic for ensuring the database is updated before running the service.

Usage

Source

providers.mongodb.migrations.MigrationManager()

The migrate_or_wait method must be called before any instance of the service begins its main execution loop.

Version 1 is reserved for the framework as a way to mark when versioning was added.

Example usage:

from my_service.config import Config  # inherits from MongoDbConfig
from hexkit.providers.mongodb.migrations import MigrationManager
from my_service.migrations import V2Migration, V3Migration

DB_VERSION = 2  # the current expected DB version
MY_MIGRATION_MAP = {2: V2Migration, 3: V3Migration} # etc.

def migrate_my_service():
    # Called before starting my_service
    config = Config()

    async with MigrationManager(config, DB_VERSION, MY_MIGRATION_MAP) as mm:
        await mm.migrate_or_wait()

Methods

Name Description
__aenter__() Set up database client and database reference
__aexit__() Release DB lock and close/remove database client
__init__() Instantiate the MigrationManager.
migrate_or_wait() Try to migrate the database or wait until migrations are completed.

__aenter__()

Set up database client and database reference

Usage

Source

__aenter__()


__aexit__()

Release DB lock and close/remove database client

Usage

Source

__aexit__(exc_type_, exc_value, exc_tb)


__init__()

Instantiate the MigrationManager.

Usage

Source

__init__(config, target_version, migration_map)

Args - config: Config containing db connection str and lock/db versioning collections - target_version: Which version the db needs to be at for this version of the service - migration_map: A dict with the MigrationDefinition class for each db version


migrate_or_wait()

Try to migrate the database or wait until migrations are completed.

Usage

Source

migrate_or_wait()