Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add db_table and db_tablespace handling

  • Loading branch information...
commit 60873ea2ade8bed909b9f2dabb0f8a499226e10d 1 parent 184cf9a
Andrew Godwin authored August 10, 2012
32  django/db/backends/schema.py
... ...
@@ -1,9 +1,3 @@
1  
-import sys
2  
-import time
3  
-
4  
-from django.conf import settings
5  
-from django.db import transaction
6  
-from django.db.utils import load_backend
7 1
 from django.db.backends.creation import BaseDatabaseCreation
8 2
 from django.db.backends.util import truncate_name
9 3
 from django.utils.log import getLogger
@@ -25,12 +19,19 @@ class BaseDatabaseSchemaEditor(object):
25 19
     then the relevant actions, and then commit(). This is necessary to allow
26 20
     things like circular foreign key references - FKs will only be created once
27 21
     commit() is called.
  22
+
  23
+    TODO:
  24
+        - Repointing of FKs
  25
+        - Repointing of M2Ms
  26
+        - Check constraints (PosIntField)
  27
+        - PK changing
28 28
     """
29 29
 
30 30
     # Overrideable SQL templates
31 31
     sql_create_table = "CREATE TABLE %(table)s (%(definition)s)"
32 32
     sql_create_table_unique = "UNIQUE (%(columns)s)"
33 33
     sql_rename_table = "ALTER TABLE %(old_table)s RENAME TO %(new_table)s"
  34
+    sql_retablespace_table = "ALTER TABLE %(table)s SET TABLESPACE %(new_tablespace)s"
34 35
     sql_delete_table = "DROP TABLE %(table)s CASCADE"
35 36
 
36 37
     sql_create_column = "ALTER TABLE %(table)s ADD COLUMN %(column)s %(definition)s"
@@ -261,6 +262,25 @@ def alter_unique_together(self, model, old_unique_together, new_unique_together)
261 262
                 "columns": ", ".join(self.quote_name(column) for column in columns),
262 263
             })
263 264
 
  265
+    def alter_db_table(self, model, old_db_table, new_db_table):
  266
+        """
  267
+        Renames the table a model points to.
  268
+        """
  269
+        self.execute(self.sql_rename_table % {
  270
+            "old_table": self.quote_name(old_db_table),
  271
+            "new_table": self.quote_name(new_db_table),
  272
+        })
  273
+
  274
+    def alter_db_tablespace(self, model, old_db_tablespace, new_db_tablespace):
  275
+        """
  276
+        Moves a model's table between tablespaces
  277
+        """
  278
+        self.execute(self.sql_rename_table % {
  279
+            "table": self.quote_name(model._meta.db_table),
  280
+            "old_tablespace": self.quote_name(old_db_tablespace),
  281
+            "new_tablespace": self.quote_name(new_db_tablespace),
  282
+        })
  283
+
264 284
     def create_field(self, model, field, keep_default=False):
265 285
         """
266 286
         Creates a field on a model.
39  tests/modeltests/schema/tests.py
@@ -342,3 +342,42 @@ def test_unique_together(self):
342 342
         UniqueTest.objects.create(year=2012, slug="foo")
343 343
         self.assertRaises(IntegrityError, UniqueTest.objects.create, year=2012, slug="foo")
344 344
         connection.rollback()
  345
+
  346
+    def test_db_table(self):
  347
+        """
  348
+        Tests renaming of the table
  349
+        """
  350
+        # Create the table
  351
+        editor = connection.schema_editor()
  352
+        editor.start()
  353
+        editor.create_model(Author)
  354
+        editor.commit()
  355
+        # Ensure the table is there to begin with
  356
+        columns = self.column_classes(Author)
  357
+        self.assertEqual(columns['name'][0], "CharField")
  358
+        # Alter the table
  359
+        editor = connection.schema_editor()
  360
+        editor.start()
  361
+        editor.alter_db_table(
  362
+            Author,
  363
+            "schema_author",
  364
+            "schema_otherauthor",
  365
+        )
  366
+        editor.commit()
  367
+        # Ensure the table is there afterwards
  368
+        Author._meta.db_table = "schema_otherauthor"
  369
+        columns = self.column_classes(Author)
  370
+        self.assertEqual(columns['name'][0], "CharField")
  371
+        # Alter the table again
  372
+        editor = connection.schema_editor()
  373
+        editor.start()
  374
+        editor.alter_db_table(
  375
+            Author,
  376
+            "schema_otherauthor",
  377
+            "schema_author",
  378
+        )
  379
+        editor.commit()
  380
+        # Ensure the table is still there
  381
+        Author._meta.db_table = "schema_author"
  382
+        columns = self.column_classes(Author)
  383
+        self.assertEqual(columns['name'][0], "CharField")

0 notes on commit 60873ea

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