A Django app used to remove oldest records from specific db tables.
Project description
tables_cleaner is a Django app used to remove oldest records from specific db tables
Quick start
Installation:
pip install django-tables-cleaner or pip install git+https://github.com/morlandi/django-tables-cleaner
Add “tables_cleaner” to your INSTALLED_APPS setting like this:
INSTALLED_APPS = [ ... 'tables_cleaner', ]
Run the management command periodically (i.e. with cron)
python manage.py clean_tables
Usage
usage: manage.py clean_tables [-h] [--database DATABASE] [-d] [--vacuum] [--version] [-v {0,1,2,3}] [--settings SETTINGS] [--pythonpath PYTHONPATH] [--traceback] [--no-color] optional arguments: -h, --help show this help message and exit --database DATABASE Nominates a specific database to load fixtures into. Defaults to the "default" database. -d, --dry-run Don't actually delete records (default: False) --vacuum Run VACUUM after deletion --version show program's version number and exit -v {0,1,2,3}, --verbosity {0,1,2,3} Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output --settings SETTINGS The Python path to a settings module, e.g. "myproject.settings.main". If this isn't provided, the DJANGO_SETTINGS_MODULE environment variable will be used. --pythonpath PYTHONPATH A directory to add to the Python path, e.g. "/home/djangoprojects/myproject". --traceback Raise on CommandError exceptions --no-color Don't colorize the command output.
Settings
- TABLES_CLEANER_TABLES
The list of models to be cleaned;
options:
keep_records: n. of most recent records to be preserved; 0=unused
keep_since_days: always preserve records more recent than this; 0=unused
keep_since_hourse: always preserve records more recent than this; 0=unused
Example:
TABLES_CLEANER_TABLES = [ { 'model': 'backend.log', 'keep_records': 1000, 'keep_since_days': 1, 'keep_since_hours': 0, }, { 'model': 'tasks.updatedevicetask', 'keep_records': 100, 'keep_since_days': 0, 'keep_since_hours': 12, 'get_latest_by': 'created', }, ]
get_latest_by attribute is optional; if not supplied, Model’s Meta get_latest_by is used instead.
Vacuum strategy
“VACUUM” is optionally executed as a final activity (’–vacuum’).
Since version v0.1.0, we opted to use “VACUUM” instead of “VACUUM FULL”, since that seems more appropriate for ordinary database maintenance, for the following reasons:
it’s available for Postgresql and Sqlite (and, hopefully, for other databases too)
database owners are allowed to vacuum all tables in their databases
an exclusive lock is not required
it’s potentially much faster
PostgreSQL documentation explicitly states that The FULL option is not recommended for routine use; see: VACUUM — garbage-collect and optionally analyze a database
Thanks to John Vandenberg for bringing my attention to this.
FileFields and ImageFields
Removing rows in the database when the Model contains one or more FileField or ImageField is not enough, since some garbage is left in the Media folder.
I normally use django-cleanup to cope with this.
License
This code is distributed under the terms of the MIT license.
History
v0.1.0
published on PyPI
use “VACUUM” instead of “VACUUM FULL”
dry run option renamed as “-d” (was “-n”)
v0.0.5
Fix for Django 2.x: call super() from Command.__init__() as required
v0.0.4
Customizable ‘get_latest_by’ attribute
Remove EmptyResultSet import which is not available in older versions of Django
v0.0.3
Setup fix
v0.0.2
First working implementation
v0.0.1
Initial setup
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for django_tables_cleaner-0.1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea784cd799d02ec4e17fbda0a4d7d429648bae5beae37291f3fa881c33d04386 |
|
MD5 | ca8860639423fb1ee9a6040fafa5afa7 |
|
BLAKE2b-256 | 50b84a5d549d18ab93516480337053057d16a75df6ae488a97fb30ac04a85347 |