Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move PG version check to awx-manage check_db & migrate commands #15463

Open
wants to merge 1 commit into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions awx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,6 @@ def version_file():
MODE = 'production'


try:
import django # noqa: F401
except ImportError:
pass
else:
from django.db import connection


def prepare_env():
# Update the default settings environment variable based on current mode.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'awx.settings.%s' % MODE)
Expand All @@ -78,14 +70,6 @@ def manage():
from django.conf import settings
from django.core.management import execute_from_command_line

# 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 not os.getenv('SKIP_PG_VERSION_CHECK', False) and not MODE == 'development':
if (connection.pg_version // 10000) < 12:
sys.stderr.write("At a minimum, postgres version 12 is required\n")
sys.exit(1)

if len(sys.argv) >= 2 and sys.argv[1] in ('version', '--version'): # pragma: no cover
sys.stdout.write('%s\n' % __version__)
# If running as a user without permission to read settings, display an
Expand Down
10 changes: 10 additions & 0 deletions awx/main/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

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.common import bypass_in_test, load_all_entry_points_for
from awx.main.utils.migration import is_database_synchronized
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

from awx_plugins.interfaces._temporary_private_licensing_api import detect_server_product_name
Expand All @@ -14,6 +18,11 @@
name = 'awx.main'
verbose_name = _('Main')

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

Check warning on line 24 in awx/main/apps.py

View check run for this annotation

Codecov / codecov/patch

awx/main/apps.py#L24

Added line #L24 was not covered by tests

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 @@ -85,3 +94,4 @@
self.load_credential_types_feature()
self.load_named_url_feature()
self.load_inventory_plugins()
pre_migrate.connect(self.check_db_requirement, sender=self)
8 changes: 7 additions & 1 deletion awx/main/management/commands/check_db.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Copyright (c) 2015 Ansible, Inc.
# All Rights Reserved

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

Check warning on line 4 in awx/main/management/commands/check_db.py

View check run for this annotation

Codecov / codecov/patch

awx/main/management/commands/check_db.py#L4

Added line #L4 was not covered by tests
from django.db import connection

from awx.main.utils.db import db_requirement_violations

Check warning on line 7 in awx/main/management/commands/check_db.py

View check run for this annotation

Codecov / codecov/patch

awx/main/management/commands/check_db.py#L7

Added line #L7 was not covered by tests


class Command(BaseCommand):
"""Checks connection to the database, and prints out connection info if not connected"""
Expand All @@ -13,4 +15,8 @@
cursor.execute("SELECT version()")
version = str(cursor.fetchone()[0])

violations = db_requirement_violations()

Check warning on line 18 in awx/main/management/commands/check_db.py

View check run for this annotation

Codecov / codecov/patch

awx/main/management/commands/check_db.py#L18

Added line #L18 was not covered by tests
if violations:
raise CommandError(violations)

Check warning on line 20 in awx/main/management/commands/check_db.py

View check run for this annotation

Codecov / codecov/patch

awx/main/management/commands/check_db.py#L20

Added line #L20 was not covered by tests

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

Check warning on line 26 in awx/main/utils/db.py

View check run for this annotation

Codecov / codecov/patch

awx/main/utils/db.py#L26

Added line #L26 was not covered by tests
if major_version < MIN_PG_VERSION:
return f"At a minimum, postgres version {MIN_PG_VERSION} is required, found {major_version}\n"

Check warning on line 28 in awx/main/utils/db.py

View check run for this annotation

Codecov / codecov/patch

awx/main/utils/db.py#L28

Added line #L28 was not covered by tests

return None

Check warning on line 30 in awx/main/utils/db.py

View check run for this annotation

Codecov / codecov/patch

awx/main/utils/db.py#L30

Added line #L30 was not covered by tests
else:
if MODE == 'production':
return f"Running server with '{connection.vendor}' type database is not supported\n"

Check warning on line 33 in awx/main/utils/db.py

View check run for this annotation

Codecov / codecov/patch

awx/main/utils/db.py#L33

Added line #L33 was not covered by tests
return None
Loading