Skip to main content

Django integration with litestream, a a standalone streaming replication tool for SQLite.

Project description

django-litestream

PyPI - Version PyPI - Python Version


[!IMPORTANT] This package currently contains minimal features and is a work-in-progress

This package installs and integrates litestream, the SQLite replication tool, as a Django command.

Table of Contents

Installation

pip install django-litestream

Add django_litestream to your Django INSTALLED_APPS.

Usage

The package integrates all the commands and options from the litestream command-line tool with minimal differences.

Configuration

These are the available configurations for django-litestream:

# settings.py
LITESTREAM = {
    "config_file": "/etc/litestream.yml",
    "bin_path": "litestream",
    "dbs": [],
    "extend_dbs": [],
    "logging": {},
    "addr": "",
}

The config_file is where the Litestream configuration file should be generated by the init command. The config_file will be automatically passed to every command you run, so you can freely change the default location without having to pass the -config argument manually each time. For example, you could place it in your project directory by setting config_file = BASE_DIR / 'litestream.yml'.

The bin_path is the path to the Litestream binary. If you want to use a custom installation, specify it here.

The dbs, logging, and addr configurations are the same as those in the Litestream configuration file. You can read more about them here. This allows you to keep your replica configuration in your Django settings.

The extend_dbs is a list of dictionaries with the same format as the dbs configuration, and, as its name suggests, it will extend the dbs configuration when the final configuration is generated.

Commands

You can run python manage.py litestream to see all available commands.

[!Note] Wherever it says dj, assume it is an alias for python manage.py.

litestream init

dj litestream init

This command will write the Litestream configuration to the indicated config_file based on your settings. If you did not specify any values in the dbs key, it will automatically parse your Django DATABASES configuration and write one s3 replica for each SQLite database it finds.

For example, if you have the following DATABASES configuration:

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": BASE_DIR / "db.sqlite3",
    },
    "other": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": BASE_DIR / "other.sqlite3",
    },
}

And your BASE_DIR is /home/tobi/myproject, the generated configuration after running init will look like this:

dbs:
- path: /home/tobi/myproject/db.sqlite3
  replicas:
  - type: s3
    bucket: $LITESTREAM_REPLICA_BUCKET
    path: db.sqlite3
    access-key-id: $LITESTREAM_ACCESS_KEY_ID
    secret-access-key: $LITESTREAM_SECRET_ACCESS_KEY
- path: /home/tobi/myproject/other.sqlite3
  replicas:
  - type: s3
    bucket: $LITESTREAM_REPLICA_BUCKET
    path: other.sqlite3
    access-key-id: $LITESTREAM_ACCESS_KEY_ID
    secret-access-key: $LITESTREAM_SECRET_ACCESS_KEY

You can tweak these settings according to your preferences. Check the databases settings reference for more information.

If you have any entries in the dbs configuration, the init command won’t automatically parse the DATABASES configuration. To extend the configuration generated by the init command, you should use the extend_dbs configuration, for example:

# settings.py
import os

LITESTREAM = {
    "config_file": BASE_DIR / "litestream.yml",
    "extend_dbs": [
        {
            "path": BASE_DIR / "cache.sqlite3",
            "replicas": [
                {
                    "type": "s3",
                    "bucket": os.getenv("LITESTREAM_REPLICA_BUCKET"),
                    "path": "cache.sqlite3",
                    "access-key-id": os.getenv("LITESTREAM_ACCESS_KEY_ID"),
                    "secret-access-key": os.getenv("LITESTREAM_SECRET_ACCESS_KEY"),
                }
            ]
        }
    ]
}

litestream databases

This works exactly like the equivalent litestream command and lists all the databases.

Examples:

dj litestream databases

[!IMPORTANT] For the rest of the commands, wherever you are asked to specify the database path db_path, you can use the Django database alias instead, for example, default instead of /home/tobi/myproject/db.sqlite3.

litestream generations

This works exactly like the equivalent litestream command. Examples:

dj litestream generations default
dj litestream generations -replica s3 default

litestream replicate

This works exactly like the equivalent litestream command, except it does not support the ability to replicate a single file. Running litestream replicate db_path replica_url won't work. You can only run:

dj litestream replicate 
dj litestream replicate -exec "gunicorn myproject.wsgi:application"

litestream restore

This works exactly like the equivalent litestream command.

Examples:

dj litestream restore default
dj litestream restore -if-replica-exists default

litestream snapshots

This works exactly like the equivalent litestream command.

Examples:

dj litestream snapshots default
dj litestream snapshots -replica s3 default

litestream wal

This works exactly like the equivalent litestream command.

Examples:

dj litestream wal default
dj litestream wal -replica s3 default

litestream version

Print the version of the Litestream binary.

dj litestream version

License

django-litestream is distributed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_litestream-0.1.1.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

django_litestream-0.1.1-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file django_litestream-0.1.1.tar.gz.

File metadata

  • Download URL: django_litestream-0.1.1.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.27.0

File hashes

Hashes for django_litestream-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7e4356afd118ace2083646bdc7d2cc7cb2962dc252a55182a05f03e436305c5b
MD5 bede523fa0444a489572be17bd05ab94
BLAKE2b-256 e88b9ad6b43d22e62fe52d9228168dfc135c6146a427d9b89961101d61003650

See more details on using hashes here.

File details

Details for the file django_litestream-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_litestream-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f833839f5ade8087149c0dbb723b58db4be329b178b8795712afa84ed91a53e6
MD5 48f845c331bff2e407bb78706db60e8a
BLAKE2b-256 3cb98871e3529a8d9822bd1e3b84f4cd2d57a92c3e726c5747d06dfc8fbdbfb9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page