Commit 83098dcc authored by Aymeric Augustin's avatar Aymeric Augustin Committed by Tim Graham
Browse files

[1.6.x] Fixed #23089 -- Fixed transaction handling in two management commands.

Previously, when createcachetable and flush operated on non-default
databases, they weren't atomic.

Also avoided transactional DDL and transactional truncates on databases
that don't support them (refs #22308).

Backport of 753a22a6, 0757e0f3, and 6877a9d4 from master
parent 290e389f
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -53,7 +53,7 @@ class Command(LabelCommand):
        for i, line in enumerate(table_output):
            full_statement.append('    %s%s' % (line, ',' if i < len(table_output)-1 else ''))
        full_statement.append(');')
        with transaction.commit_on_success_unless_managed():
        with transaction.atomic(using=db, savepoint=connection.features.can_rollback_ddl):
            curs = connection.cursor()
            try:
                curs.execute("\n".join(full_statement))
+5 −4
Original line number Diff line number Diff line
@@ -28,8 +28,8 @@ class Command(NoArgsCommand):
           're-executed, and the initial_data fixture will be re-installed.')

    def handle_noargs(self, **options):
        db = options.get('database')
        connection = connections[db]
        database = options.get('database')
        connection = connections[database]
        verbosity = int(options.get('verbosity'))
        interactive = options.get('interactive')
        # The following are stealth options used by Django's internals.
@@ -63,7 +63,8 @@ Are you sure you want to do this?

        if confirm == 'yes':
            try:
                with transaction.commit_on_success_unless_managed():
                with transaction.atomic(using=database,
                                        savepoint=connection.features.can_rollback_ddl):
                    cursor = connection.cursor()
                    for sql in sql_list:
                        cursor.execute(sql)
@@ -78,7 +79,7 @@ Are you sure you want to do this?
                six.reraise(CommandError, CommandError(new_msg), sys.exc_info()[2])

            if not inhibit_post_syncdb:
                self.emit_post_syncdb(verbosity, interactive, db)
                self.emit_post_syncdb(verbosity, interactive, database)

            # Reinstall the initial_data fixture.
            if options.get('load_initial_data'):
+3 −0
Original line number Diff line number Diff line
@@ -652,6 +652,9 @@ class BaseDatabaseFeatures(object):
    # supported by the Python driver
    supports_paramstyle_pyformat = True

    # Can we roll back DDL in a transaction?
    can_rollback_ddl = False

    def __init__(self, connection):
        self.connection = connection

+1 −0
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
    supports_tablespaces = True
    supports_transactions = True
    can_distinct_on_fields = True
    can_rollback_ddl = True

class DatabaseWrapper(BaseDatabaseWrapper):
    vendor = 'postgresql'
+1 −0
Original line number Diff line number Diff line
@@ -102,6 +102,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
    can_combine_inserts_with_and_without_auto_increment_pk = False
    autocommits_when_autocommit_is_off = True
    atomic_transactions = False
    can_rollback_ddl = True
    supports_paramstyle_pyformat = False

    @cached_property
Loading