Reputation: 1152
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?
Upvotes: 1
Views: 400
Reputation: 43922
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
.
Upvotes: 1