Hello,

I have an idea on how DB migration can work in Django. It's very
simple, it will allow the programmer to do anything he wants in the
migration, and he will have to learn almost nothing in order to
migrate the DB. The drawback is that it's not very efficient, which
means that it will probably work only for small to medium databases -
but I think that most Django sites are like this. If you have a bigger
site, you will have to use SQL.

My idea is this: create a tool which will create a script which will
migrate the DB. The user will then edit the script as he likes, and
run it to create a new DB with the migrated data. If the result is not
what he wanted, he will edit the script again and run it again.

For example, say that I have a directory with the old site (and old
models) and a directory with the new site. The only difference between
them is that I renamed the field store.Book.name to
store.Book.book_name. I will run "django-admin.py migrate oldsitedir
newsitedir". This will create a file which will look like this:

=============================
#!/usr/bin/env python

from django.db import ...

class old_settings:
    DATABASE_ENGINE = 'mysql'
    DATABASE_NAME = 'store_3'
    ...

class new_settings:
    DATABASE_ENGINE = 'mysql'
    DATABASE_NAME = 'store_4'
    ...

old_connection = make_connection_from_settings(old_settings)
new_connection = make_connection_from_settings(new_settings)

# Old models

class old:
    class auth:
        class User(models.Model):
            _connection = old_connection  # or whatever will be needed

            username = models.CharField(unique=True, maxlength=30)
            ...

    class store:
        class Book(models.Model):
            _connection = old_connection

            name = models.CharField()
    ...

# New models

class new:
    ...
    class store:
        class Book(models.Model):
            _connection = old_connection

            book_name = models.CharField()
    ...

# Migration function

def migrate():
    drop_tables_in_new_db_and_sync_it()

    # Migrate auth.User
    for o in old.auth.User.objects.all():
        n = new.auth.User()
        n.id = o.id
        n.username = o.username
        ...
        n.save()

    # Migrate store.Book
    for o in old.store.Book.objects.all():
        n = new.store.Book()
        n.id = o.id
        # TODO:
        # Removed fields: name
        # Added fields: book_name
        n.save()
    ...

if __name__ == '__main__':
    migrate()

=============================

Then, to migrate the DB, I will edit the file, search for "TODO"
comments, and replace the comment in the file with "n.book_name =
o.name". I will create a new empty DB with the name "store_4".

Then I will run "./migrate.py", and the database will be migrated. I
will run the new version of my site, and if everything works I can
delete the DB "store_3".

Another thing: the migrate command will work also with only one given
site directory. In that case the old db definition and the new db
definition will be identical, and the user will be able to edit them
manually in the generated file. This feature will also help in
migrating between different types of databases - in that case, the
user will only change the settings on the top of migrate.py.

To summarize: the programmer won't have to learn commands which add
new fields, delete fields, rename fields, modify fields, etc. The
programmer will just have to slightly modify a generated file, to
create the new version of the DB as he likes.

I hope I managed to explain my idea. What do you think about it?

Have a good day,
Noam


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to