Skip to content

Commit

Permalink
Move to utils, check in pre_migrate signal
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanCoding committed Aug 23, 2024
1 parent caea879 commit f9d2e28
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
10 changes: 10 additions & 0 deletions awx/main/apps.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
from django.apps import AppConfig
from django.utils.translation import gettext_lazy as _
from django.core.management.base import CommandError
from django.db.models.signals import pre_migrate

from awx.main.utils.named_url_graph import _customize_graph, generate_graph
from awx.main.utils.db import db_requirement_violations
from awx.conf import register, fields


class MainConfig(AppConfig):
name = 'awx.main'
verbose_name = _('Main')

def check_db_requirement(self, *args, **kwargs):
violations = db_requirement_violations()
if violations:
raise CommandError(violations)

def load_named_url_feature(self):
models = [m for m in self.get_models() if hasattr(m, 'get_absolute_url')]
generate_graph(models)
Expand Down Expand Up @@ -38,3 +47,4 @@ def ready(self):
super().ready()

self.load_named_url_feature()
pre_migrate.connect(self.check_db_requirement, sender=self)
28 changes: 10 additions & 18 deletions awx/main/management/commands/check_db.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
# Copyright (c) 2015 Ansible, Inc.
# All Rights Reserved

import sys

from django.core.management.base import BaseCommand
from django.core.management.base import BaseCommand, CommandError
from django.db import connection

from awx.main.utils.db import db_requirement_violations


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

def handle(self, *args, **options):
if connection.vendor == 'postgresql':

with connection.cursor() as cursor:
cursor.execute("SELECT version()")
version = str(cursor.fetchone()[0])
with connection.cursor() as cursor:
cursor.execute("SELECT version()")
version = str(cursor.fetchone()[0])

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

return "Database Version: {}".format(version)
else:
self.stderr.write(f"Running server with '{connection.vendor}' type database is not supported\n")
sys.exit(1)
return "Database Version: {}".format(version)
24 changes: 24 additions & 0 deletions awx/main/utils/db.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
# Copyright (c) 2017 Ansible by Red Hat
# All Rights Reserved.

from typing import Optional

from awx.settings.application_name import set_application_name
from awx import MODE

from django.conf import settings
from django.db import connection


def set_connection_name(function):
set_application_name(settings.DATABASES, settings.CLUSTER_HOST_ID, function=function)


MIN_PG_VERSION = 12


def db_requirement_violations() -> Optional[str]:
if connection.vendor == 'postgresql':

# enforce the postgres version is a minimum of 12 (we need this for partitioning); if not, then terminate program with exit code of 1
# In the future if we require a feature of a version of postgres > 12 this should be updated to reflect that.
# The return of connection.pg_version is something like 12013
major_version = connection.pg_version // 10000
if major_version < MIN_PG_VERSION:
return f"At a minimum, postgres version {MIN_PG_VERSION} is required, found {major_version}\n"

return None
else:
if MODE == 'production':
return f"Running server with '{connection.vendor}' type database is not supported\n"
return None

0 comments on commit f9d2e28

Please sign in to comment.