Django PG Migration Tools

0.1.26 · active · verified Fri Apr 17

django-pg-migration-tools is a Python library that provides a set of specialized Django migration operations designed to make database schema changes safer and more scalable on PostgreSQL. It includes tools for idempotent model creation, concurrent index and constraint management, function migration, and concurrent column alterations, aiming to minimize downtime and avoid common locking issues during deployments. The current version is 0.1.26, and it typically sees releases for new features or bug fixes rather than a fixed cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `AddIndexConcurrently` within a Django migration. Note that for any 'CONCURRENTLY' operation, the migration must set `atomic = False`. This example assumes you have a Django project and an app named `your_app_name` with a model `yourmodelname` and a `field_name`.

import os
from django.db import migrations, models
from django_pg_migration_tools.operations import AddIndexConcurrently

# Example of a Django migration using AddIndexConcurrently
# This migration file would typically be in app_name/migrations/000X_...

class Migration(migrations.Migration):
    atomic = False # Required for CONCURRENTLY operations

    dependencies = [
        ('your_app_name', '0001_initial'), # Replace with your actual dependency
    ]

    operations = [
        # Add a B-tree index concurrently on 'field_name' of 'model_name'
        AddIndexConcurrently(
            model_name='yourmodelname',
            name='yourmodelname_field_name_idx',
            fields=['field_name'],
            db_tablespace='', # Optional: specify tablespace
            opclasses=[],     # Optional: specify opclasses for the index
            condition=None    # Optional: specify a partial index condition
        ),
        # Other operations could follow
    ]

view raw JSON →