diff options
author | Aymeric Augustin <aymeric.augustin@m4x.org> | 2013-06-09 21:04:36 +0200 |
---|---|---|
committer | Aymeric Augustin <aymeric.augustin@m4x.org> | 2013-06-10 11:21:54 +0200 |
commit | 13b7f299de79e3eb101c3f015386eba39a8f3928 (patch) | |
tree | 84b45cf5fefcfab3b930aef073f8b856f84cc7fb /django/db/backends/postgresql_psycopg2/operations.py | |
parent | 7a65c95d42af421e036c5348f205128dd455a89c (diff) | |
download | django-13b7f299de79e3eb101c3f015386eba39a8f3928.tar.gz |
Added a stealth option to flush to allow cascades.
This allows using flush on a subset of the tables without having to
manually cascade to all tables with foreign keys to the tables being
truncated, when they're known to be empty.
On databases where truncate is implemented with DELETE FROM, this
doesn't make a difference. The cascade is allowed, not mandatory.
Diffstat (limited to 'django/db/backends/postgresql_psycopg2/operations.py')
-rw-r--r-- | django/db/backends/postgresql_psycopg2/operations.py | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/django/db/backends/postgresql_psycopg2/operations.py b/django/db/backends/postgresql_psycopg2/operations.py index f06eec5a1d..f96757da8b 100644 --- a/django/db/backends/postgresql_psycopg2/operations.py +++ b/django/db/backends/postgresql_psycopg2/operations.py @@ -101,15 +101,24 @@ class DatabaseOperations(BaseDatabaseOperations): def set_time_zone_sql(self): return "SET TIME ZONE %s" - def sql_flush(self, style, tables, sequences): + 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. - sql = ['%s %s;' % \ - (style.SQL_KEYWORD('TRUNCATE'), - style.SQL_FIELD(', '.join([self.quote_name(table) for table in tables])) - )] + 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: |