Skip to content

Commit

Permalink
Add 'pgrefreshmv' command to refresh a materialized view
Browse files Browse the repository at this point in the history
  • Loading branch information
Photonios committed Nov 3, 2019
1 parent 3a13758 commit 37e5135
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
12 changes: 5 additions & 7 deletions psqlextra/management/commands/pgautopartition.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import structlog

from django.apps import apps
from django.core.management.base import BaseCommand

Expand All @@ -9,8 +7,6 @@
)
from psqlextra.models import PostgresPartitionedModel

LOGGER = structlog.get_logger(__name__)


class PostgresAutoPartitioningError(RuntimeError):
pass
Expand All @@ -24,24 +20,25 @@ class Command(BaseCommand):

def add_arguments(self, parser):
parser.add_argument(
"--app-label",
"-a",
"app-label",
type=str,
help="Label of the app the partitioned model is in.",
required=True,
)

parser.add_argument(
"model_name",
type=str,
help="Name of the partitioned model to auto partition for.",
)

parser.add_argument(
"--count",
"-c",
type=int,
help="Amount of partitions to create in ahead of the current date.",
default=1,
)

parser.add_argument(
"--interval-unit",
"-u",
Expand All @@ -50,6 +47,7 @@ def add_arguments(self, parser):
help="Unit in which to express the interval (months/weeks/days).",
default="month",
)

parser.add_argument(
"--interval",
"-i",
Expand Down
50 changes: 50 additions & 0 deletions psqlextra/management/commands/pgrefreshmv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

from django.apps import apps
from django.core.management.base import BaseCommand
from django.db.utils import NotSupportedError, OperationalError

from psqlextra.models import PostgresMaterializedViewModel


class Command(BaseCommand):
"""Refreshes a :see:PostgresMaterializedViewModel."""

help = "Refreshes the specified materialized view."

def add_arguments(self, parser):
parser.add_argument(
"app_label",
type=str,
help="Label of the app the materialized view model is in.",
)

parser.add_argument(
"model_name",
type=str,
help="Name of the materialized view model to refresh.",
)

parser.add_argument(
"--concurrently",
"-c",
action="store_true",
help="Whether to refresh the materialized view model concurrently.",
required=False,
default=False,
)

def handle(self, *app_labels, **options):
app_label = options.get("app_label")
model_name = options.get("model_name")
concurrently = options.get("concurrently")

model = apps.get_model(app_label, model_name)
if not model:
raise OperationalError(f"Cannot find a model named '{model_name}'")

if not issubclass(model, PostgresMaterializedViewModel):
raise NotSupportedError(
f"Model {model.__name__} is not a `PostgresMaterializedViewModel`"
)

model.refresh(concurrently=concurrently)

0 comments on commit 37e5135

Please sign in to comment.