Metadata-Version: 2.4
Name: django-schema-audit
Version: 1.0.5
Summary: PostgreSQL Schema Audit For Django
Author: Harshal Patil
License: MIT
Classifier: Framework :: Django
Classifier: Framework :: Django :: 4.2
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: OS Independent
Classifier: Topic :: Database
Classifier: Intended Audience :: Developers
Requires-Python: >=3.10
Description-Content-Type: text/markdown
Requires-Dist: Django>=4.2
Requires-Dist: psycopg2-binary>=2.9
Dynamic: author
Dynamic: classifier
Dynamic: description
Dynamic: description-content-type
Dynamic: license
Dynamic: requires-dist
Dynamic: requires-python
Dynamic: summary

# django-schema-audit

A reusable Django app that automatically audits all PostgreSQL DDL schema changes — `CREATE`, `ALTER`, `DROP`, `RENAME` — using native PostgreSQL event triggers. Every structural change to your database is silently recorded with full context: who did it, when, and the exact SQL used.

---

## Requirements

- Python >= 3.10
- Django >= 4.2
- PostgreSQL (required — uses PostgreSQL event triggers)
- psycopg2-binary >= 2.9

---

## Installation — 4 Steps

**Step 1 — Install the package**

```bash
pip install django-schema-audit
```

**Step 2 — Add to INSTALLED_APPS**

```python
# settings.py
INSTALLED_APPS = [
    ...
    'django_schema_audit_app',
]
```

**Step 3 — Run migrations** (creates the audit table in your database)

```bash
python manage.py migrate
```

**Step 4 — Attach the PostgreSQL event trigger**

```bash
python manage.py attach_schema_audit
```

That's it. All DDL changes in your PostgreSQL database are now being recorded automatically.

---

## Model — `pg_schema_audit_log`

The app creates a single table `pg_schema_audit_log` with the following fields:

| Field          | Type            | Description                                              |
|----------------|-----------------|----------------------------------------------------------|
| `id`           | BigAutoField    | Auto-incrementing primary key                            |
| `event_time`   | DateTimeField   | Timestamp of when the DDL event occurred (auto-set)      |
| `username`     | CharField(150)  | PostgreSQL user who executed the command                 |
| `command_tag`  | CharField(100)  | Type of DDL command e.g. `CREATE TABLE`, `ALTER TABLE`   |
| `object_type`  | CharField(100)  | Type of object affected e.g. `table`, `index`, `sequence`|
| `schema_name`  | CharField(100)  | PostgreSQL schema where the change occurred e.g. `public`|
| `table_name`   | CharField(255)  | Full identity of the affected object                     |
| `ddl_command`  | TextField       | The complete DDL SQL statement that was executed         |

---

## Output — What Gets Recorded

Every time a DDL statement runs against your PostgreSQL database, a new row is inserted into `pg_schema_audit_log`. For example, running:

```sql
CREATE TABLE orders (
    id SERIAL PRIMARY KEY,
    total NUMERIC
);
```

Produces a record like:

| Field         | Value                                          |
|---------------|------------------------------------------------|
| `event_time`  | 2025-06-01 10:32:45 UTC                        |
| `username`    | postgres                                       |
| `command_tag` | CREATE TABLE                                   |
| `object_type` | table                                          |
| `schema_name` | public                                         |
| `table_name`  | public.orders                                  |
| `ddl_command` | CREATE TABLE orders (id SERIAL PRIMARY KEY, …) |

You can browse all audit records directly in **Django Admin** under the Schema Audit section, with search by table name or DDL command, and sorted by most recent event.

---

## Django Admin

The `pg_schema_audit_log` model is registered in Django Admin out of the box:

- Listed columns: `event_time`, `command_tag`, `object_type`, `table_name`, `username`
- Searchable by: `table_name`, `ddl_command`
- Default ordering: newest events first

---

## License

MIT
