Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Deployment: Handling data in database

Right now I am using git for Django deployment which seems satisfying to me. My only problem is still how to handle the data in the database properly. Eg. I need often to edit data coming from the prodution site locally and put the data back on the production site (please note I'm talking about data changes and not schema migrations!). I think the workflow should be somehow like the following: Dump data on production site > download data > load data in db > make changes locally > dump data > make diff for data > upload diff & apply changes on production site.

Important to me would be that this also works for changes to existing database rows, deletions etc...

So if I start experimenting with that on my own: 1. Will this work with any of the data dump formats offers? 2. Anybody else working like that, maybe having some (fabric) script solutions for that ready already?

like image 721
Bernhard Vallant Avatar asked Aug 06 '10 11:08

Bernhard Vallant


People also ask

How does Django store data in database?

Creating objectsTo create an object, instantiate it using keyword arguments to the model class, then call save() to save it to the database. This performs an INSERT SQL statement behind the scenes. Django doesn't hit the database until you explicitly call save() . The save() method has no return value.

Does Django handle database?

Django officially supports the following databases: PostgreSQL. MariaDB. MySQL.

How does Django interact with database?

By default, Django works with SQLite, database and allows configuring for other databases as well. Database connectivity requires all the connection details such as database name, user credentials, hostname drive name etc. To connect with MySQL, django. db.

What is the best way to deploy Django?

Django websites can be deployed on any number of hosting providers. The first choice is deciding whether to use a Platform-as-a-service (PaaS) option or a virtual private server (VPS). A PaaS is an easier albeit more expensive option that can handle many deployment issues with minimal configuration.


2 Answers

The tables I want to dump/change/restore are quite small and they are read-only via public interface. The following approach is used:

  1. The data is dumped with ./manage.py dumpdata command on server.
  2. Then result file is commited to VCS on server.
  3. I pull changes and execute ./manage.py loaddata.
  4. After changes are made ./manage dumpdata is executed locally.
  5. The result file is commited to VCS and pushed back to server
  6. ./manage loaddata command is executed on server

This can be automated with Fabric, e.g

1 + 2 + 3 = fab dump_data:cities, 4+5+6 = fab push_data:cities

Diffs are produced internally by VCS. This approach won't work for everything but I found it useful for simple cases.

like image 199
Mikhail Korobov Avatar answered Nov 15 '22 22:11

Mikhail Korobov


1) I understand that you are not talking about schema migration. There is however such a thing as a data migration. I have used South to do make the kind of changes to production data that you described. It might be worth your while to investigate it.

2) IMHO applying a diff is not the best way to go about modifying database dumps. Diff and merge are more applicable to source code/text than they are to database dumps. That said I am curious to know if anyone has successfully done diff/patch/merge on database dumps.

like image 45
Manoj Govindan Avatar answered Nov 15 '22 21:11

Manoj Govindan