File: pgrefreshmv.py

package info (click to toggle)
python-django-postgres-extra 2.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,096 kB
  • sloc: python: 9,057; makefile: 17; sh: 7; sql: 1
file content (49 lines) | stat: -rw-r--r-- 1,556 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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)