Core & CLI
The openviper.core package contains internal machinery for application
bootstrapping, request-context variables, and the flexible app resolver.
The viperctl CLI command provides management operations (migrations,
shell, worker, etc.) for projects with non-standard directory layouts.
Overview
openviper.core is not typically used directly in application code. It
exposes the following building blocks:
AppResolver — discovers app modules from
INSTALLED_APPSregardless of whether they live as sub-packages or flat modules onsys.path.Context variables —
current_userandignore_permissions_ctxContextVars that flow through the async task tree for the duration of a single request.FlexibleAdapter — bootstraps
OPENVIPER_SETTINGS_MODULEand runs management commands; used by theviperctlCLI entry point.
The viperctl sub-command is exposed through the openviper CLI and
supports the following management commands:
Command |
Description |
|---|---|
|
Generate new migration files for changed models. |
|
Apply pending migrations to the database. |
|
Interactively create an admin superuser. |
|
Change a user’s password interactively. |
|
Open a Python REPL with models and settings pre-loaded. |
|
Start the background task worker in-process. |
|
Collect static assets into |
|
Run the project test suite via pytest. |
|
Create a compressed database backup archive. |
|
Restore a database from a |
Key Classes & Functions
- class openviper.core.app_resolver.AppResolver
Resolves physical filesystem paths for each entry in
INSTALLED_APPS. Handles both package-style apps (myproject.blog) and flat modules.
- openviper.core.context.current_user
contextvars.ContextVarholding the authenticated user for the current async task. Set byAuthenticationMiddleware.
- openviper.core.context.ignore_permissions_ctx
contextvars.ContextVar(bool) used by the ORM permission layer. WhenTrue, all model-level permission checks are bypassed for the current async task.
Example Usage
Running Management Commands
# Generate and apply migrations
openviper viperctl makemigrations .
openviper viperctl migrate .
# Custom settings module
openviper viperctl --settings myproject.settings makemigrations myapp
# Interactive shell
openviper viperctl shell
# Start a background task worker
openviper viperctl runworker .
# Collect static files
openviper viperctl collectstatic .
Accessing the Current User in Async Code
from openviper.core.context import current_user
async def my_service_function() -> None:
user = current_user.get()
if user and user.is_authenticated:
print(f"Called by: {user.username}")
Bypassing Permissions for Internal Operations
from openviper.db.executor import bypass_permissions
from myapp.models import SensitiveRecord
async def migrate_records() -> None:
with bypass_permissions():
records = await SensitiveRecord.objects.all()
for record in records:
await record.save()
Database Backup & Restore
The backup-db and restore-db commands are built-in management commands
available in every project via viperctl.py.
System Requirements
Each database engine requires the corresponding native CLI tools to be
installed and available on PATH:
Engine |
Required tools |
Install hint |
|---|---|---|
PostgreSQL |
|
|
MariaDB / MySQL |
|
|
Oracle |
|
Included with Oracle Instant Client |
SQL Server |
|
|
SQLite |
(none) |
SQLite is backed up by file copy — no extra tools needed |
Usage
Backup a database:
python viperctl.py backup-db
Back up to a custom directory:
python viperctl.py backup-db --path /var/backups/myapp
Custom filename (without extension):
python viperctl.py backup-db --name myapp_prod
Specify a database URL explicitly:
python viperctl.py backup-db --db postgresql://user:pass@host/mydb
Skip compression (produce a plain .sql file):
python viperctl.py backup-db --no-compress
Restore from an archive:
python viperctl.py restore-db postgres_20260404-121212.tar.gz
Force overwrite of the existing database:
python viperctl.py restore-db postgres_20260404-121212.tar.gz --force
Restore to an explicit database URL:
python viperctl.py restore-db backup.tar.gz --db postgresql://user:pass@host/mydb
Restore from a plain SQL file:
python viperctl.py restore-db backup.sql --db sqlite:///db.sqlite3
Supported Databases
Engine |
Backup Method |
Restore Method |
|---|---|---|
SQLite |
File copy (async) |
File copy |
PostgreSQL |
|
|
MariaDB / MySQL |
|
|
Oracle |
|
|
SQL Server |
|
|
Archive Format
Each compressed backup produces a .tar.gz file containing:
postgres_20260404-121212.tar.gz
└── backup.sql # database dump or raw file copy
A sidecar metadata file is written alongside the archive:
postgres_20260404-121212.tar.gz.meta.json
Field |
Description |
|---|---|
|
Logical name derived from the database URL |
|
Engine identifier ( |
|
UTC ISO-8601 timestamp when the backup was taken |
|
Archive filename |
|
Version of OpenViper used to create the backup |
|
SHA-256 hex digest of the archive |
Backup files are named automatically using UTC datetime:
{database_name}_{YYYYMMDD-HHMMSS}.tar.gz
Example filenames:
postgres_20260404-121212.tar.gz
sqlite_20260404-121212.tar.gz
Workflow Examples
Backup before migrations:
python viperctl.py backup-db --path ./backups
python viperctl.py migrate
# Roll back if needed:
python viperctl.py restore-db ./backups/sqlite_20260404-121212.tar.gz --force
Production PostgreSQL backup:
python viperctl.py backup-db \\
--db postgresql://appuser:password@db.prod.internal/appdb \\
--path /backups/daily \\
--name appdb_daily
Restore workflow:
python viperctl.py restore-db \\
/backups/daily/appdb_daily_20260404-020000.tar.gz \\
--db postgresql://appuser:password@db.prod.internal/appdb \\
--force
Scheduled nightly backup (cron):
# crontab -e
0 2 * * * cd /srv/myapp && /srv/venv/bin/python viperctl.py backup-db \\
--path /backups/nightly \\
--name myapp_nightly \\
>> /var/log/openviper_backup.log 2>&1
Error Handling
Both commands exit with code 1 on failure and print an error message
to stderr.
Error |
Cause / fix |
|---|---|
|
No |
|
URL prefix not in the engine registry. Check the URL format. |
|
The path supplied to |
|
Archive is corrupt or was not created by |
|
PostgreSQL client tools not on |
|
MariaDB/MySQL client tools not on |
|
|
Configuration Reference
backup-db arguments:
Argument |
Type |
Default |
Description |
|---|---|---|---|
|
string |
|
Directory to store backup archives |
|
string |
auto-generated |
Custom filename (without extension); defaults to |
|
string |
|
Database URL (overrides settings) |
|
flag |
|
Compress to |
restore-db arguments:
Argument |
Type |
Default |
Description |
|---|---|---|---|
|
string |
(required) |
Path to a |
|
string |
|
Database URL (overrides settings) |
|
flag |
|
Allow overwriting the target database without prompting |