Login

Tag "multi-db"

Snippet List

Multi-DB Reconnecting Persistent Postgres Connection

This is a modification of http://djangosnippets.org/snippets/1707/ that handles the database going down or PG Bouncer killing the connection. This also works in things like Twisted to make sure the connection is alive before doing a real query. Thanks @mike_tk for the original post! EDIT: Updated the wrapper to handle multi-db. Before it was using the first connection it made, now it creates an attribute name for the connection based on the name of the database.

  • database
  • multi-db
  • twisted
  • connection
  • persistent
  • multiple-databases
  • socket
  • web-socket
Read More

Save a model using an arbitrary db connection

This function lets you save an instance of a model to another database based on a connection argument. Useful when doing data migrations across databases. Connection is anything that would work as a django.db.connection I'm not sure if this handles proxy models or model inheritance properly, though.

  • multi-db
  • connection
Read More

Dynamic Models Revisited

Somebody mentioned in #django the other day that they have multiple databases with the same schema... Well *lucky me* so do I!! This is one way to work with this issue. I've also included migrate_table_structure just in case the schema doesn't exist in the new database yet. As per the multiple-db-support branch, write all of your databases into the OTHER_DATABASES in settings.py. You can now copy models from your various models.py files and use them in different databases at the same time. This can also be used to migrate databases from one dialect to the other without having to translate your data into an interim format (e.g. csv, XML). You can just do: qs = MyModel.objects.filter(**filters) NewModel = duplicate_model_and_rels(MyModel, 'new_db') #Assuming that the schema is already in new_db: for mod in qs: new = NewModel() new.__dict__ = mod.__dict__ new.save() I tried this using some hacks with SQLAlchemy, and the above approach is a huge amount quicker! I've used this to copy some stuff from an oracle db, into a sqlite db so i could carry on working later and transferred about 20,000 in 5 mins or so. GOTCHAS ======== This only works against my copy of multi-db as I've made a couple of changes. My copy is substantially the same as my patch attached to ticket 4747 though, so it might work to a point (especially the data migration aspect). If it doesn't work hit me up and I'll send you my patch against trunk. I'm not too crazy about the code in copy_field, it works fine, but looks ugly... If anyone knows of a better way to achieve the same, please let me know.

  • dynamic
  • multi-db
  • copy
Read More
Author: Ben
  • 2
  • 7

3 snippets posted so far.