Metadata-Version: 2.5
Name: django-parquet-archive
Version: 1.1.0
Summary: Archive old Django table rows to parquet files, configured and browsable from the admin
Project-URL: Repository, https://github.com/onidata/django-parquet-archive
Project-URL: Issues, https://github.com/onidata/django-parquet-archive/issues
Project-URL: Changelog, https://github.com/onidata/django-parquet-archive/releases
License-Expression: BSD-3-Clause
License-File: LICENSE
Keywords: admin,archive,django,parquet,polars,s3
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Web Environment
Classifier: Framework :: Django
Classifier: Framework :: Django :: 5.2
Classifier: Framework :: Django :: 6.0
Classifier: Framework :: Django :: 6.1
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Database
Requires-Python: >=3.10
Requires-Dist: celery>=5.5
Requires-Dist: django>=5.2
Requires-Dist: polars>=1.36
Requires-Dist: python-dateutil>=2.8
Provides-Extra: s3
Requires-Dist: django-storages[s3]>=1.14; extra == 's3'
Description-Content-Type: text/markdown

# django-parquet-archive

[![PyPI](https://img.shields.io/pypi/v/django-parquet-archive?logo=pypi&logoColor=white)](https://pypi.org/project/django-parquet-archive/)
[![Python](https://img.shields.io/pypi/pyversions/django-parquet-archive?logo=python&logoColor=white)](https://pypi.org/project/django-parquet-archive/)
[![Django](https://img.shields.io/pypi/frameworkversions/django/django-parquet-archive?logo=django&logoColor=white&label=django)](https://pypi.org/project/django-parquet-archive/)
[![CI](https://github.com/onidata/django-parquet-archive/actions/workflows/ci.yml/badge.svg)](https://github.com/onidata/django-parquet-archive/actions/workflows/ci.yml)
[![License](https://img.shields.io/pypi/l/django-parquet-archive)](LICENSE)

Archive old rows of Django tables: they are written to parquet files (S3 or
any Django storage) and deleted from the database. Tables are configured in
the admin, and the archived rows can be browsed in the model's own changelist.

The archive runs as a celery task.

## Supported versions

| Django | Python       |
|--------|--------------|
| 5.2    | 3.10 to 3.14 |
| 6.0    | 3.12 to 3.14 |
| 6.1    | 3.12 to 3.14 |

## Installation

```bash
pip install "django-parquet-archive[s3]"
```

The `s3` extra installs `django-storages[s3]`. Without it the library works
with any Django storage, but reads the parquet files by downloading them
through the storage instead of querying the bucket directly.

## Configuration

```python
INSTALLED_APPS = [
    # ...
    'parquet_archive',
]

# A STORAGES alias (default 'default') or the import path of a storage object of the project
PARQUET_ARCHIVE_STORAGE = 'parquet_archive'
STORAGES['parquet_archive'] = {
    'BACKEND': 'storages.backends.s3boto3.S3Boto3Storage',
    'OPTIONS': {'bucket_name': 'my-archive-bucket'},
}

# Optional: files an admin page with a search or other filters reads at most (default 200)
PARQUET_ARCHIVE_MAX_FILES = 200
```

Schedule it with celery beat (the queue and the frequency are up to the project):

```python
CELERY_BEAT_SCHEDULE = {
    'Archive old table rows': {
        'task': 'parquet_archive.archive_tables',
        'schedule': crontab(day_of_month='5', hour='0', minute='0'),
    },
}
```

Then run `python manage.py migrate parquet_archive`.

## Archived tables

Each table is an `ArchivePolicy`, created in the admin under
*Parquet archive → Archive policies*. The form lists the fields of the chosen
model:

- **Date fields**: a row is archived when any of them is older than the
  configured age. Includes fields of direct FKs (e.g. `order__cancelled_at`).
- **Keep for**: age in days, months or years.
- **Ordering**: the order of the rows in the files, and the field is always
  saved. Ordering by a date field of the model itself that is not null (e.g.
  `-requested_at`) makes it the **index field**: each file covers a short
  stretch of it, and the admin reads only the files of each page (see
  *Browsing in the admin*). In a policy created by a migration, include the
  field in the `schema`.
- **Saved columns**: the parquet columns. The primary key is always saved.
  JSON is written as text and `DurationField` as a duration. Fields with
  `from_db_value` (encrypted fields, custom value objects...) are not offered:
  the value read from the database is a Python object, and an encrypted
  value would be archived decrypted.

Only superusers create, change, delete or run policies, because they define
which rows are deleted from the database. Other users with the view
permission see the saved policy. Every change made in the admin is recorded
in the admin's own history, with the user and the changed fields.

The form rejects models that other models reference with `CASCADE`,
`PROTECT` or `RESTRICT`, or that have a `ManyToMany`: deleting the rows after
archiving them would delete data that is not archived, or would fail after
the parquet files were written.

To create the initial policies with a migration in the project:

```python
def create_policies(apps, schema_editor):
    ContentType = apps.get_model('contenttypes', 'ContentType')
    ArchivePolicy = apps.get_model('parquet_archive', 'ArchivePolicy')
    content_type, _ = ContentType.objects.get_or_create(app_label='core', model='apirequest')
    ArchivePolicy.objects.get_or_create(content_type=content_type, defaults={'config': {
        'rows_per_file': 3000,
        'filter': {'fields': ['requested_at'], 'keep_for': 3, 'unit': 'months'},
        'ordering': ['-requested_at'],
        'schema': {'id': 'String', 'requested_at': 'Datetime', 'status_code': 'Int64'},
    }})


class Migration(migrations.Migration):
    dependencies = [('parquet_archive', '0001_initial')]
    operations = [migrations.RunPython(create_policies, migrations.RunPython.noop)]
```

The `schema` types are `String`, `Int64`, `Float64`, `Boolean`, `Date`,
`Datetime`, `Duration`, `Json` and `Decimal(precision,scale)`. Use the type
the admin shows for each field in the form: a policy whose types differ from
those of its fields is not archived (see *Changes in the model*).

Each file has up to `rows_per_file` rows, which are deleted from the database
by their primary keys right after the file is written. If a run fails halfway,
the next one picks up what is left without duplicating the archive, and
nothing that started to match the filter during the run is deleted without
being in a file.

Files are stored as `<lowercase model name>/<UTC timestamp>_<uuid>.parquet`,
and each one is registered in the index of the archive files right after it
is written.

Each file is read with its own column types, and the files of a page are
joined into a type that fits all of them (for example, the decimal with more
digits, or text), so the files written before a field changed type are still
read. A date column of a field that is now a `DateTimeField` becomes the start
of its day, and a datetime column of a field that is now a `DateField` becomes
its day, both in the project time zone. The admin lists the columns of the
newest file (by the timestamp in the file name): columns added to the policy
later show up, and are empty in older files.

## Changes in the model

A policy never changes by itself. When the saved policy no longer matches its
model, that table is not archived, with an error in the log, and the other
tables are still archived:

- a saved column whose field changed type (e.g. a `DecimalField` with more
  digits): writing with the saved type could round the values;
- a saved column whose field is no longer in the model: a renamed field looks
  the same as a removed one, and archiving without the column would lose its
  data;
- a field of the filter or of the ordering no longer in the model: saving the
  policy, the admin asks for another one;
- a field whose type the archive does not support;
- the model removed from the project. In the admin the policy keeps its
  configuration and can be deactivated.

Saving the policy again in the admin, which builds it from the current fields,
fixes it, and the admin history records who changed it. A field added to the
model is only saved once it is selected in the policy.

The archive task fails at the end with an `ArchiveError` that lists the tables
that were not archived. A celery soft time limit is not the failure of one
table: it stops the run right away. The database system check
`parquet_archive.W001` warns about these policies, without stopping anything,
in `migrate` (before the migrations), in the test runner and in
`manage.py check --database default`.

## Index of the archive files

The admin lists the archive from an index in the database (`ArchiveFile`):
the rows of each file and the first and last value of the index field. The
archive task registers every file it writes. Files written before the index
existed, or by another process into the same folders, are registered by the
indexing, which reads each one once.

There is no manual step: the first time the archive of a table is listed in
the admin, if the table was never indexed (e.g. right after the upgrade) or
has files indexed by another field (its ordering changed), the admin queues
the indexing task (`parquet_archive.index_files`, which needs a celery worker)
and warns that the listing may be missing files for a few minutes. It is
queued once; a request pending for more than an hour is queued again. The
indexing can also run at any time:

- in the admin, *Parquet archive → Archive policies*, action *Index the
  archive files of the selected tables* (superusers only);
- or the celery task `parquet_archive.index_files`, which also takes
  `model_names` and can be scheduled to pick up files changed in the storage
  by other processes.

The indexing also removes the files that are no longer in the storage. A file
that cannot be read is skipped with an error in the log. Files without the
index field (written before it was saved) are left out of the listing, with a
warning.

A file removed from the storage outside the library is still in the index
until the next indexing: the admin pages that read it show an error message
instead of the rows, and the detail of its rows goes back to the list with
the message.

## Browsing in the admin

```python
from parquet_archive.admin import ArchiveAdminMixin


class ApiRequestAdmin(ArchiveAdminMixin, admin.ModelAdmin):
    ...
```

The mixin adds the **Data source** filter (Database / Archive). In the
archive, the changelist reads the parquet files and keeps `list_display`,
pagination, column sorting, field filters (FKs included) and search. The
link of each row opens a read-only detail page.

Disabled in the archive: actions, custom filters (`SimpleListFilter`),
facets and `date_hierarchy`. Filters and search fields that do not exist in
the parquet files are ignored with a warning on the page.

Text in a field that is now a number or a boolean (written before the field
changed type) is converted to it, so filters and ordering compare numbers; the
primary key is the exception, and stays text when some file has it as text. A
value that the model field does not convert (e.g. `abc` in a field that is now
an integer) is shown as it is in the file, or left empty in the list when the
field cannot show it (e.g. text in a `BooleanField`, or in a foreign key). The
detail of the row shows these values as they are in the file, and also the
columns of fields removed from the model, as text.

In tables with an index field, each page reads only the files it needs:

- the filter of the index field becomes a dedicated filter, with the months
  and, for the month shown, the days that the archive files cover (the
  shortcuts of the default date filter count from today, and the archive only
  has old rows). In tables with few rows per day a file covers several days,
  and days without rows may show up;
- without other filters, search or ordering by another column, the count
  comes from the index and each page reads only its files, in any period.
  Without an `ordering` in the `ModelAdmin` or in the model, the archive is
  listed by the index field, newest first;
- with a search, other filters or ordering by another column, the page reads
  the files of the period. When they are more than `PARQUET_ARCHIVE_MAX_FILES`,
  it shows the rows of the most recent ones, with a link to continue before
  them;
- the link of each row carries its date (`?at=...`), and the detail page reads
  only the files whose range has it.

Tables without an index field read every file on every page.

## Translations

The admin texts are in English, with a Brazilian Portuguese (`pt_BR`)
translation included.

## Development

```bash
uv sync
uv run pytest
# Minimum supported Django version
uv run --with 'django~=5.2.0' pytest
```

CI runs the suite on every supported combination of Python and Django, and a
Django deprecation caused by the library code fails the suite.

### Releasing

Bump `version` in `pyproject.toml` and publish a GitHub release with the
`v<version>` tag. The `publish` workflow checks that the tag matches the
version, builds and publishes to PyPI with trusted publishing, with no token
stored in the repository, and attaches the distributions to the release.

## License

[BSD-3-Clause](LICENSE).
