Repository navigation
Fix phonenumber 0001_initial leaving two_factor_phonedevice table uncreated - #803
wahajahmed010 wants to merge 1 commit into
Conversation
…wo_factor The phonenumber plugin's 0001_initial migration used SeparateDatabaseAndState with only state_operations, so it never created the two_factor_phonedevice table. On a live deployment where two_factor was migrated first and two_factor.plugins.phonenumber was added to INSTALLED_APPS afterwards, 0001_initial would be recorded as applied and then the squashed 0001_squashed_0001_initial would be skipped as already applied, leaving the database without the table needed for SMS / phone call authentication. Extract the custom CreatePhoneDevice operation into a shared module so both 0001_initial and 0001_squashed_0001_initial use the same code path. The operation checks for an existing two_factor_phonedevice table and creates it only when missing, preserving the upgrade safety from jazzband#611. Fixes jazzband#800
|
Not sure I like this approach as I don't think it really addresses the core problem that causes #800 Also I'm fairly sure this is AI generated |
|
You were right to close this - I went back, reproduced the actual scenario instead of pattern-matching the migration files, and the mechanism is different. Scenario from issue #800: project already migrated So the previous fix here was wired into the wrong migration for this path. The one that runs when the plugin is enabled later is I have that fix prepared: https://github.com/wahajahmed010/django-two-factor-auth/tree/fix/800-create-phonedevice-on-late-install
Happy to open a PR if you'd consider another attempt - no pressure if you'd rather fix it your own way, given the last one missed the point. |
Description
The
two_factor.plugins.phonenumberapp's0001_initialmigration usedSeparateDatabaseAndStatewith onlystate_operations. When a project that already hadtwo_factormigrated addedtwo_factor.plugins.phonenumbertoINSTALLED_APPSand migrated,0001_initialwas recorded as applied without ever creating thetwo_factor_phonedevicedatabase table. The squashed0001_squashed_0001_initialwas then skipped because Django saw0001_initialas already applied, leaving the schema with noPhoneDevicetable and no way to enable SMS or phone-call authentication without manual intervention.This change extracts the custom
CreatePhoneDeviceoperation into a shared module (two_factor/plugins/phonenumber/migrations/_operations.py) and uses it from both0001_initialand0001_squashed_0001_initial. The operation checks whethertwo_factor_phonedevicealready exists and creates it only when missing, which preserves the upgrade safety from #611 (a database that already has the table from the oldtwo_factorapp is left untouched) while also creating the table on a fresh0001_initialrun.Motivation and Context
Fixes #800.
Before the fix, the only ways to recover a system affected by this bug were:
0001_initialrow fromdjango_migrations, then re-run the squashed migration, ortwo_factor_phonedevicetable.After the fix, the next
migrateafter adding the app toINSTALLED_APPScreates the table.How Has This Been Tested?
Added
tests/test_migrations_phonenumber.pycovering both code paths:test_initial_migration_creates_table_when_missing: drops the table, un-recordsphonenumber.0001_initial, and verifies that runningmigrate phonenumber 0001_initialrecreates it. This fails against the original code.test_noop_when_table_already_exists: verifies that running the migration against a database that already hastwo_factor_phonedevicedoes not raise.makemigrations --dry-run --checkreports no missing migrations.Types of changes
Checklist: