views:

133

answers:

1

What is the right way to create custom pgsql types for django application so that each time database is created with syncdb, all custom types are created prior to creating any tables (so that tables can use this type)?

I also use django-evolution, but that's not an appropriate solution — it runs after syncdb. I can imagine doing a workaround like defining models with standard field types and then creating types and altering column types in evolutions, but that's definitely not nice and sort of obscure...

Any idea?

+1  A: 

I don't believe there's a way to do this in Django. As you probably know, there's a post_syncdb signal but no signal for pre_syncdb.

So I think there's only two options: hacking pre_syncdb signal into Django or use an automation tool like Fabric.

Hacking your own pre_syncdb signal, even if it's the right way to do this, probably won't be simple and you have to maintain the patch each new Django release.

On the other hand, not only is an automation tool like Fabric simple but it provides other benefits to your project.

As an example, a portion of my Fabfile looks like:

def createdb():
    "Create a clean database"
    run('createdb --encoding=UNICODE $(db_name) -O $(db_owner) -U $(db_owner)')
    run('python manage.py syncdb --noinput')

Add something like this just before the syncdb:

run('psql -U $(db_owner) $(db_name) < app/sql/custom_types.sql')

and you should be good to go by just typing:

$ fab createdb

or:

$ fab cluster createdb

to run the command on all the machines listed in your environment called cluster.

Van Gale
Yeah, I thought about hacking pre_syncdb — and I can actually submit it as patch. I am also using my own git repo for django so I have no problem maintaining my set of patches until they are in the mainstream...I just thought may be there is any other way, but apparently there is none.. pre_syncdb looks quite naturally, so I'll probably go for it.
Yurii Rashkovskii
Well I'm not a Django dev so my answer can't be authoritative. There might be good reasons why they don't have a pre_syncdb signal and there might be another way to do it. I'd ask the question on django-dev mailing list before doing any coding.
Van Gale
http://code.djangoproject.com/ticket/11398
Yurii Rashkovskii