diff options
| author | Calvin DeBoer <cdeboer@horizoninvestments.com> | 2018-05-19 11:38:02 -0400 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2018-08-03 15:22:42 -0400 |
| commit | 058d33f3eddef950e4266ea942d39b1df95ee5de (patch) | |
| tree | 2fa903a5f7048153a0e78696a1dda71a2a04a363 /tests | |
| parent | 1160a975968f86953a6c4405b772eb86283c5a4a (diff) | |
Fixed #29198 -- Added migrate --plan option.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/migrations/test_commands.py | 67 | ||||
| -rw-r--r-- | tests/migrations/test_migrations_plan/0001_initial.py | 28 | ||||
| -rw-r--r-- | tests/migrations/test_migrations_plan/0002_second.py | 20 | ||||
| -rw-r--r-- | tests/migrations/test_migrations_plan/0003_third.py | 19 | ||||
| -rw-r--r-- | tests/migrations/test_migrations_plan/0004_fourth.py | 12 | ||||
| -rw-r--r-- | tests/migrations/test_migrations_plan/__init__.py | 0 |
6 files changed, 146 insertions, 0 deletions
diff --git a/tests/migrations/test_commands.py b/tests/migrations/test_commands.py index 8276049dc6..3bc37b6c15 100644 --- a/tests/migrations/test_commands.py +++ b/tests/migrations/test_commands.py @@ -298,6 +298,73 @@ class MigrateTests(MigrationTestBase): # Cleanup by unmigrating everything call_command("migrate", "migrations", "zero", verbosity=0) + @override_settings(MIGRATION_MODULES={'migrations': 'migrations.test_migrations_plan'}) + def test_migrate_plan(self): + """Tests migrate --plan output.""" + out = io.StringIO() + # Show the plan up to the third migration. + call_command('migrate', 'migrations', '0003', plan=True, stdout=out, no_color=True) + self.assertEqual( + 'Planned operations:\n' + 'migrations.0001_initial\n' + ' Create model Salamander\n' + ' Raw Python operation -> Grow salamander tail.\n' + 'migrations.0002_second\n' + ' Create model Book\n' + ' Raw SQL operation -> SELECT * FROM migrations_book\n' + 'migrations.0003_third\n' + ' Create model Author\n' + ' Raw SQL operation -> SELECT * FROM migrations_author\n', + out.getvalue() + ) + # Migrate to the third migration. + call_command('migrate', 'migrations', '0003', verbosity=0) + out = io.StringIO() + # Show the plan for when there is nothing to apply. + call_command('migrate', 'migrations', '0003', plan=True, stdout=out, no_color=True) + self.assertEqual( + 'Planned operations:\n' + ' No planned migration operations.\n', + out.getvalue() + ) + out = io.StringIO() + # Show the plan for reverse migration back to 0001. + call_command('migrate', 'migrations', '0001', plan=True, stdout=out, no_color=True) + self.assertEqual( + 'Planned operations:\n' + 'migrations.0003_third\n' + ' Undo Create model Author\n' + ' Raw SQL operation -> SELECT * FROM migrations_book\n' + 'migrations.0002_second\n' + ' Undo Create model Book\n' + ' Raw SQL operation -> SELECT * FROM migrations_salamander\n', + out.getvalue() + ) + out = io.StringIO() + # Show the migration plan to fourth, with truncated details. + call_command('migrate', 'migrations', '0004', plan=True, stdout=out, no_color=True) + self.assertEqual( + 'Planned operations:\n' + 'migrations.0004_fourth\n' + ' Raw SQL operation -> SELECT * FROM migrations_author W...\n', + out.getvalue() + ) + # Migrate to the fourth migration. + call_command('migrate', 'migrations', '0004', verbosity=0) + out = io.StringIO() + # Show the plan when an operation is irreversible. + call_command('migrate', 'migrations', '0003', plan=True, stdout=out, no_color=True) + self.assertEqual( + 'Planned operations:\n' + 'migrations.0004_fourth\n' + ' Raw SQL operation -> IRREVERSIBLE\n', + out.getvalue() + ) + # Cleanup by unmigrating everything: fake the irreversible, then + # migrate all to zero. + call_command('migrate', 'migrations', '0003', fake=True, verbosity=0) + call_command('migrate', 'migrations', 'zero', verbosity=0) + @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_empty"}) def test_showmigrations_plan_no_migrations(self): """ diff --git a/tests/migrations/test_migrations_plan/0001_initial.py b/tests/migrations/test_migrations_plan/0001_initial.py new file mode 100644 index 0000000000..0a4001d52a --- /dev/null +++ b/tests/migrations/test_migrations_plan/0001_initial.py @@ -0,0 +1,28 @@ +from django.db import migrations, models + + +def grow_tail(x, y): + """Grow salamander tail.""" + pass + + +def shrink_tail(x, y): + """Shrink salamander tail.""" + pass + + +class Migration(migrations.Migration): + + initial = True + + operations = [ + migrations.CreateModel( + 'Salamander', + [ + ('id', models.AutoField(primary_key=True)), + ('tail', models.IntegerField(default=0)), + ('silly_field', models.BooleanField(default=False)), + ], + ), + migrations.RunPython(grow_tail, shrink_tail), + ] diff --git a/tests/migrations/test_migrations_plan/0002_second.py b/tests/migrations/test_migrations_plan/0002_second.py new file mode 100644 index 0000000000..e8aeec880b --- /dev/null +++ b/tests/migrations/test_migrations_plan/0002_second.py @@ -0,0 +1,20 @@ +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('migrations', '0001_initial'), + ] + + operations = [ + + migrations.CreateModel( + 'Book', + [ + ('id', models.AutoField(primary_key=True)), + ], + ), + migrations.RunSQL('SELECT * FROM migrations_book', 'SELECT * FROM migrations_salamander') + + ] diff --git a/tests/migrations/test_migrations_plan/0003_third.py b/tests/migrations/test_migrations_plan/0003_third.py new file mode 100644 index 0000000000..d045a91448 --- /dev/null +++ b/tests/migrations/test_migrations_plan/0003_third.py @@ -0,0 +1,19 @@ +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('migrations', '0002_second'), + ] + + operations = [ + + migrations.CreateModel( + 'Author', + [ + ('id', models.AutoField(primary_key=True)), + ], + ), + migrations.RunSQL('SELECT * FROM migrations_author', 'SELECT * FROM migrations_book') + ] diff --git a/tests/migrations/test_migrations_plan/0004_fourth.py b/tests/migrations/test_migrations_plan/0004_fourth.py new file mode 100644 index 0000000000..d3e1a54b4d --- /dev/null +++ b/tests/migrations/test_migrations_plan/0004_fourth.py @@ -0,0 +1,12 @@ +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("migrations", "0003_third"), + ] + + operations = [ + migrations.RunSQL('SELECT * FROM migrations_author WHERE id = 1') + ] diff --git a/tests/migrations/test_migrations_plan/__init__.py b/tests/migrations/test_migrations_plan/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/migrations/test_migrations_plan/__init__.py |
