mattiaslinnap / django-partial-index

PostgreSQL and SQLite partial index support for Django
BSD 3-Clause "New" or "Revised" License
116 stars 12 forks source link

For those who come here to upgrade to django 3.0 and higher #21

Open simkimsia opened 3 years ago

simkimsia commented 3 years ago

this library is in maintenance mode. so you need to move away if you want to upgrade to django 3.0

I am in the midst of doing so.

here's how I'm upgrading.

if u use unique=True

General idea: switch to UniqueConstraint with condition available in django 2.2. see https://docs.djangoproject.com/en/3.0/ref/models/constraints/#condition for details

Step by Step

  1. remain in 2.2
  2. remove your unique index then makemigrations
  3. add in the uniqueconstraint then makemigrations
  4. run migrate
  5. repeat step 2-4 until all removed
  6. drop partial index
  7. upgrade to 3.0 or higher

Done.

Step 2 and 3 can be compressed into a single step after you run step 2 and 3 separately once to gain confidence.

2 and 3 can become "replace partialindex with uniqueconstraint then makemigrations"

An actual example I use

Originally

    class Meta:
        indexes = [
            PartialIndex(
                fields=["serial_number", "sheet"], unique=True, where=PQ(is_removed=False)
            ),
        ]

Now

    class Meta:
        constraints = [
            models.UniqueConstraint(
                fields=["serial_number", "sheet"],
                condition=models.Q(is_removed=False),
                name="unique_serial_number_in_sheet_for_nondeleted",
            )
        ]

I almost exclusively use this library to write partial unique constraints. So I have nothing to add about situations where partial index is used for non unique. Sorry.