Changeset 5963
- Timestamp:
- 08/19/07 19:15:53 (1 year ago)
- Files:
-
- django/trunk/django/core/management/sql.py (modified) (1 diff)
- django/trunk/django/db/backends/ado_mssql/base.py (modified) (1 diff)
- django/trunk/django/db/backends/dummy/base.py (modified) (1 diff)
- django/trunk/django/db/backends/__init__.py (modified) (1 diff)
- django/trunk/django/db/backends/mysql/base.py (modified) (2 diffs)
- django/trunk/django/db/backends/mysql_old/base.py (modified) (2 diffs)
- django/trunk/django/db/backends/oracle/base.py (modified) (2 diffs)
- django/trunk/django/db/backends/postgresql/base.py (modified) (2 diffs)
- django/trunk/django/db/backends/postgresql_psycopg2/base.py (modified) (2 diffs)
- django/trunk/django/db/backends/sqlite3/base.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/core/management/sql.py
r5960 r5963 179 179 180 180 def sql_flush(style): 181 "Returns a list of the SQL statements used to flush the database "182 from django.db import backend183 statements = backend.get_sql_flush(style, table_list(), sequence_list())181 "Returns a list of the SQL statements used to flush the database." 182 from django.db import connection 183 statements = connection.ops.sql_flush(style, table_list(), sequence_list()) 184 184 return statements 185 185 django/trunk/django/db/backends/ado_mssql/base.py
r5962 r5963 110 110 return "ON %s" % quote_name(tablespace) 111 111 112 def get_sql_flush(style, tables, sequences):113 """Return a list of SQL statements required to remove all data from114 all tables in the database (without actually removing the tables115 themselves) and put the database in an empty 'initial' state116 """117 # Return a list of 'TRUNCATE x;', 'TRUNCATE y;', 'TRUNCATE z;'... style SQL statements118 # TODO - SQL not actually tested against ADO MSSQL yet!119 # TODO - autoincrement indices reset required? See other get_sql_flush() implementations120 sql_list = ['%s %s;' % \121 (style.SQL_KEYWORD('TRUNCATE'),122 style.SQL_FIELD(quote_name(table))123 ) for table in tables]124 125 112 def get_sql_sequence_reset(style, model_list): 126 113 "Returns a list of the SQL statements to reset sequences for the given models." django/trunk/django/db/backends/dummy/base.py
r5962 r5963 45 45 dictfetchall = complain 46 46 get_start_transaction_sql = complain 47 get_sql_flush = complain48 47 get_sql_sequence_reset = complain 49 48 django/trunk/django/db/backends/__init__.py
r5962 r5963 140 140 """ 141 141 return 'RANDOM()' 142 143 def sql_flush(self, style, tables, sequences): 144 """ 145 Returns a list of SQL statements required to remove all data from 146 the given database tables (without actually removing the tables 147 themselves). 148 149 The `style` argument is a Style object as returned by either 150 color_style() or no_style() in django.core.management.color. 151 """ 152 raise NotImplementedError() django/trunk/django/db/backends/mysql/base.py
r5962 r5963 87 87 def random_function_sql(self): 88 88 return 'RAND()' 89 90 def sql_flush(self, style, tables, sequences): 91 # NB: The generated SQL below is specific to MySQL 92 # 'TRUNCATE x;', 'TRUNCATE y;', 'TRUNCATE z;'... style SQL statements 93 # to clear all tables of all data 94 if tables: 95 sql = ['SET FOREIGN_KEY_CHECKS = 0;'] 96 for table in tables: 97 sql.append('%s %s;' % (style.SQL_KEYWORD('TRUNCATE'), style.SQL_FIELD(quote_name(table)))) 98 sql.append('SET FOREIGN_KEY_CHECKS = 1;') 99 100 # 'ALTER TABLE table AUTO_INCREMENT = 1;'... style SQL statements 101 # to reset sequence indices 102 sql.extend(["%s %s %s %s %s;" % \ 103 (style.SQL_KEYWORD('ALTER'), 104 style.SQL_KEYWORD('TABLE'), 105 style.SQL_TABLE(quote_name(sequence['table'])), 106 style.SQL_KEYWORD('AUTO_INCREMENT'), 107 style.SQL_FIELD('= 1'), 108 ) for sequence in sequences]) 109 return sql 110 else: 111 return [] 89 112 90 113 class DatabaseWrapper(BaseDatabaseWrapper): … … 169 192 return "BEGIN;" 170 193 171 def get_sql_flush(style, tables, sequences):172 """Return a list of SQL statements required to remove all data from173 all tables in the database (without actually removing the tables174 themselves) and put the database in an empty 'initial' state175 176 """177 # NB: The generated SQL below is specific to MySQL178 # 'TRUNCATE x;', 'TRUNCATE y;', 'TRUNCATE z;'... style SQL statements179 # to clear all tables of all data180 if tables:181 sql = ['SET FOREIGN_KEY_CHECKS = 0;'] + \182 ['%s %s;' % \183 (style.SQL_KEYWORD('TRUNCATE'),184 style.SQL_FIELD(quote_name(table))185 ) for table in tables] + \186 ['SET FOREIGN_KEY_CHECKS = 1;']187 188 # 'ALTER TABLE table AUTO_INCREMENT = 1;'... style SQL statements189 # to reset sequence indices190 sql.extend(["%s %s %s %s %s;" % \191 (style.SQL_KEYWORD('ALTER'),192 style.SQL_KEYWORD('TABLE'),193 style.SQL_TABLE(quote_name(sequence['table'])),194 style.SQL_KEYWORD('AUTO_INCREMENT'),195 style.SQL_FIELD('= 1'),196 ) for sequence in sequences])197 return sql198 else:199 return []200 201 194 def get_sql_sequence_reset(style, model_list): 202 195 "Returns a list of the SQL statements to reset sequences for the given models." django/trunk/django/db/backends/mysql_old/base.py
r5962 r5963 97 97 def random_function_sql(self): 98 98 return 'RAND()' 99 100 def sql_flush(self, style, tables, sequences): 101 # NB: The generated SQL below is specific to MySQL 102 # 'TRUNCATE x;', 'TRUNCATE y;', 'TRUNCATE z;'... style SQL statements 103 # to clear all tables of all data 104 if tables: 105 sql = ['SET FOREIGN_KEY_CHECKS = 0;'] 106 for table in tables: 107 sql.append('%s %s;' % (style.SQL_KEYWORD('TRUNCATE'), style.SQL_FIELD(quote_name(table)))) 108 sql.append('SET FOREIGN_KEY_CHECKS = 1;') 109 110 # 'ALTER TABLE table AUTO_INCREMENT = 1;'... style SQL statements 111 # to reset sequence indices 112 sql.extend(["%s %s %s %s %s;" % \ 113 (style.SQL_KEYWORD('ALTER'), 114 style.SQL_KEYWORD('TABLE'), 115 style.SQL_TABLE(quote_name(sequence['table'])), 116 style.SQL_KEYWORD('AUTO_INCREMENT'), 117 style.SQL_FIELD('= 1'), 118 ) for sequence in sequences]) 119 return sql 120 else: 121 return [] 99 122 100 123 class DatabaseWrapper(BaseDatabaseWrapper): … … 188 211 return "BEGIN;" 189 212 190 def get_sql_flush(style, tables, sequences):191 """Return a list of SQL statements required to remove all data from192 all tables in the database (without actually removing the tables193 themselves) and put the database in an empty 'initial' state194 195 """196 # NB: The generated SQL below is specific to MySQL197 # 'TRUNCATE x;', 'TRUNCATE y;', 'TRUNCATE z;'... style SQL statements198 # to clear all tables of all data199 if tables:200 sql = ['SET FOREIGN_KEY_CHECKS = 0;'] + \201 ['%s %s;' % \202 (style.SQL_KEYWORD('TRUNCATE'),203 style.SQL_FIELD(quote_name(table))204 ) for table in tables] + \205 ['SET FOREIGN_KEY_CHECKS = 1;']206 207 # 'ALTER TABLE table AUTO_INCREMENT = 1;'... style SQL statements208 # to reset sequence indices209 sql.extend(["%s %s %s %s %s;" % \210 (style.SQL_KEYWORD('ALTER'),211 style.SQL_KEYWORD('TABLE'),212 style.SQL_TABLE(quote_name(sequence['table'])),213 style.SQL_KEYWORD('AUTO_INCREMENT'),214 style.SQL_FIELD('= 1'),215 ) for sequence in sequences])216 return sql217 else:218 return []219 220 213 def get_sql_sequence_reset(style, model_list): 221 214 "Returns a list of the SQL statements to reset sequences for the given models." django/trunk/django/db/backends/oracle/base.py
r5962 r5963 73 73 def random_function_sql(self): 74 74 return "DBMS_RANDOM.RANDOM" 75 76 def sql_flush(self, style, tables, sequences): 77 # Return a list of 'TRUNCATE x;', 'TRUNCATE y;', 78 # 'TRUNCATE z;'... style SQL statements 79 if tables: 80 # Oracle does support TRUNCATE, but it seems to get us into 81 # FK referential trouble, whereas DELETE FROM table works. 82 sql = ['%s %s %s;' % \ 83 (style.SQL_KEYWORD('DELETE'), 84 style.SQL_KEYWORD('FROM'), 85 style.SQL_FIELD(quote_name(table)) 86 ) for table in tables] 87 # Since we've just deleted all the rows, running our sequence 88 # ALTER code will reset the sequence to 0. 89 for sequence_info in sequences: 90 table_name = sequence_info['table'] 91 seq_name = get_sequence_name(table_name) 92 query = _get_sequence_reset_sql() % {'sequence':seq_name, 'table':quote_name(table_name)} 93 sql.append(query) 94 return sql 95 else: 96 return [] 75 97 76 98 class DatabaseWrapper(BaseDatabaseWrapper): … … 218 240 END; 219 241 /""" 220 221 def get_sql_flush(style, tables, sequences):222 """Return a list of SQL statements required to remove all data from223 all tables in the database (without actually removing the tables224 themselves) and put the database in an empty 'initial' state225 """226 # Return a list of 'TRUNCATE x;', 'TRUNCATE y;',227 # 'TRUNCATE z;'... style SQL statements228 if tables:229 # Oracle does support TRUNCATE, but it seems to get us into230 # FK referential trouble, whereas DELETE FROM table works.231 sql = ['%s %s %s;' % \232 (style.SQL_KEYWORD('DELETE'),233 style.SQL_KEYWORD('FROM'),234 style.SQL_FIELD(quote_name(table))235 ) for table in tables]236 # Since we've just deleted all the rows, running our sequence237 # ALTER code will reset the sequence to 0.238 for sequence_info in sequences:239 table_name = sequence_info['table']240 seq_name = get_sequence_name(table_name)241 query = _get_sequence_reset_sql() % {'sequence':seq_name,242 'table':quote_name(table_name)}243 sql.append(query)244 return sql245 else:246 return []247 242 248 243 def get_sequence_name(table): django/trunk/django/db/backends/postgresql/base.py
r5962 r5963 73 73 cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (table_name, pk_name)) 74 74 return cursor.fetchone()[0] 75 76 def sql_flush(self, style, tables, sequences): 77 if tables: 78 if postgres_version[0] >= 8 and postgres_version[1] >= 1: 79 # Postgres 8.1+ can do 'TRUNCATE x, y, z...;'. In fact, it *has to* 80 # in order to be able to truncate tables referenced by a foreign 81 # key in any other table. The result is a single SQL TRUNCATE 82 # statement. 83 sql = ['%s %s;' % \ 84 (style.SQL_KEYWORD('TRUNCATE'), 85 style.SQL_FIELD(', '.join([quote_name(table) for table in tables])) 86 )] 87 else: 88 # Older versions of Postgres can't do TRUNCATE in a single call, so 89 # they must use a simple delete. 90 sql = ['%s %s %s;' % \ 91 (style.SQL_KEYWORD('DELETE'), 92 style.SQL_KEYWORD('FROM'), 93 style.SQL_FIELD(quote_name(table)) 94 ) for table in tables] 95 96 # 'ALTER SEQUENCE sequence_name RESTART WITH 1;'... style SQL statements 97 # to reset sequence indices 98 for sequence_info in sequences: 99 table_name = sequence_info['table'] 100 column_name = sequence_info['column'] 101 if column_name and len(column_name)>0: 102 # sequence name in this case will be <table>_<column>_seq 103 sql.append("%s %s %s %s %s %s;" % \ 104 (style.SQL_KEYWORD('ALTER'), 105 style.SQL_KEYWORD('SEQUENCE'), 106 style.SQL_FIELD(quote_name('%s_%s_seq' % (table_name, column_name))), 107 style.SQL_KEYWORD('RESTART'), 108 style.SQL_KEYWORD('WITH'), 109 style.SQL_FIELD('1') 110 ) 111 ) 112 else: 113 # sequence name in this case will be <table>_id_seq 114 sql.append("%s %s %s %s %s %s;" % \ 115 (style.SQL_KEYWORD('ALTER'), 116 style.SQL_KEYWORD('SEQUENCE'), 117 style.SQL_FIELD(quote_name('%s_id_seq' % table_name)), 118 style.SQL_KEYWORD('RESTART'), 119 style.SQL_KEYWORD('WITH'), 120 style.SQL_FIELD('1') 121 ) 122 ) 123 return sql 124 else: 125 return [] 75 126 76 127 class DatabaseWrapper(BaseDatabaseWrapper): … … 134 185 def get_start_transaction_sql(): 135 186 return "BEGIN;" 136 137 def get_sql_flush(style, tables, sequences):138 """Return a list of SQL statements required to remove all data from139 all tables in the database (without actually removing the tables140 themselves) and put the database in an empty 'initial' state141 142 """143 if tables:144 if postgres_version[0] >= 8 and postgres_version[1] >= 1:145 # Postgres 8.1+ can do 'TRUNCATE x, y, z...;'. In fact, it *has to*146 # in order to be able to truncate tables referenced by a foreign147 # key in any other table. The result is a single SQL TRUNCATE148 # statement.149 sql = ['%s %s;' % \150 (style.SQL_KEYWORD('TRUNCATE'),151 style.SQL_FIELD(', '.join([quote_name(table) for table in tables]))152 )]153 else:154 # Older versions of Postgres can't do TRUNCATE in a single call, so155 # they must use a simple delete.156 sql = ['%s %s %s;' % \157 (style.SQL_KEYWORD('DELETE'),158 style.SQL_KEYWORD('FROM'),159 style.SQL_FIELD(quote_name(table))160 ) for table in tables]161 162 # 'ALTER SEQUENCE sequence_name RESTART WITH 1;'... style SQL statements163 # to reset sequence indices164 for sequence_info in sequences:165 table_name = sequence_info['table']166 column_name = sequence_info['column']167 if column_name and len(column_name)>0:168 # sequence name in this case will be <table>_<column>_seq169 sql.append("%s %s %s %s %s %s;" % \170 (style.SQL_KEYWORD('ALTER'),171 style.SQL_KEYWORD('SEQUENCE'),172 style.SQL_FIELD(quote_name('%s_%s_seq' % (table_name, column_name))),173 style.SQL_KEYWORD('RESTART'),174 style.SQL_KEYWORD('WITH'),175 style.SQL_FIELD('1')176 )177 )178 else:179 # sequence name in this case will be <table>_id_seq180 sql.append("%s %s %s %s %s %s;" % \181 (style.SQL_KEYWORD('ALTER'),182 style.SQL_KEYWORD('SEQUENCE'),183 style.SQL_FIELD(quote_name('%s_id_seq' % table_name)),184 style.SQL_KEYWORD('RESTART'),185 style.SQL_KEYWORD('WITH'),186 style.SQL_FIELD('1')187 )188 )189 return sql190 else:191 return []192 187 193 188 def get_sql_sequence_reset(style, model_list): django/trunk/django/db/backends/postgresql_psycopg2/base.py
r5962 r5963 35 35 cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (table_name, pk_name)) 36 36 return cursor.fetchone()[0] 37 38 def sql_flush(self, style, tables, sequences): 39 if tables: 40 if postgres_version[0] >= 8 and postgres_version[1] >= 1: 41 # Postgres 8.1+ can do 'TRUNCATE x, y, z...;'. In fact, it *has to* 42 # in order to be able to truncate tables referenced by a foreign 43 # key in any other table. The result is a single SQL TRUNCATE 44 # statement. 45 sql = ['%s %s;' % \ 46 (style.SQL_KEYWORD('TRUNCATE'), 47 style.SQL_FIELD(', '.join([quote_name(table) for table in tables])) 48 )] 49 else: 50 # Older versions of Postgres can't do TRUNCATE in a single call, so 51 # they must use a simple delete. 52 sql = ['%s %s %s;' % \ 53 (style.SQL_KEYWORD('DELETE'), 54 style.SQL_KEYWORD('FROM'), 55 style.SQL_FIELD(quote_name(table)) 56 ) for table in tables] 57 58 # 'ALTER SEQUENCE sequence_name RESTART WITH 1;'... style SQL statements 59 # to reset sequence indices 60 for sequence_info in sequences: 61 table_name = sequence_info['table'] 62 column_name = sequence_info['column'] 63 if column_name and len(column_name)>0: 64 # sequence name in this case will be <table>_<column>_seq 65 sql.append("%s %s %s %s %s %s;" % \ 66 (style.SQL_KEYWORD('ALTER'), 67 style.SQL_KEYWORD('SEQUENCE'), 68 style.SQL_FIELD(quote_name('%s_%s_seq' % (table_name, column_name))), 69 style.SQL_KEYWORD('RESTART'), 70 style.SQL_KEYWORD('WITH'), 71 style.SQL_FIELD('1') 72 ) 73 ) 74 else: 75 # sequence name in this case will be <table>_id_seq 76 sql.append("%s %s %s %s %s %s;" % \ 77 (style.SQL_KEYWORD('ALTER'), 78 style.SQL_KEYWORD('SEQUENCE'), 79 style.SQL_FIELD(quote_name('%s_id_seq' % table_name)), 80 style.SQL_KEYWORD('RESTART'), 81 style.SQL_KEYWORD('WITH'), 82 style.SQL_FIELD('1') 83 ) 84 ) 85 return sql 86 else: 87 return [] 37 88 38 89 class DatabaseWrapper(BaseDatabaseWrapper): … … 89 140 return "BEGIN;" 90 141 91 def get_sql_flush(style, tables, sequences):92 """Return a list of SQL statements required to remove all data from93 all tables in the database (without actually removing the tables94 themselves) and put the database in an empty 'initial' state95 """96 if tables:97 if postgres_version[0] >= 8 and postgres_version[1] >= 1:98 # Postgres 8.1+ can do 'TRUNCATE x, y, z...;'. In fact, it *has to* in order to be able to99 # truncate tables referenced by a foreign key in any other table. The result is a100 # single SQL TRUNCATE statement101 sql = ['%s %s;' % \102 (style.SQL_KEYWORD('TRUNCATE'),103 style.SQL_FIELD(', '.join([quote_name(table) for table in tables]))104 )]105 else:106 sql = ['%s %s %s;' % \107 (style.SQL_KEYWORD('DELETE'),108 style.SQL_KEYWORD('FROM'),109 style.SQL_FIELD(quote_name(table))110 ) for table in tables]111 112 # 'ALTER SEQUENCE sequence_name RESTART WITH 1;'... style SQL statements113 # to reset sequence indices114 for sequence in sequences:115 table_name = sequence['table']116 column_name = sequence['column']117 if column_name and len(column_name) > 0:118 # sequence name in this case will be <table>_<column>_seq119 sql.append("%s %s %s %s %s %s;" % \120 (style.SQL_KEYWORD('ALTER'),121 style.SQL_KEYWORD('SEQUENCE'),122 style.SQL_FIELD(quote_name('%s_%s_seq' % (table_name, column_name))),123 style.SQL_KEYWORD('RESTART'),124 style.SQL_KEYWORD('WITH'),125 style.SQL_FIELD('1')126 )127 )128 else:129 # sequence name in this case will be <table>_id_seq130 sql.append("%s %s %s %s %s %s;" % \131 (style.SQL_KEYWORD('ALTER'),132 style.SQL_KEYWORD('SEQUENCE'),133 style.SQL_FIELD(quote_name('%s_id_seq' % table_name)),134 style.SQL_KEYWORD('RESTART'),135 style.SQL_KEYWORD('WITH'),136 style.SQL_FIELD('1')137 )138 )139 return sql140 else:141 return []142 143 142 def get_sql_sequence_reset(style, model_list): 144 143 "Returns a list of the SQL statements to reset sequences for the given models." django/trunk/django/db/backends/sqlite3/base.py
r5962 r5963 51 51 def pk_default_value(self): 52 52 return 'NULL' 53 54 def sql_flush(self, style, tables, sequences): 55 # NB: The generated SQL below is specific to SQLite 56 # Note: The DELETE FROM... SQL generated below works for SQLite databases 57 # because constraints don't exist 58 sql = ['%s %s %s;' % \ 59 (style.SQL_KEYWORD('DELETE'), 60 style.SQL_KEYWORD('FROM'), 61 style.SQL_FIELD(quote_name(table)) 62 ) for table in tables] 63 # Note: No requirement for reset of auto-incremented indices (cf. other 64 # sql_flush() implementations). Just return SQL at this point 65 return sql 53 66 54 67 class DatabaseWrapper(BaseDatabaseWrapper): … … 122 135 return "BEGIN;" 123 136 124 def get_sql_flush(style, tables, sequences):125 """126 Return a list of SQL statements required to remove all data from127 all tables in the database (without actually removing the tables128 themselves) and put the database in an empty 'initial' state129 """130 # NB: The generated SQL below is specific to SQLite131 # Note: The DELETE FROM... SQL generated below works for SQLite databases132 # because constraints don't exist133 sql = ['%s %s %s;' % \134 (style.SQL_KEYWORD('DELETE'),135 style.SQL_KEYWORD('FROM'),136 style.SQL_FIELD(quote_name(table))137 ) for table in tables]138 # Note: No requirement for reset of auto-incremented indices (cf. other139 # get_sql_flush() implementations). Just return SQL at this point140 return sql141 142 137 def get_sql_sequence_reset(style, model_list): 143 138 "Returns a list of the SQL statements to reset sequences for the given models."
