#30023: SQLite schema editor can cause table corruption if unsed within a
transaction since Django 2.0
-------------------------------------+-------------------------------------
               Reporter:  Simon      |          Owner:  Simon Charette
  Charette                           |
                   Type:  Bug        |         Status:  assigned
              Component:             |        Version:  2.0
  Migrations                         |
               Severity:  Release    |       Keywords:
  blocker                            |
           Triage Stage:  Accepted   |      Has patch:  0
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 From Django 2.0+ SQLite schema editor requires foreign key constraint
 checking to be disabled before performing any operations to make sure
 foreign keys are not left pointing at `__old` tables when performing
 operations requiring table rebuilds.

 Since [https://sqlite.org/foreignkeys.html#fk_enable SQLite prevents
 foreign key constraint checking from being disabled within a transaction]
 `SchemaEditor(atomic=True).__enter__()` has no choice but to disable
 foreign key constraints **before** opening the transaction meant to ensure
 atomic DDL operations.

 One edge case that `SchemaEditor` doesn't account for though is that the
 it might contextually be used within an already opened transaction that
 would prevent foreign key constraints from being effectively disabled and
 result in silent referent table corruption when an operation requiring a
 table is rebuild id performed.

 In order to prevent this from happening `SchemaEditor().__enter__()`
 should ensure foreign key constraint checks are effectively disabled after
 requesting it and error out if it's not the case.

 This assertion should be more adequate than preventing schema editor from
 being used in a transaction altogether as disabling constraint checks
 before opening a transaction works just fine as well.

 For example

 {{{#!python
 with transaction.atomic():
     call_command('migrate')
 }}}

 Just has to be converted to

 {{{#!python
 with connection.constraint_checks_disabled(), transaction.atomic():
     call_command('migrate')
 }}}

 And work just fine.

 ''This is was originally reported in #29182 but was hijacked to deal with
 a SQLite 3.26 issue with similar symptoms and can be reproduced in a
 [https://github.com/ezaquarii/django-sqlite-migration-bug test project].

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30023>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.760e573a389b25011818dd0fc2b522c3%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to