Skip to content

Commit f9d2e28

Browse files
committed
Move to utils, check in pre_migrate signal
1 parent caea879 commit f9d2e28

File tree

3 files changed

+44
-18
lines changed

3 files changed

+44
-18
lines changed

awx/main/apps.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
from django.apps import AppConfig
22
from django.utils.translation import gettext_lazy as _
3+
from django.core.management.base import CommandError
4+
from django.db.models.signals import pre_migrate
5+
36
from awx.main.utils.named_url_graph import _customize_graph, generate_graph
7+
from awx.main.utils.db import db_requirement_violations
48
from awx.conf import register, fields
59

610

711
class MainConfig(AppConfig):
812
name = 'awx.main'
913
verbose_name = _('Main')
1014

15+
def check_db_requirement(self, *args, **kwargs):
16+
violations = db_requirement_violations()
17+
if violations:
18+
raise CommandError(violations)
19+
1120
def load_named_url_feature(self):
1221
models = [m for m in self.get_models() if hasattr(m, 'get_absolute_url')]
1322
generate_graph(models)
@@ -38,3 +47,4 @@ def ready(self):
3847
super().ready()
3948

4049
self.load_named_url_feature()
50+
pre_migrate.connect(self.check_db_requirement, sender=self)
Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
11
# Copyright (c) 2015 Ansible, Inc.
22
# All Rights Reserved
33

4-
import sys
5-
6-
from django.core.management.base import BaseCommand
4+
from django.core.management.base import BaseCommand, CommandError
75
from django.db import connection
86

7+
from awx.main.utils.db import db_requirement_violations
8+
99

1010
class Command(BaseCommand):
1111
"""Checks connection to the database, and prints out connection info if not connected"""
1212

1313
def handle(self, *args, **options):
14-
if connection.vendor == 'postgresql':
15-
16-
with connection.cursor() as cursor:
17-
cursor.execute("SELECT version()")
18-
version = str(cursor.fetchone()[0])
14+
with connection.cursor() as cursor:
15+
cursor.execute("SELECT version()")
16+
version = str(cursor.fetchone()[0])
1917

20-
# enforce the postgres version is a minimum of 12 (we need this for partitioning); if not, then terminate program with exit code of 1
21-
# In the future if we require a feature of a version of postgres > 12 this should be updated to reflect that.
22-
# The return of connection.pg_version is something like 12013
23-
if (connection.pg_version // 10000) < 12:
24-
self.stderr.write(f"At a minimum, postgres version 12 is required, found {version}\n")
25-
sys.exit(1)
18+
violations = db_requirement_violations()
19+
if violations:
20+
raise CommandError(violations)
2621

27-
return "Database Version: {}".format(version)
28-
else:
29-
self.stderr.write(f"Running server with '{connection.vendor}' type database is not supported\n")
30-
sys.exit(1)
22+
return "Database Version: {}".format(version)

awx/main/utils/db.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
11
# Copyright (c) 2017 Ansible by Red Hat
22
# All Rights Reserved.
33

4+
from typing import Optional
45

56
from awx.settings.application_name import set_application_name
7+
from awx import MODE
8+
69
from django.conf import settings
10+
from django.db import connection
711

812

913
def set_connection_name(function):
1014
set_application_name(settings.DATABASES, settings.CLUSTER_HOST_ID, function=function)
15+
16+
17+
MIN_PG_VERSION = 12
18+
19+
20+
def db_requirement_violations() -> Optional[str]:
21+
if connection.vendor == 'postgresql':
22+
23+
# enforce the postgres version is a minimum of 12 (we need this for partitioning); if not, then terminate program with exit code of 1
24+
# In the future if we require a feature of a version of postgres > 12 this should be updated to reflect that.
25+
# The return of connection.pg_version is something like 12013
26+
major_version = connection.pg_version // 10000
27+
if major_version < MIN_PG_VERSION:
28+
return f"At a minimum, postgres version {MIN_PG_VERSION} is required, found {major_version}\n"
29+
30+
return None
31+
else:
32+
if MODE == 'production':
33+
return f"Running server with '{connection.vendor}' type database is not supported\n"
34+
return None

0 commit comments

Comments
 (0)