From 8005829bb9d9e0a14d73c9375bb55eb05daa46e1 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Fri, 17 Apr 2020 10:44:27 +0200 Subject: [PATCH] Simplified DatabaseOperations.sql_flush() on Oracle and PostgreSQL. Added early return to decrease an indentation level. --- django/db/backends/oracle/operations.py | 89 ++++++++++----------- django/db/backends/postgresql/operations.py | 40 ++++----- 2 files changed, 64 insertions(+), 65 deletions(-) diff --git a/django/db/backends/oracle/operations.py b/django/db/backends/oracle/operations.py index a712442f6e..0dc77aefc9 100644 --- a/django/db/backends/oracle/operations.py +++ b/django/db/backends/oracle/operations.py @@ -405,53 +405,52 @@ END; return lru_cache(maxsize=512)(self.__foreign_key_constraints) def sql_flush(self, style, tables, sequences, allow_cascade=False): - if tables: - truncated_tables = {table.upper() for table in tables} - constraints = set() - # Oracle's TRUNCATE CASCADE only works with ON DELETE CASCADE - # foreign keys which Django doesn't define. Emulate the - # PostgreSQL behavior which truncates all dependent tables by - # manually retrieving all foreign key constraints and resolving - # dependencies. - for table in tables: - for foreign_table, constraint in self._foreign_key_constraints(table, recursive=allow_cascade): - if allow_cascade: - truncated_tables.add(foreign_table) - constraints.add((foreign_table, constraint)) - sql = [ - "%s %s %s %s %s %s %s %s;" % ( - style.SQL_KEYWORD('ALTER'), - style.SQL_KEYWORD('TABLE'), - style.SQL_FIELD(self.quote_name(table)), - style.SQL_KEYWORD('DISABLE'), - style.SQL_KEYWORD('CONSTRAINT'), - style.SQL_FIELD(self.quote_name(constraint)), - style.SQL_KEYWORD('KEEP'), - style.SQL_KEYWORD('INDEX'), - ) for table, constraint in constraints - ] + [ - "%s %s %s;" % ( - style.SQL_KEYWORD('TRUNCATE'), - style.SQL_KEYWORD('TABLE'), - style.SQL_FIELD(self.quote_name(table)), - ) for table in truncated_tables - ] + [ - "%s %s %s %s %s %s;" % ( - style.SQL_KEYWORD('ALTER'), - style.SQL_KEYWORD('TABLE'), - style.SQL_FIELD(self.quote_name(table)), - style.SQL_KEYWORD('ENABLE'), - style.SQL_KEYWORD('CONSTRAINT'), - style.SQL_FIELD(self.quote_name(constraint)), - ) for table, constraint in constraints - ] - # Since we've just deleted all the rows, running our sequence - # ALTER code will reset the sequence to 0. - sql.extend(self.sequence_reset_by_name_sql(style, sequences)) - return sql - else: + if not tables: return [] + truncated_tables = {table.upper() for table in tables} + constraints = set() + # Oracle's TRUNCATE CASCADE only works with ON DELETE CASCADE foreign + # keys which Django doesn't define. Emulate the PostgreSQL behavior + # which truncates all dependent tables by manually retrieving all + # foreign key constraints and resolving dependencies. + for table in tables: + for foreign_table, constraint in self._foreign_key_constraints(table, recursive=allow_cascade): + if allow_cascade: + truncated_tables.add(foreign_table) + constraints.add((foreign_table, constraint)) + sql = [ + '%s %s %s %s %s %s %s %s;' % ( + style.SQL_KEYWORD('ALTER'), + style.SQL_KEYWORD('TABLE'), + style.SQL_FIELD(self.quote_name(table)), + style.SQL_KEYWORD('DISABLE'), + style.SQL_KEYWORD('CONSTRAINT'), + style.SQL_FIELD(self.quote_name(constraint)), + style.SQL_KEYWORD('KEEP'), + style.SQL_KEYWORD('INDEX'), + ) for table, constraint in constraints + ] + [ + '%s %s %s;' % ( + style.SQL_KEYWORD('TRUNCATE'), + style.SQL_KEYWORD('TABLE'), + style.SQL_FIELD(self.quote_name(table)), + ) for table in truncated_tables + ] + [ + '%s %s %s %s %s %s;' % ( + style.SQL_KEYWORD('ALTER'), + style.SQL_KEYWORD('TABLE'), + style.SQL_FIELD(self.quote_name(table)), + style.SQL_KEYWORD('ENABLE'), + style.SQL_KEYWORD('CONSTRAINT'), + style.SQL_FIELD(self.quote_name(constraint)), + ) for table, constraint in constraints + ] + # Since we've just deleted all the rows, running our sequence ALTER + # code will reset the sequence to 0. + sql.extend(self.sequence_reset_by_name_sql(style, sequences)) + return sql + def sequence_reset_by_name_sql(self, style, sequences): sql = [] for sequence_info in sequences: diff --git a/django/db/backends/postgresql/operations.py b/django/db/backends/postgresql/operations.py index ec87b61f49..dd422af1af 100644 --- a/django/db/backends/postgresql/operations.py +++ b/django/db/backends/postgresql/operations.py @@ -118,28 +118,28 @@ class DatabaseOperations(BaseDatabaseOperations): return "SET TIME ZONE %s" def sql_flush(self, style, tables, sequences, allow_cascade=False): - if tables: - # Perform a single SQL 'TRUNCATE x, y, z...;' statement. It allows - # us to truncate tables referenced by a foreign key in any other - # table. - tables_sql = ', '.join( - style.SQL_FIELD(self.quote_name(table)) for table in tables) - if allow_cascade: - sql = ['%s %s %s;' % ( - style.SQL_KEYWORD('TRUNCATE'), - tables_sql, - style.SQL_KEYWORD('CASCADE'), - )] - else: - sql = ['%s %s;' % ( - style.SQL_KEYWORD('TRUNCATE'), - tables_sql, - )] - sql.extend(self.sequence_reset_by_name_sql(style, sequences)) - return sql - else: + if not tables: return [] + # Perform a single SQL 'TRUNCATE x, y, z...;' statement. It allows us + # to truncate tables referenced by a foreign key in any other table. + tables_sql = ', '.join( + style.SQL_FIELD(self.quote_name(table)) for table in tables + ) + if allow_cascade: + sql = ['%s %s %s;' % ( + style.SQL_KEYWORD('TRUNCATE'), + tables_sql, + style.SQL_KEYWORD('CASCADE'), + )] + else: + sql = ['%s %s;' % ( + style.SQL_KEYWORD('TRUNCATE'), + tables_sql, + )] + sql.extend(self.sequence_reset_by_name_sql(style, sequences)) + return sql + def sequence_reset_by_name_sql(self, style, sequences): # 'ALTER SEQUENCE sequence_name RESTART WITH 1;'... style SQL statements # to reset sequence indices