Skip to content

Commit

Permalink
Update README instructions and set migration state in dev_cli db-gene…
Browse files Browse the repository at this point in the history
…rate #425
  • Loading branch information
joelvdavies committed Nov 26, 2024
1 parent e51c4db commit feeac08
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 14 deletions.
52 changes: 43 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,30 +329,64 @@ a microservice that provides user authentication against an LDAP server and retu
### Migrations
Migration scripts are located inside the `inventory_management_system/migrations/scripts`. See the
`example_migration.py` for an example on how to implement one. Any new migrations added should be automatically picked
up and shown via
#### Adding a migration
To add a migration first use
```bash
ims-migrate create <migration_name> <migration_description>
```
to create a new one inside the `inventory_management_system/migrations/scripts` directory. Then add the code necessary
to perform the migration. See `_example_migration.py` for an example on how to implement one.
#### Performing forward migrations
Before performing a you can first check the current status of the database and any outstanding migrations using
```bash
ims-migrate list
ims-migrate status
```
or
or in Docker
```bash
docker exec -it inventory_management_system_api_container ims-migrate list
```
if running in Docker.
Then to perform all outstanding migrations up to the latest one use
```bash
ims-migrate forward latest
```
To perform a migration you should use
You may also specify a specific migration name to apply instead which will apply all migrations between the current
applied one and the specified one. A prompt will be shown to ensure the migrations being applied are sensible.
#### Performing backward migrations
To revert the database by performing backwards migrations you can first use
```bash
ims-migrate forward <migration_name>
ims-migrate status
```
To revert the same migration use
to check the current status of the database and available migrations and then use
```bash
ims-migrate backward <migration_name>
```
to perform all backward migrations to get from the current database state back to the state prior to the chosen
migration name (i.e. it also performs the backward migration for the given migration name).
#### Forcing migration state
If for some reason the migration state is different to what you expect it may be forced via
```bash
ims-migrate set <migration_name>
```
This is already set to `latest` automatically when using the `dev_cli` to regenerate mock data so that the dump retains
the expected state.
Binary file modified data/mock_data.dump
Binary file not shown.
26 changes: 21 additions & 5 deletions inventory_management_system_api/migrations/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
import sys
from abc import ABC, abstractmethod
from typing import Optional

from inventory_management_system_api.core.database import get_database
from inventory_management_system_api.migrations.core import (
Expand All @@ -23,17 +24,30 @@
database = get_database()


def check_user_sure() -> bool:
def check_user_sure(message: Optional[str] = None, skip: bool = False) -> bool:
"""
Asks user if they are sure action should proceed and exits if not.
:param message: Message to accompany the check.
:param skip: Whether to skip printing out the message and performing the check.
:return: Whether user is sure.
"""

if skip:
return True

print(message)
print()
answer = input("Are you sure you wish to proceed? ")
return answer in ("y", "yes")


def add_skip_args(parser: argparse.ArgumentParser):
"""Adds common arguments for skipping user prompts."""

parser.add_argument("--yes", "-y", help="Specify to skip all are you sure prompts", action="store_true")


class SubCommand(ABC):
"""Base class for a sub command."""

Expand Down Expand Up @@ -198,6 +212,8 @@ def __init__(self):
def setup(self, parser: argparse.ArgumentParser):
parser.add_argument("name", help="Name of the last migration the database currently matches.")

add_skip_args(parser)

def run(self, args: argparse.Namespace):
available_migrations = find_available_migrations()

Expand All @@ -206,10 +222,10 @@ def run(self, args: argparse.Namespace):
except ValueError:
sys.exit(f"Migration '{args.name}' was not found in the available list of migrations")

print(f"This operation will forcibly set the latest migration to '{available_migrations[end_index]}'")
print()

if check_user_sure():
if check_user_sure(
message=f"This operation will forcibly set the latest migration to '{available_migrations[end_index]}'",
skip=args.yes,
):
set_previous_migration(available_migrations[end_index])


Expand Down
4 changes: 4 additions & 0 deletions scripts/dev_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ def run(self, args: argparse.Namespace):
generate_mock_data()
except ImportError:
logging.error("Failed to find generate_mock_data.py")

logging.info("Ensuring previous migration is set to latest...")
run_command(["ims-migrate", "set", "latest", "-y"])

if args.dump:
logging.info("Dumping output...")
# Dump output again
Expand Down

0 comments on commit feeac08

Please sign in to comment.