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?')

        self.docker.ensure_container(recreate=False)
        self.postgres.dump_cloud_to_docker_database(redump, reuse)

backup_production

A command that backs up a production database to a .sql file.

Source code in dev_tool/commands/database.py
@ordered_command(label='Backup production database')
@is_developer_token
def backup_production(self) -> None:
    """A command that backs up a production database to a .sql file."""

    project = CONTEXT.configuration.get_project_name()
    database = os.getenv('DATABASE_NAME', 'localhost-project')

    self.portal.backup_database(project, database)

dump_production

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

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

    self.docker.ensure_container(recreate=False)

    CONTEXT.notification.flush()

    superuser = get_user_confirmation('Do you want to generate superuser(s)?')

    project = CONTEXT.configuration.get_project_name()
    database = os.getenv('DATABASE_NAME', 'localhost-project')

    identifier = self.portal.restore_database(project, database)

    if superuser:
        manager = TaskManager()

        manager.set_posthook(
            identifier,
            partial(self.django.run_django_create_user)
        )

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)