From 5564d0f2ee9b33d30471eafef30efa71c7c47459 Mon Sep 17 00:00:00 2001 From: Adam Chainz Date: Wed, 1 Apr 2015 22:33:27 +0100 Subject: [PATCH] Fixed #24560 -- Added a --dry-run mode to the createcachetable command. --- .../management/commands/createcachetable.py | 20 +++++++++++++++---- docs/ref/django-admin.txt | 7 +++++++ docs/releases/1.9.txt | 3 +++ docs/topics/cache.txt | 3 +++ tests/cache/tests.py | 11 ++++++++++ 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/django/core/management/commands/createcachetable.py b/django/core/management/commands/createcachetable.py index 4c014c88fd..c1ec4539b6 100644 --- a/django/core/management/commands/createcachetable.py +++ b/django/core/management/commands/createcachetable.py @@ -22,21 +22,25 @@ class Command(BaseCommand): default=DEFAULT_DB_ALIAS, help='Nominates a database onto which the cache tables will be ' 'installed. Defaults to the "default" database.') + parser.add_argument('--dry-run', action='store_true', dest='dry_run', + help='Does not create the table, just prints the SQL that would ' + 'be run.') def handle(self, *tablenames, **options): db = options.get('database') self.verbosity = int(options.get('verbosity')) + dry_run = options.get('dry_run') if len(tablenames): # Legacy behavior, tablename specified as argument for tablename in tablenames: - self.create_table(db, tablename) + self.create_table(db, tablename, dry_run) else: for cache_alias in settings.CACHES: cache = caches[cache_alias] if isinstance(cache, BaseDatabaseCache): - self.create_table(db, cache._table) + self.create_table(db, cache._table, dry_run) - def create_table(self, database, tablename): + def create_table(self, database, tablename, dry_run): cache = BaseDatabaseCache(tablename, {}) if not router.allow_migrate_model(database, cache.cache_model_class): return @@ -74,11 +78,19 @@ class Command(BaseCommand): full_statement.append(' %s%s' % (line, ',' if i < len(table_output) - 1 else '')) full_statement.append(');') + full_statement = "\n".join(full_statement) + + if dry_run: + self.stdout.write(full_statement) + for statement in index_output: + self.stdout.write(statement) + return + with transaction.atomic(using=database, savepoint=connection.features.can_rollback_ddl): with connection.cursor() as curs: try: - curs.execute("\n".join(full_statement)) + curs.execute(full_statement) except DatabaseError as e: raise CommandError( "Cache table '%s' could not be created.\nThe error was: %s." % diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt index 3515c615e7..2b68f6575d 100644 --- a/docs/ref/django-admin.txt +++ b/docs/ref/django-admin.txt @@ -189,6 +189,13 @@ The :djadminopt:`--database` option can be used to specify the database onto which the cache table will be installed, but since this information is pulled from your settings by default, it's typically not needed. +The :djadminopt:`--dry-run` option will print the SQL that would be run without +actually running it, so you can customize it or use the migrations framework. + +.. versionchanged:: 1.9 + + The ``--dry-run`` option was added. + dbshell ------- diff --git a/docs/releases/1.9.txt b/docs/releases/1.9.txt index cd124bf602..d89354ea6a 100644 --- a/docs/releases/1.9.txt +++ b/docs/releases/1.9.txt @@ -166,6 +166,9 @@ Management Commands * The :djadmin:`dumpdata` command output is now deterministically ordered. +* The :djadmin:`createcachetable` command now has a ``--dry-run`` flag to + print out the SQL rather than execute it. + Models ^^^^^^ diff --git a/docs/topics/cache.txt b/docs/topics/cache.txt index ec973a5008..1a21eab164 100644 --- a/docs/topics/cache.txt +++ b/docs/topics/cache.txt @@ -206,6 +206,9 @@ If you are using multiple databases, :djadmin:`createcachetable` observes the Like :djadmin:`migrate`, :djadmin:`createcachetable` won't touch an existing table. It will only create missing tables. +To print the SQL that would be run, rather than run it, use the +:djadminopt:`--dry-run` option. + Multiple databases ~~~~~~~~~~~~~~~~~~ diff --git a/tests/cache/tests.py b/tests/cache/tests.py index ffbed67197..9d29239cf6 100644 --- a/tests/cache/tests.py +++ b/tests/cache/tests.py @@ -943,6 +943,17 @@ class DBCacheTests(BaseCacheTests, TransactionTestCase): self.assertEqual(out.getvalue(), "Cache table 'test cache table' already exists.\n" * len(settings.CACHES)) + @override_settings(CACHES=caches_setting_for_tests( + BACKEND='django.core.cache.backends.db.DatabaseCache', + # Use another table name to avoid the 'table already exists' message. + LOCATION='createcachetable_dry_run_mode' + )) + def test_createcachetable_dry_run_mode(self): + out = six.StringIO() + management.call_command('createcachetable', dry_run=True, stdout=out) + output = out.getvalue() + self.assertTrue(output.startswith("CREATE TABLE")) + def test_createcachetable_with_table_argument(self): """ Delete and recreate cache table with legacy behavior (explicitly