Skip to content

Commit 61dc944

Browse files
committed
Create documentation for migrations - closes #246
1 parent 99495e7 commit 61dc944

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Dropped support for Python 3.5
1010
* Dropped support for Django below 2.2.x LTS release
1111
* Added support for BigIntegerFields (#169)
12+
* Added documentation for migration existing data (#246)
1213

1314
## 2.5.2
1415

README.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ Example:
328328

329329
## Limitations
330330

331-
#### `.distinct('encrypted_field_name')`
331+
### `.distinct('encrypted_field_name')`
332332

333333
Due to a missing feature in the Django ORM, using `distinct()` on an encrypted field
334334
does not work for Django 2.0.x and lower.
@@ -364,6 +364,52 @@ items = EncryptedFKModel.objects.filter(
364364
This works because the annotated field is auto-decrypted by Django as a `F` field and that
365365
field is used in the `distinct()`.
366366

367+
### Migrating existing fields into PGCrypto Fields
368+
369+
Migrating existing fields into PGCrypto Fields is not performed by this library. You will need to migrate the data
370+
in a forwards migration or other means. The only migration that is supported except to create/activate the pgcrypto
371+
extension in Postgres.
372+
373+
Migrating data is complicated as there might be a few things to consider such as:
374+
375+
* the shape of the data
376+
* validations/constrains done on the table/model/form and anywhere else
377+
378+
The library has no way of doing all these guesses or to make all these decisions.
379+
380+
If you need to migrate data from unencrypted fields to encrypted fields, three ways to solve it:
381+
382+
1. When there's no data in the db it should be possible to start from scratch by recreating the db
383+
1. When there's no data in the table it should be possible to recreate the table
384+
1. When there's data or if the project is shared it should be possible to do it in a non destructive way
385+
386+
**Option 1: No data is in the db**
387+
388+
1. Drop the database
389+
1. Squash the migrations
390+
1. Recreate the db
391+
392+
**Option 2: No data in the table**
393+
394+
1. Create a migration to drop the table
395+
1. Create a new migration for the table with the encrypted field
396+
1. Optionally squash the migration
397+
398+
**Option 3: Migrating in a non-destructive way**
399+
400+
The goal here is to be able to use to legacy field if something goes wrong.
401+
402+
Part 1:
403+
404+
1. Create new field
405+
1. When data is saved write both to legacy and new field
406+
1. Create a data migration to cast data from legacy field to new field
407+
1. check existing data from legacy and new field are the same if possible
408+
409+
Part 2:
410+
411+
1. Rename the fields and drop legacy fields
412+
1. Update the code to use only the new field
367413

368414
## Security Limitations
369415

0 commit comments

Comments
 (0)