Skip to content

migrations Directory

Purpose

This directory contains Django database migration files for the parodynews application. These migrations define database schema changes, table creation, field modifications, and data transformations that evolve the database structure over time while maintaining data integrity.

Contents

  • __init__.py: Python package initialization file making this directory a Python package
  • Migration files (generated by Django): Python files that define database schema changes and data migrations

Notable migrations

  • 0002_provider_agnostic_ai: The v0.6.0 refactor. Renames OpenAIModel to AIModel and adds its provider field, gives threads and messages locally minted ids, tags legacy assistant replies by the OpenAI run_id they carry, creates AIProviderConfig, and copies the stored OpenAI credentials out of AppConfig before dropping those columns.
  • 0003_drop_nullable_text_columns: Backfills NULLs in Assistant.name, Assistant.description and Message.run_id, then makes the columns NOT NULL with a default.

Usage

Django migrations are managed through Django's migration framework:

# Create new migrations after model changes
python manage.py makemigrations parodynews

# Apply migrations to database
python manage.py migrate

# Show migration status
python manage.py showmigrations

# Reverse migrations (if needed)
python manage.py migrate parodynews <migration_number>

# Create empty migration for custom operations
python manage.py makemigrations --empty parodynews

Reversibility is not automatic

Both v0.6.0 migrations were written to reverse cleanly, and neither did so on the first attempt. Two traps worth knowing, because Django will not warn you:

  1. Reversing a RemoveField re-adds the column with no default. If the column was NOT NULL, the reverse fails on any populated table before your restore step can run. 0002 fixes this by altering the three AppConfig columns to blank=True, default="" before removing them, so the reverse AddField succeeds and the restore then repopulates them.
  2. null=True to NOT NULL fails while NULLs remain. Postgres refuses SET NOT NULL outright. 0003 backfills first in a RunPython step.

Test the whole cycle, not just forward, against a database with realistic rows:

python manage.py migrate parodynews 0001    # reverse
python manage.py migrate parodynews         # forward again

Migration workflow: 1. Model Changes: Modify Django models in models.py 2. Generate Migrations: Run makemigrations to create migration files 3. Review: Check generated migration files for correctness 4. Apply: Run migrate to apply changes to database 5. Version Control: Commit migration files with model changes

Container Configuration

Migrations are executed within the Django application environment: - Applied automatically during container startup in production - Can be run manually in development containers - Database connection configured through Django settings - PostgreSQL-only (this project does not support SQLite)

  • Incoming: Generated from Django model definitions and schema changes
  • Outgoing: Applied to database to create/modify tables and data structures