Skip to main content

Easily monitor database usage - and clean it up (based on your django models)

Project description

django-dbcleanup

Easily monitor database usage - and clean it up (based on your django models)

This pluggable app provides:

  • visibility over database disk space usage for your models
  • command to remove unused tables and recover disk space
  • remove historical data from models that have any DateTimeField (configurable in the project settings.py)

Usage

model and admin view

dbcleanup.Table is an unmanaged model mapped to information tables in MySQL and PostgreSQL and added to django admin

image

command

dbcleanup is the management command that can be used (or scheduled) to remove unused

$ ./manage.py dbcleanup -h
usage: manage.py dbcleanup [-h] [-f] [-i] [-j {tables,history,analyze,migrations}] [--no-fk] [--version] [-v {0,1,2,3}] [--settings SETTINGS] [--pythonpath PYTHONPATH] [--traceback] [--no-color]
                           [--force-color] [--skip-checks]

Remove database tables that do not map to any models, such as when a django app is removed/disabled.

optional arguments:
  -h, --help            show this help message and exit
  -f, --force           Delete the items (instead of just listing them)
  -i, --interactive     Ask which items to delete, interactively
  -j {tables,history,analyze,migrations}, --just {tables,history,analyze,migrations}
                        Perform only a subset of actions
  --no-fk               Disable FOREIGNKEY_CHECK when DROPping tables - CAREFUL! use only if you are sure the constraints are not from a table in use (ie: circular dependencies between drop candidates)

Need to use --force or --interactive to actually perform changes, otherwise it'll be a dry run.
Covered actions are:

  • tables: remove database tables that do not map to any model (ie: when a app is removed from the project, there is no migration to delete the tables) - use settings.DBCLEANUP_REQUIRED_TABLES to whitelist tables that would otherwise be removed
  • history: remove old records for the models defined in settings.DBCLEANUP_HISTORY_MODELS (more below)
  • analyze: only for MySQL - force analyze on all the tables to update the row count and size estimates
  • migrations: remove migrations (from django_migrations table) that not in the project migration path (ie: after migration squashing and reset)

historical data

settings.DBCLEANUP_HISTORY_MODELS is a list of tuples where each tuple is (MODEL_NAME, DAYS_TO_KEEP, DATE_TIME_FIELD_NAME).

# someproject/settings.py
DBCLEANUP_HISTORY_MODELS = [
    ('notifications.notification', 365, 'time'),
    ...
]

With this setup, when ./manage.py dbcleanup -j history is executed, all notifications.Notification entries with time older than 365 days would be deleted.
History cleanup is skipped if it would cascade into other models (unless it's a multi-table model and it would cascade to the parent model).

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-dbcleanup-0.1.4.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

django_dbcleanup-0.1.4-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file django-dbcleanup-0.1.4.tar.gz.

File metadata

  • Download URL: django-dbcleanup-0.1.4.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for django-dbcleanup-0.1.4.tar.gz
Algorithm Hash digest
SHA256 e5902b90fc2ea60ca54524fd35028bb89423b19bb06dfb855194d2c3e75e981a
MD5 461f8f8896101f7e7a7518c4c726ca97
BLAKE2b-256 dd422392d46f6a8a2c067d17e574b73e43d2c226573098951126aeeb3daee8f6

See more details on using hashes here.

File details

Details for the file django_dbcleanup-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for django_dbcleanup-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a622945568cced66dc663cde0370a1f97243f5b10d618f95f2e054ba31eb311d
MD5 a3d3f5ce9c91975c0856fa34ff5a14a2
BLAKE2b-256 d04d3762d6f0ff6f1cad40fe409cc34030493b3f0f68ecafb19f7473c6ad8595

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