Custom backend¶
Implementing a backend requires you to extend BaseDbdevBackend
and override/implement the following methods:
BaseDbdevBackend.init()
BaseDbdevBackend.destroy()
BaseDbdevBackend.run_database_server_in_foreground()
BaseDbdevBackend.start_database_server()
BaseDbdevBackend.stop_database_server()
BaseDbdevBackend.backup()
BaseDbdevBackend.restore()
BaseDbdevBackend.serverinfo()
You must also:
- Create a template that the
BaseDbdevBackend.guide()
method can use. - Add a DBSETTINGS dict to the module containing your backend (see the other backends).
Register your backend¶
You must register your backend some place that is always executed when Django
starts up. The most natural place is in your development settings.py
. Lets
say you have implemented an Oracle backend, you need to tell django_dbdev to use
your dbdev backend for the django.db.backends.oracle
engine like this:
from django_dbdev import backendregistry
from mypackage.dbdev_backends.oracle import OracleBackend
backendregistry.register('django.db.backends.oracle', OracleBackend)
Note
You can replace the built in backends this way too. The registry is a dict
mapping engine string to backend class, so registering a custom backend for
an engine with a built in backend (like django.db.backends.mysql
) does
not raise any errors.
BaseDbdevBackend docs¶
-
class
django_dbdev.backends.base.
BaseDbdevBackend
(command)¶ Bases:
future.types.newobject.newobject
Abstract base class for dbdev backends.
Parameters: command – A Django management command class. Will always be a subclass of django_dbdev.management.commands._base.BaseDbdevCommand
.-
init
()¶ Create the database and grant the required previleges to the database user.
-
destroy
()¶ Remove all the files for the database.
Typically stops any running database server and deletes the data directory.
-
run_database_server_in_foreground
()¶ Run database server in the foreground.
-
start_database_server
()¶ Start database server in the background.
-
stop_database_server
()¶ Stop database server started with
start_database_server()
.
-
backup
(directory)¶ Create a backup of the database.
Parameters: directory – The backup directory to create the backup in.
-
restore
(directory)¶ Restore a backup created with
backup.()
Parameters: directory – The backup directory to restore.
-
serverinfo
()¶ Print information about the server.
Must at least tell if the server is running or not.
-
guide
()¶ Print useful database specific commands and tips for the user.
Examples should include all the needed login info.
The idea is to avoid having to lookup those commonly needed database-specific management and connection commands that is needed from time to time.
The default expects the backend to create a Django template named
django_dbdev/<backend-class-name-lowercased>.rst
. The template gets the backend class asbackend
context variable, and thedbsettings
context variable contains<backendmodule>.DBSETTINGS
.Use the ReStructuredText format for the text.
-
datadir
¶ Get the path to the temporary data directory for this database backend.
The directory is created if it does not exist.
-
create_datadir_if_not_exists
()¶ Create
datadir()
if it does not exist.
-
remove_datadir
()¶ Remove the
datadir()
.
-
stdout
¶ Shortcut for
self.command.stdout
.Use for normal messages. I.E.:
self.stdout.write('Something useful here!')
-
stderr
¶ Shortcut for
self.command.stderr
.Use for error messages. I.E.:
self.stderr.write('An error of some sort')
-
root_backupdir
¶ Get the path to the backup directory for this database backend.
-
create_timestamped_backupdir
(name=None)¶ Create a timestamped directory within
root_backupdir()
.Returns: The path to the created directory.
-
reinit
()¶ Destroy and re-initialize.
-