mirror of
				https://github.com/django/django.git
				synced 2025-10-30 17:16:10 +00:00 
			
		
		
		
	Fixes #2333 -- Added test fixtures framework.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4659 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
		| @@ -52,6 +52,8 @@ class UnicodeCursorWrapper(object): | ||||
|         else: | ||||
|             return getattr(self.cursor, attr) | ||||
|  | ||||
| postgres_version = None | ||||
|  | ||||
| class DatabaseWrapper(local): | ||||
|     def __init__(self, **kwargs): | ||||
|         self.connection = None | ||||
| @@ -81,6 +83,10 @@ class DatabaseWrapper(local): | ||||
|         if set_tz: | ||||
|             cursor.execute("SET TIME ZONE %s", [settings.TIME_ZONE]) | ||||
|         cursor = UnicodeCursorWrapper(cursor, settings.DEFAULT_CHARSET) | ||||
|         global postgres_version | ||||
|         if not postgres_version: | ||||
|             cursor.execute("SELECT version()") | ||||
|             postgres_version = [int(val) for val in cursor.dictfetchone()['version'].split()[1].split('.')]         | ||||
|         if settings.DEBUG: | ||||
|             return util.CursorDebugWrapper(cursor, self) | ||||
|         return cursor | ||||
| @@ -151,6 +157,62 @@ def get_drop_foreignkey_sql(): | ||||
| def get_pk_default_value(): | ||||
|     return "DEFAULT" | ||||
|  | ||||
| def get_sql_flush(style, tables, sequences): | ||||
|     """Return a list of SQL statements required to remove all data from | ||||
|     all tables in the database (without actually removing the tables | ||||
|     themselves) and put the database in an empty 'initial' state | ||||
|      | ||||
|     """     | ||||
|     if tables: | ||||
|         if postgres_version[0] >= 8 and postgres_version[1] >= 1: | ||||
|             # Postgres 8.1+ can do 'TRUNCATE x, y, z...;'. In fact, it *has to* in order to be able to | ||||
|             # truncate tables referenced by a foreign key in any other table. The result is a | ||||
|             # single SQL TRUNCATE statement. | ||||
|             sql = ['%s %s;' % \ | ||||
|                 (style.SQL_KEYWORD('TRUNCATE'), | ||||
|                  style.SQL_FIELD(', '.join(quote_name(table) for table in tables)) | ||||
|             )] | ||||
|         else: | ||||
|             # Older versions of Postgres can't do TRUNCATE in a single call, so they must use  | ||||
|             # a simple delete. | ||||
|             sql = ['%s %s %s;' % \ | ||||
|                     (style.SQL_KEYWORD('DELETE'), | ||||
|                      style.SQL_KEYWORD('FROM'), | ||||
|                      style.SQL_FIELD(quote_name(table)) | ||||
|                      ) for table in tables] | ||||
|  | ||||
|         # 'ALTER SEQUENCE sequence_name RESTART WITH 1;'... style SQL statements | ||||
|         # to reset sequence indices | ||||
|         for sequence_info in sequences: | ||||
|             table_name = sequence_info['table'] | ||||
|             column_name = sequence_info['column'] | ||||
|             if column_name and len(column_name)>0: | ||||
|                 # sequence name in this case will be <table>_<column>_seq | ||||
|                 sql.append("%s %s %s %s %s %s;" % \ | ||||
|                     (style.SQL_KEYWORD('ALTER'), | ||||
|                     style.SQL_KEYWORD('SEQUENCE'), | ||||
|                     style.SQL_FIELD('%s_%s_seq' % (table_name, column_name)), | ||||
|                     style.SQL_KEYWORD('RESTART'), | ||||
|                     style.SQL_KEYWORD('WITH'), | ||||
|                     style.SQL_FIELD('1') | ||||
|                     ) | ||||
|                 ) | ||||
|             else: | ||||
|                 # sequence name in this case will be <table>_id_seq | ||||
|                 sql.append("%s %s %s %s %s %s;" % \ | ||||
|                     (style.SQL_KEYWORD('ALTER'), | ||||
|                      style.SQL_KEYWORD('SEQUENCE'), | ||||
|                      style.SQL_FIELD('%s_id_seq' % table_name), | ||||
|                      style.SQL_KEYWORD('RESTART'), | ||||
|                      style.SQL_KEYWORD('WITH'), | ||||
|                      style.SQL_FIELD('1') | ||||
|                      ) | ||||
|                 ) | ||||
|         return sql | ||||
|     else: | ||||
|         return [] | ||||
|  | ||||
|          | ||||
| # Register these custom typecasts, because Django expects dates/times to be | ||||
| # in Python's native (standard-library) datetime/time format, whereas psycopg | ||||
| # use mx.DateTime by default. | ||||
|   | ||||
		Reference in New Issue
	
	Block a user