Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[multi-db] Added postgresql-specific SchemaBuilder subclass that incl…

…udes method for sequence resets.

git-svn-id: http://code.djangoproject.com/svn/django/branches/multiple-db-support@3442 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 6c65e612b4907be5bc202d326599d8119a9db3ff 1 parent 14f8c83
JP authored July 25, 2006

Showing 1 changed file with 41 additions and 0 deletions. Show diff stats Hide diff stats

  1. 41  django/db/backends/postgresql/sql.py
41  django/db/backends/postgresql/sql.py
... ...
@@ -0,0 +1,41 @@
  1
+from django.db import models
  2
+from django.db.backends.ansi.sql import BoundStatement, SchemaBuilder, \
  3
+    default_style
  4
+
  5
+class PgSchemaBuilder(SchemaBuilder):
  6
+    """SchemaBuilder for postgres. Implements an additional method that
  7
+    outputs SQL statements to reset the sequence(s) for a model.
  8
+    """
  9
+    def get_sequence_reset(self, model, style=None):
  10
+        """Get sequence reset sql for a model.
  11
+        """
  12
+        if style is None:
  13
+            style=default_style
  14
+        for f in model._meta.fields:
  15
+            output = []
  16
+            db = model._default_manager.db
  17
+            connection = db.connection
  18
+            qn = db.backend.quote_name
  19
+            if isinstance(f, models.AutoField):
  20
+                output.append(BoundStatement(
  21
+                        "%s setval('%s', (%s max(%s) %s %s));" % \
  22
+                        (style.SQL_KEYWORD('SELECT'),
  23
+                         style.SQL_FIELD('%s_%s_seq' %
  24
+                                         (model._meta.db_table, f.column)),
  25
+                         style.SQL_KEYWORD('SELECT'),
  26
+                         style.SQL_FIELD(qn(f.column)),
  27
+                         style.SQL_KEYWORD('FROM'),
  28
+                         style.SQL_TABLE(qn(model._meta.db_table))),
  29
+                        connection))
  30
+                break # Only one AutoField is allowed per model, so don't bother continuing.
  31
+        for f in model._meta.many_to_many:
  32
+            output.append(
  33
+                BoundStatement("%s setval('%s', (%s max(%s) %s %s));" % \
  34
+                               (style.SQL_KEYWORD('SELECT'),
  35
+                                style.SQL_FIELD('%s_id_seq' % f.m2m_db_table()),
  36
+                                style.SQL_KEYWORD('SELECT'),
  37
+                                style.SQL_FIELD(qn('id')),
  38
+                                style.SQL_KEYWORD('FROM'),
  39
+                                style.SQL_TABLE(f.m2m_db_table())),
  40
+                               connection))
  41
+        return output

0 notes on commit 6c65e61

Please sign in to comment.
Something went wrong with that request. Please try again.