8000 GitHub - nnseva/django2-postgres-backport: The collection of backports for the Django v2.x using modern PostgreSQL features
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

nnseva/django2-postgres-backport

Repository files navigation

Tests with postgres9.6 Tests with postgres12 Tests with postgres14

Django2-Postgres-Backport

The collection of backports for the Django v2.x using modern PostgreSQL features

Installation

Latest version from the GIT repository:

pip install "git+git://github.com/nnseva/django2-postgres-backport.git"

Stable version from the PyPi repository:

pip install django2-postgres-backport

Using modern versions of the psycopg package

The modern versions of the psycopg package allow using modern PostgreSQL database versions. The patch makes the Django v2.x code compatible with the modern psycopg package versions.

Note the PostGIS part of the Django v2.x requires additional forces (not yet implemented) to be compatible with the database.

Using

Use the following code in settings.py to patch the Django v2.x code:

from django2_postgres import psycopg_patch

psycopg_patch.fix()

Safe transfer to the modern Django versions

The patch code is version-dependent and just does nothing for the modern Django versions.

Add/Remove Index Concurrently

Introduce running Add and Remove PostgreSQL indexes concurrently as it is available in the Django version >= 3

Index declaration

Use the modern syntax in your model to declare indexes, for example:

class MyModel(models.Model):
    field1 = models.CharField(max_length=10)
    field2 = models.charField(max_length=10)
    class Meta:
        indexes = [
            models.Index(fields=['field1', 'field2'], name='my_index'),
        ]

Automatic creation of a migration

Create a migration as usual, for example:

python manage.py makemigrations mypackage

Manual updating of the migration file

Modify a migration to use the CREATE INDEX CONCURRENTLY manually.

  • import the backport
  • mark the migration as non-atomic
  • replace the AddIndex by the AddIndexConcurrently
  • replace the RemoveIndex by the RemoveIndexConcurrently

For example:

# Generated by Django 2.2.5 on 2025-05-26 13:51

from django.db import migrations, models
# -- import the backport
from django2_postgres import operations


class Migration(migrations.Migration):

    dependencies = [
        ('tests', '0001_initial'),
    ]
# -- The concurrent index creation can be only in non-atomic migration
    atomic = False

    operations = [
# -- We remove the originally generated operation ...
#        migrations.AddIndex(
#            model_name='mymodel',
#            index=models.Index(fields=['field1', 'field2'], name='my_index'),
#        ),
# -- And replace it by a modified one
        operations.AddIndexConcurrently(
            model_name='mymodel',
            index=models.Index(fields=['field1', 'field2'], name='my_index'),
        ),
    ]

Side effects

Using concurrent index creation has its own side effects described in the Django documentation, and in the PostgreSQL documentation, please read it carefully.

The backport doesn't add any additional side effects.

Safe transfer to the modern Django versions

The modern Django versions (>=3.0) have originally provided versions of these functions.

The backport files are version-dependent and provide original Django operations instead of backported, when the version of the Django is upgraded. Therefore, you can use all your old migrations without any changes when transfer your code to the new Django version.

About

The collection of backports for the Django v2.x using modern PostgreSQL features

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0