Database Dump Script

Description

This tool is used for dump and restore database of Django. And it can also support some simple situations for Model changes, so it can also be used in importing data after the migration of Model.

It includes: dump and restore.

Dump

Command Line:

python db_dump.py [-svdh] [--settings] dump [applist]

If applist is ignored，then it means that all app will be dumped. applist can be one or more app name.

Description of options:

-s Output will displayed in console, default is writing into file

-v Display execution infomation, default is does not display

-d Directory of output, default is datadir in current directory. If the path is not existed, it'll be created automatically.

-h Display help information.

--settings settings model, default is settings.py in current directory.

It can only support Python format for now. It'll create a standard python source file, for example:

dump = {'table': 'tablename', 'records': [[...]], 'fields': [...]}

table' is table name in database, records is all records of the table, it's a list of list, that is each record is a list. fields` is the fields name of the table.

Load(Restore)

Command Line:

python db_dump.py [-svdrh] [--settings] load [applist]

You can refer to the above description for same option. Others is:

-r Does not empty the table as loading the data, default is empty the table first then load the data

Using this tool, you can not only restore the database, but also can deal with the simple changes of database. It can select the suitable field from the backup data file according to the changed Model automatically, and it can also deal with the default value define in Model, such as default parameter and auto_now and auto_now_add parameter for Date-like field. And you can even edit the backup data file manually, and add a default key for specify the default value for some fields, the basic format is:

'default':{'fieldname':('type', 'value')}

default is a dict, the key will be the field name of the table, the value will be a two element tuple, and the first element of this tuple is type field, the second element is its value. Below is a description of type field:

type

value

说明

'value'

real value

using the value field directly

'reference'

referred field name

the value of this filed will use the value of referred field. It'll be used when the field name is changed

'date'

'now'|'yyyy-mm-dd'

It's a date date type, if the value field is 'now', then the value be current time. Otherwise, it'll be a string, it's format is 'yyyy-mm-dd'

'datetime'

'now'|'yyyy-mm-dd hh:mm:ss'

The same as above

The strategy of selection of default value of a field is: first, create a default value dict according the Model, then update it according the default key of backup data file. So you can see if there is a same definition of a field in both Model and backup data file, it'll use the one in backup data file.

According the process of default value, this tool will suport these changes, such as: change of field name, add or remove field name, etc. So you can use this tool to finish some simple update work of database.

But I don't give it too much test, and my situation is in sqlite3. So download and test are welcome, and I hope you can give me some improve advices.

You can also find this file in my personal project SharePlat, which is at