## providers.mongodb.migrations.MigrationManager

<span id="hexkit.providers.mongodb.migrations.MigrationManager"></span>


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


Usage

``` python
providers.mongodb.migrations.MigrationManager(
    config, target_version, migration_map
)
```


The [migrate_or_wait](providers.mongodb.migrations.MigrationManager.md#hexkit.providers.mongodb.migrations.MigrationManager.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__()](#__aenter__) | Set up database client and database reference |
| [__aexit__()](#__aexit__) | Release DB lock and close/remove database client |
| [__init__()](#__init__) | Instantiate the MigrationManager. |
| [migrate_or_wait()](#migrate_or_wait) | Try to migrate the database or wait until migrations are completed. |

<span id="__aenter__"></span>

<span id="hexkit.providers.mongodb.migrations.MigrationManager.__aenter__"></span>


#### \_\_aenter\_\_()


Set up database client and database reference


Usage

``` python
__aenter__()
```


<span id="__aexit__"></span>

<span id="hexkit.providers.mongodb.migrations.MigrationManager.__aexit__"></span>

------------------------------------------------------------------------


#### \_\_aexit\_\_()


Release DB lock and close/remove database client


Usage

``` python
__aexit__(exc_type_, exc_value, exc_tb)
```


<span id="__init__"></span>

<span id="hexkit.providers.mongodb.migrations.MigrationManager.__init__"></span>

------------------------------------------------------------------------


#### \_\_init\_\_()


Instantiate the MigrationManager.


Usage

``` python
__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


<span id="hexkit.providers.mongodb.migrations.MigrationManager.migrate_or_wait"></span>

------------------------------------------------------------------------


#### migrate_or_wait()


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


Usage

``` python
migrate_or_wait()
```
