Connectors¶
Database connectors handle the database-specific dump and restore logic.
The correct connector is selected automatically from DATABASES[alias]["ENGINE"],
or you can specify one explicitly in database_connectors.
Protocol¶
Auto-detection¶
- django_snapshots.connectors.auto.get_connector_class(engine: str) type[source]¶
Return the connector class for engine (a DATABASES ENGINE string).
Falls back to
DjangoDumpDataConnectorfor unrecognised engines.
- django_snapshots.connectors.auto.get_connector_for_alias(db_alias: str) DatabaseConnector[source]¶
Return a connector instance for db_alias.
Checks
SNAPSHOTS.database_connectorsfor an override first, then auto-detects fromDATABASES[db_alias]["ENGINE"].
Engine mapping¶
ENGINE substring |
Connector |
|---|---|
|
SQLiteConnector |
|
PostgresConnector |
|
MySQLConnector |
(anything else) |
DjangoDumpDataConnector |
Built-in connectors¶
SQLiteConnector¶
Uses Python’s stdlib sqlite3 module. No external binaries required.
PostgresConnector¶
Uses pg_dump and psql. Requires these binaries on PATH.
The database password is passed via the PGPASSWORD environment variable.
MySQLConnector¶
Uses mysqldump and mysql. Requires these binaries on PATH.
Works for both MySQL and MariaDB.
DjangoDumpDataConnector¶
Uses Django’s built-in dumpdata and loaddata management
commands. Works with any database backend and requires no external binaries.
This is the automatic fallback for unrecognised engines.
Note
dumpdata / loaddata use Django’s JSON serialisation format, which
does not preserve all database-native types (e.g. custom PostgreSQL types).
For production PostgreSQL or MySQL, prefer the native connectors.
Writing a custom connector¶
Implement dump()
and restore() on
any class. Register it in settings:
from pathlib import Path
from typing import Any
class OracleConnector:
def dump(self, db_alias: str, dest: Path) -> dict[str, Any]:
# Run expdp, return metadata dict
return {"format": "dmp"}
def restore(self, db_alias: str, src: Path) -> None:
# Run impdp
pass
# In Django settings:
SNAPSHOTS = {
"DATABASE_CONNECTORS": {"default": OracleConnector()},
}