File: createrefschema.py

package info (click to toggle)
python-django-pgschemas 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 848 kB
  • sloc: python: 3,887; makefile: 33; sh: 10; sql: 2
file content (44 lines) | stat: -rw-r--r-- 1,765 bytes parent folder | download | duplicates (2)
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
from django.core.checks import Tags, run_checks
from django.core.management.base import BaseCommand, CommandError

from django_pgschemas.utils import create_schema, drop_schema, get_clone_reference


class Command(BaseCommand):
    help = "Creates the reference schema for faster dynamic tenant creation"

    def _run_checks(self, **kwargs):  # pragma: no cover
        issues = run_checks(tags=[Tags.database])
        issues.extend(super()._run_checks(**kwargs))
        return issues

    def add_arguments(self, parser):
        super().add_arguments(parser)
        parser.add_argument(
            "--recreate",
            action="store_true",
            dest="recreate",
            help="Recreate reference schema.",
        )

    def handle(self, *args, **options):
        clone_reference = get_clone_reference()
        if not clone_reference:
            raise CommandError("There is no reference schema configured.")
        if options.get("recreate", False):
            drop_schema(clone_reference, check_if_exists=True, verbosity=options["verbosity"])
            if options["verbosity"] >= 1:
                self.stdout.write("Destroyed existing reference schema.")
        created = create_schema(
            clone_reference, check_if_exists=True, verbosity=options["verbosity"]
        )
        if options["verbosity"] >= 1:
            if created:
                self.stdout.write("Reference schema successfully created!")
            else:
                self.stdout.write("Reference schema already exists.")
                self.stdout.write(
                    self.style.WARNING(
                        "Run this command again with --recreate if you want to recreate the reference schema."
                    )
                )