<!--
title: Ensure migration compatibility
domain: security
topic: Migrations
language: Python
source: prowler-cloud/prowler
updated: 2025-06-12
url: https://awesomereviewers.com/reviewers/prowler-ensure-migration-compatibility/
-->

When modifying database schemas through migrations, ensure compatibility with existing data. For new required fields, use `null=True` or provide appropriate default values to prevent failures on existing records. Always keep migration files synchronized with model changes, and test migrations against a representative dataset before deploying to production.

For fields added to models that may already have data:
```python
# Good practice:
first_seen_at = models.DateTimeField(null=True, editable=False)
# Or with a default value:
first_seen_at = models.DateTimeField(default=timezone.now, editable=False)

# Problematic - will fail on existing data:
first_seen_at = models.DateTimeField(editable=False)  # No null or default
```

For complex migrations involving raw SQL or custom Python functions, define and test reverse operations (`RunSQL` or `RunPython`). When true reversal is not possible (like adding enum values), use `migrations.RunSQL.noop` to explicitly indicate this limitation. Consider performance impacts when migrations affect large datasets, as this may block application startup.
