Commit 5564d0f2 authored by Adam Chainz's avatar Adam Chainz Committed by Tim Graham
Browse files

Fixed #24560 -- Added a --dry-run mode to the createcachetable command.

parent 727e40c8
Loading
Loading
Loading
Loading
+16 −4
Original line number Diff line number Diff line
@@ -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." %
+7 −0
Original line number Diff line number Diff line
@@ -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
-------

+3 −0
Original line number Diff line number Diff line
@@ -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
^^^^^^

+3 −0
Original line number Diff line number Diff line
@@ -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
~~~~~~~~~~~~~~~~~~

+11 −0
Original line number Diff line number Diff line
@@ -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