| 1 | | ===================================== |
|---|
| 2 | | Django Schema Evolution Documentation |
|---|
| 3 | | ===================================== |
|---|
| 4 | | |
|---|
| 5 | | Schema evolution is the function of updating an existing Django generated |
|---|
| 6 | | database schema to a newer/modified version based upon a newer/modified set of |
|---|
| 7 | | Django models. |
|---|
| 8 | | |
|---|
| 9 | | This documentation will take you through several common model changes and show |
|---|
| 10 | | you how Django's schema evolution handles them. Each example provides the pre |
|---|
| 11 | | and post model source code, as well as the SQL output. |
|---|
| 12 | | |
|---|
| 13 | | Adding / Removing Fields |
|---|
| 14 | | ------------------------ |
|---|
| | 1 | = Schema Evolution Documentation = |
|---|
| | 2 | |
|---|
| | 3 | == Introduction == |
|---|
| | 4 | |
|---|
| | 5 | Schema evolution is the function of updating an existing Django generated database schema to a newer/modified version based upon a newer/modified set of Django models. |
|---|
| | 6 | |
|---|
| | 7 | === Limitations === |
|---|
| | 8 | |
|---|
| | 9 | I feel it important to note that is an automated implementation designed to handle schema ''evolution'', not ''revolution''. No tool, other than storing DBA written SQL scripts and auto-applying them via schema versioning or DB fingerprinting (which is a trivial solution - I have a Java implementation if anyone wants it), can handle the full scope of possible database changes. Once you accept this fact, the following becomes self-evident: |
|---|
| | 10 | |
|---|
| | 11 | * There is a trade off between ease of use and the scope of coverable problems. |
|---|
| | 12 | |
|---|
| | 13 | Combine that with: |
|---|
| | 14 | |
|---|
| | 15 | * The vast majority of database changes are minor, evolutionary tweaks. (*) |
|---|
| | 16 | * Very few people are DBAs. |
|---|
| | 17 | |
|---|
| | 18 | And I believe the ideal solution is in easing the life of common Django developer, not in appeasing the DBA's or power-developer's desire for an all-in-one-comprehensive solution. Massive schema changes (w/ data retention) are always going to require someone with database skill, but we can empower the people to do the simple things for themselves. |
|---|
| | 19 | |
|---|
| | 20 | (*) By this I mean adding/removing/renaming tables and adding/removing/renaming/changing-attributes-of columns. |
|---|
| | 21 | |
|---|
| | 22 | == Downloading / Installing == |
|---|
| | 23 | |
|---|
| | 24 | This functionality is not yet in Django/trunk, but in a separate schema-evolution branch. To download this branch, run the following: |
|---|
| | 25 | |
|---|
| | 26 | {{{ |
|---|
| | 27 | svn co http://code.djangoproject.com/svn/django/schema-evolution/ django_se_src |
|---|
| | 28 | ln -s `pwd`/django_se_src/django SITE-PACKAGES-DIR/django |
|---|
| | 29 | }}} |
|---|
| | 30 | |
|---|
| | 31 | Or, if you're currently running Django v0.96, run the following: |
|---|
| | 32 | |
|---|
| | 33 | {{{ |
|---|
| | 34 | cd /<path_to_python_dir>/site-packages/django/ |
|---|
| | 35 | wget http://kered.org/blog/wp-content/uploads/2007/07/django_schema_evolution-v096patch.txt |
|---|
| | 36 | patch -p1 < django_schema_evolution-v096patch.txt |
|---|
| | 37 | }}} |
|---|
| | 38 | |
|---|
| | 39 | The last command will produce the following output: |
|---|
| | 40 | |
|---|
| | 41 | {{{ |
|---|
| | 42 | patching file core/management.py |
|---|
| | 43 | patching file db/backends/mysql/base.py |
|---|
| | 44 | patching file db/backends/mysql/introspection.py |
|---|
| | 45 | patching file db/backends/postgresql/base.py |
|---|
| | 46 | patching file db/backends/postgresql/introspection.py |
|---|
| | 47 | patching file db/backends/sqlite3/base.py |
|---|
| | 48 | patching file db/backends/sqlite3/introspection.py |
|---|
| | 49 | patching file db/models/fields/__init__.py |
|---|
| | 50 | patching file db/models/options.py}}} |
|---|
| | 51 | }}} |
|---|
| | 52 | |
|---|
| | 53 | == How To Use == |
|---|
| | 54 | |
|---|
| | 55 | For the most part, schema evolution is designed to be automagic via introspection. Make changes to your models, run syncdb, and you're done. But like all schema changes, it's wise to preview what is going to be run. To do this, run the following: |
|---|
| | 56 | |
|---|
| | 57 | {{{ |
|---|
| | 58 | ./manage sqlevolve app_name |
|---|
| | 59 | }}} |
|---|
| | 60 | |
|---|
| | 61 | This will output to the command line the SQL to be run to bring your database schema up to date with your model structure. |
|---|
| | 62 | |
|---|
| | 63 | However not everything can be handled through introspection. A small amount of metadata is used in the cases of model or field renames, so that the introspection code can match up the old field to the new field. (therefore preserving your data) |
|---|
| | 64 | |
|---|
| | 65 | For renaming a column, use an "aka" attribute: |
|---|
| | 66 | |
|---|
| | 67 | {{{ |
|---|
| | 68 | # this field used to be called pub_date |
|---|
| | 69 | publish_date = models.DateTimeField('date published', aka='pub_date') |
|---|
| | 70 | }}} |
|---|
| | 71 | |
|---|
| | 72 | If you have renamed this twice and still wish to support migration from both older schemas, "aka"s can be tuples: |
|---|
| | 73 | |
|---|
| | 74 | {{{ |
|---|
| | 75 | # this field used to be called pub_date |
|---|
| | 76 | publish_date = models.DateTimeField('date published', aka=('pub_date','other_old_field_name')) |
|---|
| | 77 | }}} |
|---|
| | 78 | |
|---|
| | 79 | For renaming a model, add an "aka" field to the Meta section: |
|---|
| | 80 | |
|---|
| | 81 | {{{ |
|---|
| | 82 | # the original name for this model was 'Choice' |
|---|
| | 83 | class Option(models.Model): |
|---|
| | 84 | [...] |
|---|
| | 85 | class Meta: |
|---|
| | 86 | aka = 'Choice' |
|---|
| | 87 | }}} |
|---|
| | 88 | |
|---|
| | 89 | For further examples... |
|---|
| | 90 | |
|---|
| | 91 | == Usage Examples == |
|---|
| | 92 | |
|---|
| | 93 | The following documentation will take you through several common model changes and show you how Django's schema evolution handles them. Each example provides the pre and post model source code, as well as the SQL output. |
|---|
| | 94 | |
|---|
| | 95 | === Adding / Removing Fields === |
|---|