forked from ansible/awx
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Register CredentialType(s) every time Django loads
* Register all discovered CredentialType(s) after Django finishes loading * Protect parallel registrations using shared postgres advisory lock * The down-side of this is that this will run when it does not need to, adding overhead to the init process. * Only register discovered credential types in the database IF migrations have ran and are up-to-date.
- Loading branch information
1 parent
71856d6
commit 490db08
Showing
9 changed files
with
184 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import pytest | ||
|
||
from django.apps import apps | ||
|
||
|
||
@pytest.fixture | ||
def mock_setup_tower_managed_defaults(mocker): | ||
return mocker.patch('awx.main.models.credential.CredentialType.setup_tower_managed_defaults') | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_load_credential_types_feature_migrations_ran(mocker, mock_setup_tower_managed_defaults): | ||
mocker.patch('awx.main.apps.is_database_synchronized', return_value=True) | ||
|
||
apps.get_app_config('main')._load_credential_types_feature() | ||
|
||
mock_setup_tower_managed_defaults.assert_called_once() | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_load_credential_types_feature_migrations_not_ran(mocker, mock_setup_tower_managed_defaults): | ||
mocker.patch('awx.main.apps.is_database_synchronized', return_value=False) | ||
|
||
apps.get_app_config('main')._load_credential_types_feature() | ||
|
||
mock_setup_tower_managed_defaults.assert_not_called() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from django.db.migrations.executor import MigrationExecutor | ||
from django.db import connections, DEFAULT_DB_ALIAS | ||
|
||
|
||
def is_database_synchronized(database=DEFAULT_DB_ALIAS): | ||
"""_summary_ | ||
Ensure all migrations have ran | ||
https://stackoverflow.com/questions/31838882/check-for-pending-django-migrations | ||
""" | ||
connection = connections[database] | ||
connection.prepare_database() | ||
executor = MigrationExecutor(connection) | ||
targets = executor.loader.graph.leaf_nodes() | ||
return not executor.migration_plan(targets) |