Skip to content

database

dev_tool.commands.database

log = logging.getLogger(__name__) module-attribute

DatabaseCommandGroup

Bases: CommandGroup

A command group for database operations.

This class provides commands for interacting with databases, including seeding, flushing, resetting, and dumping operations.

The constructor for the DatabaseCommandGroup class.

This method initializes the command group with required services and registers all command methods.

Parameters:

  • docker (DockerService) –

    The Docker service for container management.

  • django (DjangoService) –

    The Django service for Django operations.

  • portal (PortalService) –

    The Portal service for API integration.

  • postgres (PostgresService) –

    The PostgreSQL service for database operations.

Source code in dev_tool/commands/database.py
def __init__(
    self,
    docker: DockerService,
    django: DjangoService,
    portal: PortalService,
    postgres: PostgresService
) -> None:
    """
    The constructor for the DatabaseCommandGroup class.

    This method initializes the command group with required services
    and registers all command methods.

    :param docker: The Docker service for container management.
    :param django: The Django service for Django operations.
    :param portal: The Portal service for API integration.
    :param postgres: The PostgreSQL service for database operations.
    """

    super().__init__()

    self.django = django
    self.docker = docker
    self.portal = portal
    self.postgres = postgres

django = django instance-attribute

docker = docker instance-attribute

portal = portal instance-attribute

postgres = postgres instance-attribute

__str__

The string representation of the command group.

This method returns the category name for the command group.

Returns:

  • str –

    The category name as a string.

Source code in dev_tool/commands/database.py
def __str__(self) -> str:
    """
    The string representation of the command group.

    This method returns the category name for the command group.

    :return: The category name as a string.
    """

    return 'Database'

database_operations

A command that opens a submenu for seeding operations.

This method returns a menu for different seeding operations.

Returns:

  • Menu –

    A submenu for seeding operations.

Source code in dev_tool/commands/database.py
@ordered_submenu(label='Seeding')
def database_operations(self) -> Menu:
    """
    A command that opens a submenu for seeding operations.

    This method returns a menu for different seeding operations.

    :return: A submenu for seeding operations.
    """

    items: list[Menu | MenuItem] = [
        MenuItem(
            label='Seed the database',
            action=CommandAction(command=self._seed)
        ),
        MenuItem(
            label='Seed superuser(s)',
            action=CommandAction(command=self._seed_superuser)
        ),
        MenuItem(
            label='Flush the database',
            action=CommandAction(command=self._flush)
        ),
        MenuItem(
            label='Flush and seed the database',
            action=CommandAction(command=self._flush_and_seed)
        ),
        MenuItem(
            label='Reset and seed the database',
            action=CommandAction(command=self._reset_and_seed)
        )
    ]

    return Menu(title='Seeding', items=items)

dump_cloud

A command that dumps a cloud database to the local development environment.

This method checks for required environment variables and then dumps a cloud database to the local Docker database, with options to (re)dump or reuse existing dump files.

Source code in dev_tool/commands/database.py
@ordered_command(label='Dump cloud to local database')
def dump_cloud(self) -> None:
    """
    A command that dumps a cloud database to the local development environment.

    This method checks for required environment variables and then dumps
    a cloud database to the local Docker database, with options to (re)dump
    or reuse existing dump files.
    """

    if is_dump_environment_variables(suppress=False):
        reuse = False

        redump = get_user_confirmation('Do you want (re)dump the database?')

        if self.postgres.is_dump_file() and not redump:
            reuse = get_user_confirmation('A dump file exists. Do you want to reuse this file?')

        try:
            self.docker.ensure_container(recreate=False)
        except Exception as e:
            self.emit_error(f'Failed to ensure Docker container: {e}')
            return

        try:
            self.postgres.dump_cloud_to_docker_database(redump, reuse)
        except Exception as e:
            self.emit_error(f'Failed to dump cloud database to Docker: {e}')

backup_cloud

A command that opens a submenu for backing up a cloud database to a .sql file.

Returns:

  • Menu –

    A submenu of the cloud databases the project offers.

Source code in dev_tool/commands/database.py
@ordered_submenu(label='Backup production or staging database')
@is_developer_token
def backup_cloud(self) -> Menu:
    """
    A command that opens a submenu for backing up a cloud database to a .sql file.

    :return: A submenu of the cloud databases the project offers.
    """

    return self._cloud_database_menu('Backup production or staging database', self._backup_cloud)

dump_cloud_database

A command that opens a submenu for dumping a cloud database to the local development environment.

Returns:

  • Menu –

    A submenu of the cloud databases the project offers.

Source code in dev_tool/commands/database.py
@ordered_submenu(label='Dump production or staging to local database')
@is_developer_token
def dump_cloud_database(self) -> Menu:
    """
    A command that opens a submenu for dumping a cloud database to the local development environment.

    :return: A submenu of the cloud databases the project offers.
    """

    return self._cloud_database_menu('Dump production or staging to local database', self._dump_cloud_to_local)

dump_docker

A command that dumps the local Docker database to an SQL file.

This method dumps the running Docker database to a .sql file inside the project directory, which can then be used by the restore command.

Source code in dev_tool/commands/database.py
@ordered_command(label='Dump local database to file')
def dump_docker(self) -> None:
    """
    A command that dumps the local Docker database to an SQL file.

    This method dumps the running Docker database to a .sql file inside the
    project directory, which can then be used by the restore command.
    """

    file = self.postgres.get_dump_file_path()

    try:
        self.docker.ensure_container(recreate=False)
    except Exception as e:
        self.emit_error(f'Failed to ensure Docker container: {e}')
        return

    try:
        self.postgres.dump_docker_to_sql_file(file)
    except Exception as e:
        self.emit_error(f'Failed to dump Docker database to file: {e}')

restore_from_file

A command that opens a submenu for restoring databases from files.

This method returns a menu for selecting database files to restore from.

Returns:

  • Menu –

    A submenu for restoring databases from files.

Source code in dev_tool/commands/database.py
@ordered_submenu(label='Restore database from file')
def restore_from_file(self) -> Menu:
    """
    A command that opens a submenu for restoring databases from files.

    This method returns a menu for selecting database files to restore from.

    :return: A submenu for restoring databases from files.
    """

    files = list(
        chain(
            BASE.glob('*.sql'),
            BASE.glob('*.sql.encrypted')
        )
    )

    if not files:
        return Menu(title='Restore database from file', items=[])

    items: list[Menu | MenuItem] = []

    for file in files:
        if file.suffix == '.encrypted':
            command = partial(self._restore_from_encrypted, file)
        else:
            command = partial(self._restore_from_sql, file)

        action = CommandAction(command=command)
        item = MenuItem(label=file.name, action=action)
        items.append(item)

    items.sort(
        key=lambda item: item.label.lower()
    )

    return Menu(title='Restore database from file', items=items)