mirror of
https://github.com/django/django.git
synced 2025-08-04 02:48:35 +00:00
Fixed #22487: Optional rollback emulation for migrated apps
This commit is contained in:
parent
8721adcbfb
commit
8c12d51ea2
17 changed files with 246 additions and 26 deletions
0
tests/migration_test_data_persistence/__init__.py
Normal file
0
tests/migration_test_data_persistence/__init__.py
Normal file
|
@ -0,0 +1,34 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
def add_book(apps, schema_editor):
|
||||
apps.get_model("migration_test_data_persistence", "Book").objects.using(
|
||||
schema_editor.connection.alias,
|
||||
).create(
|
||||
title="I Love Django",
|
||||
)
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Book',
|
||||
fields=[
|
||||
('id', models.AutoField(verbose_name='ID', primary_key=True, serialize=False, auto_created=True)),
|
||||
('title', models.CharField(max_length=100)),
|
||||
],
|
||||
options={
|
||||
},
|
||||
bases=(models.Model,),
|
||||
),
|
||||
migrations.RunPython(
|
||||
add_book,
|
||||
),
|
||||
]
|
5
tests/migration_test_data_persistence/models.py
Normal file
5
tests/migration_test_data_persistence/models.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
from django.db import models
|
||||
|
||||
|
||||
class Book(models.Model):
|
||||
title = models.CharField(max_length=100)
|
33
tests/migration_test_data_persistence/tests.py
Normal file
33
tests/migration_test_data_persistence/tests.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
from django.test import TransactionTestCase
|
||||
from .models import Book
|
||||
|
||||
|
||||
class MigrationDataPersistenceTestCase(TransactionTestCase):
|
||||
"""
|
||||
Tests that data loaded in migrations is available if we set
|
||||
serialized_rollback = True.
|
||||
"""
|
||||
|
||||
available_apps = ["migration_test_data_persistence"]
|
||||
serialized_rollback = True
|
||||
|
||||
def test_persistence(self):
|
||||
self.assertEqual(
|
||||
Book.objects.count(),
|
||||
1,
|
||||
)
|
||||
|
||||
|
||||
class MigrationDataNoPersistenceTestCase(TransactionTestCase):
|
||||
"""
|
||||
Tests the failure case
|
||||
"""
|
||||
|
||||
available_apps = ["migration_test_data_persistence"]
|
||||
serialized_rollback = False
|
||||
|
||||
def test_no_persistence(self):
|
||||
self.assertEqual(
|
||||
Book.objects.count(),
|
||||
0,
|
||||
)
|
Loading…
Add table
Add a link
Reference in a new issue