Opened 5 weeks ago

Closed 4 weeks ago

#36016 closed Cleanup/optimization (fixed)

Avoid traceback when quitting makemigrations with Ctrl-C

Reported by: Adam Johnson Owned by: amansharma612
Component: Migrations Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Adam Johnson)

Sometimes makemigration asks you questions with its “questioner”. If you realize that you missed something due to these questions, you might want to quit with Ctrl-C and edit your models file before rerunning.

Currently, such a quit triggers Python’s default traceback:

$ ./manage.py makemigrations example
It is impossible to add a non-nullable field 'title' to book without specifying a default. This is because the database needs something to populate existing rows.
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
 2) Quit and manually define a default value in models.py.
Select an option:  ^CTraceback (most recent call last):
  File "/.../manage.py", line 21, in <module>
    main()
    ~~~~^^
  ...
  ...
  ...
  File "/.../django/db/migrations/questioner.py", line 169, in ask_not_null_addition
    choice = self._choice_input(
        f"It is impossible to add a non-nullable field '{field_name}' "
    ...<10 lines>...
        ],
    )
  File "/Users/chainz/tmp/django-makemigrations-ctrl-c/.venv/lib/python3.13/site-packages/django/db/migrations/questioner.py", line 114, in _choice_input
    result = input()
KeyboardInterrupt

This isn’t very user-friendly: it seems like maybe you broke Django, and it pushes the question up out of the terminal window.

Instead, we could quit with minimal output, perhaps a simple message like:

Select an option:  ^C
Cancelled.

If there's a message, it should probably use the notice style.

Change History (9)

comment:1 by Adam Johnson, 5 weeks ago

Description: modified (diff)

comment:2 by Adam Johnson, 5 weeks ago

Description: modified (diff)

comment:3 by Adam Johnson, 5 weeks ago

Description: modified (diff)

comment:4 by amansharma612, 5 weeks ago

I think one possible way to implement this would be to add an except KeyboardInterrupt statement after the existing except ValueError block in the _choice_input definition in questioner.py and moving the result = input() statement inside the try block. I can open a PR for the same if seems reasonable.

Last edited 5 weeks ago by amansharma612 (previous) (diff)

comment:5 by amansharma612, 5 weeks ago

Owner: set to amansharma612
Status: newassigned

comment:6 by amansharma612, 5 weeks ago

Last edited 5 weeks ago by amansharma612 (previous) (diff)

comment:7 by Sarah Boyce, 5 weeks ago

Has patch: set
Needs tests: set
Triage Stage: UnreviewedAccepted
Type: New featureCleanup/optimization

Thank you! Agree this makes sense, I don't think this needs documenting as might be more of a cleanup but needs tests

comment:8 by Sarah Boyce, 4 weeks ago

Needs tests: unset
Triage Stage: AcceptedReady for checkin

comment:9 by Sarah Boyce <42296566+sarahboyce@…>, 4 weeks ago

Resolution: fixed
Status: assignedclosed

In f05edb2:

Fixed #36016 -- Prevented traceback when quitting makemigrations with Ctrl-C.

Note: See TracTickets for help on using tickets.
Back to Top