Ticket #6148: generic-db_schema-r8319.diff
File generic-db_schema-r8319.diff, 9.5 KB (added by , 16 years ago) |
---|
-
django/core/management/commands/syncdb.py
61 61 app_name = app.__name__.split('.')[-2] 62 62 model_list = models.get_models(app) 63 63 for model in model_list: 64 # Add model defined schema tables if anny 65 if model._meta.db_schema: 66 tables += connection.introspection.schema_table_names(model._meta.db_schema) 64 67 # Create the model's database table, if it doesn't already exist. 65 68 if verbosity >= 2: 66 69 print "Processing %s.%s model" % (app_name, model._meta.object_name) -
django/core/management/sql.py
84 84 references_to_delete = {} 85 85 app_models = models.get_models(app) 86 86 for model in app_models: 87 # Find aditional tables in model defined schemas 88 if model._meta.db_schema: 89 table_names += connection.introspection.get_schema_table_list(cursor, model._meta.db_schema) 87 90 if cursor and connection.introspection.table_name_converter(model._meta.db_table) in table_names: 88 91 # The table exists, so it needs to be dropped 89 92 opts = model._meta -
django/db/backends/__init__.py
225 225 """ 226 226 raise NotImplementedError() 227 227 228 def prep_db_table(self, db_schema, db_table): 229 """ 230 Prepares and formats the table name if neccesary. 231 Just returns the db_table if not supported 232 """ 233 return db_table 234 228 235 def random_function_sql(self): 229 236 """ 230 237 Returns a SQL expression that returns a random value. … … 381 388 "Returns a list of names of all tables that exist in the database." 382 389 cursor = self.connection.cursor() 383 390 return self.get_table_list(cursor) 391 392 def get_schema_table_list(self, cursor, schema): 393 return [] 394 395 def schema_table_names(self, schema): 396 "Returns a list of names of all tables that exist in the database schema." 397 cursor = self.connection.cursor() 398 return self.get_schema_table_list(cursor, schema) 384 399 385 400 def django_table_names(self, only_existing=False): 386 401 """ -
django/db/backends/creation.py
243 243 tablespace_sql = '' 244 244 else: 245 245 tablespace_sql = '' 246 # Use original db_table in index name if schema is provided 247 if model._meta.db_schema: 248 index_table_name = model._meta._db_table 249 else: 250 index_table_name = model._meta.db_table 251 246 252 output = [style.SQL_KEYWORD('CREATE INDEX') + ' ' + 247 style.SQL_TABLE(qn('%s_%s' % ( model._meta.db_table, f.column))) + ' ' +253 style.SQL_TABLE(qn('%s_%s' % (index_table_name, f.column))) + ' ' + 248 254 style.SQL_KEYWORD('ON') + ' ' + 249 255 style.SQL_TABLE(qn(model._meta.db_table)) + ' ' + 250 256 "(%s)" % style.SQL_FIELD(qn(f.column)) + -
django/db/backends/mysql/base.py
106 106 def quote_name(self, name): 107 107 if name.startswith("`") and name.endswith("`"): 108 108 return name # Quoting once is enough. 109 return "`%s`" % name 109 # add support for tablenames passed that also have their schema in their name 110 return "`%s`" % name.replace('.','`.`') 110 111 112 def prep_db_table(self, db_schema, db_table): 113 return "%s.%s" % (db_schema, db_table) 114 111 115 def random_function_sql(self): 112 116 return 'RAND()' 113 117 -
django/db/backends/mysql/introspection.py
32 32 cursor.execute("SHOW TABLES") 33 33 return [row[0] for row in cursor.fetchall()] 34 34 35 def get_schema_table_list(self, cursor, schema): 36 cursor.execute("SHOW TABLES FROM %s" % self.connection.ops.quote_name(schema)) 37 return [schema + "." + row[0] for row in cursor.fetchall()] 38 35 39 def get_table_description(self, cursor, table_name): 36 40 "Returns a description of the table, with the DB-API cursor.description interface." 37 41 cursor.execute("SELECT * FROM %s LIMIT 1" % self.connection.ops.quote_name(table_name)) -
django/db/models/options.py
21 21 DEFAULT_NAMES = ('verbose_name', 'db_table', 'ordering', 22 22 'unique_together', 'permissions', 'get_latest_by', 23 23 'order_with_respect_to', 'app_label', 'db_tablespace', 24 'abstract' )24 'abstract', 'db_schema') 25 25 26 26 class Options(object): 27 27 def __init__(self, meta, app_label=None): … … 29 29 self.module_name, self.verbose_name = None, None 30 30 self.verbose_name_plural = None 31 31 self.db_table = '' 32 self.db_schema = '' 32 33 self.ordering = [] 33 34 self.unique_together = [] 34 35 self.permissions = [] … … 95 96 self.db_table = "%s_%s" % (self.app_label, self.module_name) 96 97 self.db_table = truncate_name(self.db_table, connection.ops.max_name_length()) 97 98 99 # Patch db_table with the schema if provided and allowed 100 if self.db_schema: 101 # Store original db_table in a save place first 102 self._db_table = self.db_table 103 self.db_table = connection.ops.prep_db_table(self.db_schema, self.db_table) 104 # If no changes were done then backend don't support schemas 105 if self._db_table == self.db_table: 106 self.db_schema = '' 98 107 99 108 def _prepare(self, model): 100 109 if self.order_with_respect_to: -
docs/model-api.txt
1207 1207 that aren't allowed in Python variable names -- notably, the hyphen -- 1208 1208 that's OK. Django quotes column and table names behind the scenes. 1209 1209 1210 ``db_schema`` 1211 ----------------- 1212 1213 **New in Django development version** 1214 1215 The name of the database schema to use for the model. If the backend 1216 doesn't support multiple schemas, this options is ignored. 1217 1218 If this is used Django will prefix any table names with the schema name. 1219 For MySQL Django would use ``db_schema + '.' + db_table``. 1220 1221 1210 1222 ``db_tablespace`` 1211 1223 ----------------- 1212 1224 -
tests/modeltests/schemas/models.py
1 # coding: utf-8 2 3 from django.db import models 4 5 6 class Blog(models.Model): 7 "Model in default schema" 8 name = models.CharField(max_length=50) 9 10 11 class Entry(models.Model): 12 "Model in custom schema that reference the default" 13 blog = models.ForeignKey(Blog) 14 title = models.CharField(max_length=50) 15 16 class Meta: 17 "using custom db_table as well" 18 db_table='schema_blog_entries' 19 db_schema = 'test_django' 20 21 22 class Comment(models.Model): 23 "Model in the default schema that references the custom" 24 entry = models.ForeignKey(Entry) 25 text = models.CharField(max_length=50) 26 27 __test__ = {'API_TESTS': """ 28 # Nothing in there yet 29 >>> Blog.objects.all() 30 [] 31 32 # Create a blog 33 >>> b = Blog(name='Test') 34 >>> b.save() 35 36 # Verify that we got an ID 37 >>> b.id 38 1 39 40 # Create entry 41 >>> e = Entry(blog=b, title='Test entry') 42 >>> e.save() 43 >>> e.id 44 1 45 46 # Create Comments 47 >>> c1 = Comment(entry=e, text='nice entry') 48 >>> c1.save() 49 >>> c2 = Comment(entry=e, text='really like it') 50 >>> c2.save() 51 52 53 >>> from django.conf import settings 54 >>> from django.db import connection, models 55 56 # Test if we support schemas and can find the table if so 57 >>> if e._meta.db_schema: 58 ... tables = connection.introspection.schema_table_names(e._meta.db_schema) 59 ... else: 60 ... tables = connection.introspection.table_names() 61 >>> if connection.introspection.table_name_converter(e._meta.db_table) in tables: 62 ... print "ok" 63 ... else: 64 ... print "schema=" + e._meta.db_schema 65 ok 66 67 # Test that mysql backend doesn't drop the schema 68 >>> if settings.DATABASE_ENGINE == 'mysql': 69 ... if e._meta.db_schema != 'test_django': 70 ... print "shouldn't drop or modify schema" 71 72 #Done 73 """ 74 }