Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions tutorpostgresql/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@

import click
import importlib_resources
import shlex
import typing as t

from tutor import hooks

from .__about__ import __version__

########################################
# CONFIGURATION
########################################
config = {
config: dict[str, dict[str, t.Union[str, int]]] = {
"defaults": {
"VERSION": __version__,
"IMAGE": "postgres:14-alpine",
"HOST": "postgresql",
"PORT": 5432,
"ROOT_USER": "openedx",
"ROOT_USER": "postgres",
"OPENEDX_DB": "openedx",
"OPENEDX_USER": "openedx",
},
Expand Down Expand Up @@ -94,7 +97,9 @@
with open(full_path, encoding="utf-8") as init_task_file:
init_task: str = init_task_file.read()
# Raise the priority of the init job so that the DB is initialized before the migrations are applied
hooks.Filters.CLI_DO_INIT_TASKS.add_item((service, init_task), priority=hooks.priorities.HIGH)
hooks.Filters.CLI_DO_INIT_TASKS.add_item(
(service, init_task), priority=hooks.priorities.HIGH
)

########################################
# DOCKER IMAGE MANAGEMENT
Expand Down Expand Up @@ -213,6 +218,34 @@
# $ tutor local do say-hi --name="Qasim Gulzar"


@click.command(context_settings={"ignore_unknown_options": True})
@click.option(
"--database",
is_flag=False,
nargs=1,
default="{{ POSTGRESQL_OPENEDX_DB }}",
show_default=True,
required=True,
type=str,
help="Database to connect to when the shell executes.",
)
@click.argument("args", nargs=-1)
def postgresqlshell(database: str, args: list[str]) -> t.Iterable[tuple[str, str]]:
"""
Open an PostgreSQL shell as root connected to the openedx database

Extra arguments will be passed to the `postgressql` command verbatim. For instance, to
show tables from the "openedx" database, run `do postgresqlshell -c '\dt'.
"""
command = "psql postgresql://{{ POSTGRESQL_ROOT_USER }}:{{ POSTGRESQL_ROOT_PASSWORD }}@{{ POSTGRESQL_HOST }}:{{ POSTGRESQL_PORT }}"
command += f"/{database}"
if args:
command += " " + shlex.join(args) # pylint: disable=protected-access
yield ("postgresql", command)


hooks.Filters.CLI_DO_COMMANDS.add_item(postgresqlshell)

#######################################
# CUSTOM CLI COMMANDS
#######################################
Expand Down