forked from ansible/django-ansible-base
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix resource registry migrations (ansible#119)
- Loading branch information
1 parent
502a9c5
commit 3eb2b78
Showing
10 changed files
with
109 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
from .resource import Resource, ResourceType # noqa: 401 | ||
from .resource import Resource, ResourceType, init_resource_from_object # noqa: 401 | ||
from .service_id import service_id # noqa: 401 |
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,43 @@ | ||
# Generated by Django 4.2.8 on 2024-01-26 11:03 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
def create_test_data(apps, schema_editor): | ||
""" | ||
This migration sets up some resource instances after the resource registry is | ||
created. This is to test two things: | ||
1. That the post_save and post_delete signals in the resources app do not | ||
fire during migrations. | ||
2. That the post migration signal to initialize resource instances from | ||
existing objects works correctly. | ||
""" | ||
ResourceMigrationTestModel = apps.get_model("test_app", "ResourceMigrationTestModel") | ||
Resource = apps.get_model("dab_resource_registry", "Resource") | ||
|
||
ResourceMigrationTestModel.objects.create(name="migration resource") | ||
r2 = ResourceMigrationTestModel.objects.create(name="migration resource 2") | ||
|
||
assert Resource.objects.all().count() == 0 | ||
|
||
r2.delete() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
('test_app', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='ResourceMigrationTestModel', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('name', models.CharField(max_length=255)), | ||
], | ||
), | ||
migrations.RunPython( | ||
code=create_test_data, | ||
reverse_code=migrations.RunPython.noop | ||
), | ||
] |
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,12 @@ | ||
import pytest | ||
|
||
from ansible_base.resource_registry.models import Resource | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_existing_resources_created_in_post_migration(): | ||
""" | ||
Test that resources that existed before the registry was added got | ||
created successfully. | ||
""" | ||
assert Resource.objects.filter(name="migration resource", content_type__resource_type__name="aap.resourcemigrationtestmodel").exists() |
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