Changes between Version 8 and Version 9 of MultipleDatabaseSupport
- Timestamp:
- Jul 19, 2006, 1:19:57 PM (18 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
MultipleDatabaseSupport
v8 v9 5 5 === Current status === 6 6 7 ''' HIGHLY UNSTABLE''' The branch is in the middle of major implementation changes, some of which are waiting to merge refactoring from trunk. It will be unstable and unusable for at least a few days.7 '''USEABLE''' 8 8 9 Several aspects of the design outlined below have changed, after discusson on the developer's list. I'm hoping to be able to update the code and this page within the next few days.9 All tests pass. Much work remains in integrating changes into django.core.management and improving docs and tests. 10 10 11 11 Work is ongoing in the multiple-db-support branch, which you can check out thusly: … … 18 18 19 19 ||'''TODO'''||'''status'''|| 20 ||OTHER_DATABASES not DATABASES|| ||21 ||MODELS entry in OTHER_DATABASES not model._meta.db_connection|| ||22 ||Move connection info to manager|| ||20 ||OTHER_DATABASES not DATABASES||DONE|| 21 ||MODELS entry in OTHER_DATABASES not model._meta.db_connection||DONE|| 22 ||Move connection info to manager||DONE|| 23 23 ||Tests for thread/request isolation|||| 24 ||Generate sql for dropping tables in django.db.backends.sql.ansi|| ||24 ||Generate sql for dropping tables in django.db.backends.sql.ansi||DONE|| 25 25 ||Generate sql for resetting sequences in postgres backend SchemaBuilder subclass|||| 26 26 ||When validating models, warn user if a model references another model that is configure to use a different connection|||| … … 33 33 * Avoid any change that impacts backward compatibility. The intent is to add new functionality for those who need it, not to change the most common use case of a single database connection. 34 34 35 * Add a new settings variable, DATABASES. DATABASES is a dict of named database connection parameter sets. In each set, the keys are the same as the standard database connection settings variables. 35 * Add a new settings variable, OTHER_DATABASES. OTHER_DATABASES is a dict of named database connection parameter sets. In each set, the keys are the same as the standard database connection settings variables. Each set of settings may also define a list of models that will use that connection by default. Each item in the MODELS list may an app label (in which case it applies to all models with that app label) or app_label.Model, in which case it applies only to that specific model. 36 36 37 {{{ 37 38 #!python 38 DATABASES = {39 OTHER_DATABASES = { 39 40 'a': { 'DATABASE_ENGINE': 'sqlite3', 40 41 'DATABASE_NAME': '/tmp/dba.db' 42 'MODELS': ['app_label', 'app_label_2.Model'] 41 43 }, 42 44 'b': { 'DATABASE_ENGINE': 'sqlite3', … … 47 49 48 50 49 * Add db_connection to Meta. This allows model classes to specify which connection they should use. 51 * Add db attribute to Managers. This allows access to the connection that a model should use, as determined by settings. It may also be changed on the fly to use a different connection for a particular query or set of queries. When assigning to Manager.db, assign a django.db.ConnectionInfo; the easiest way to get one is from the django.db.connections dict. 52 50 53 {{{ 51 54 #!python … … 55 58 56 59 def __str__(self): 57 return self.name 58 59 class Meta: 60 db_connection = 'a' 60 return self.name' 61 61 62 62 class Widget(models.Model): … … 66 66 def __str__(self): 67 67 return self.code 68 69 class Meta: 70 db_connection = 'b' 68 69 settings.OTHER_DATABASES['a']['MODELS'] = ['Artist'] 70 settings.OTHER_DATABASES['b']['MODELS'] = ['Widget'] 71 72 # get the db connection 73 connection = Artist.objects.db.connection 74 75 # assign another connection 76 Widget.objects.db = connections['other_widget_connection'] 77 71 78 }}} 72 79 73 80 * Allow transaction decorators, etc to take optional connections argument. Without that argument, transactions will apply across all connections already in use. 74 81 75 * Move generation of schema manipulating sql (CREATE TABLE, etc) from django.core.management into backend.creation.82 * Move generation of schema manipulating sql (CREATE TABLE, etc) from django.core.management into django.db.backends.ansi.sql. Schema statements are build with a SchemaBuilder instance; each backend gets an instance in its creation module as creation.builder. 76 83 77 84 * Add methods to Manager to support per-model installation. This will enable each model to be installed using the connection that it specifies. It causes some complications, mainly in determining the correct order in which to install. My current solution is to depend on the developer already having figure that out by defining her models in a sensible order; and, when that fails, punting any unresolved constraints to the end of the syncdb or install process. The manager methods will delegate to each model's backend to do the sql creation.