Opened 12 months ago

Closed 12 months ago

Last modified 12 months ago

#34974 closed Uncategorized (duplicate)

makemigrations --check does not show diff as documented

Reported by: Oliver Ford Owned by: nobody
Component: Uncategorized Version: 4.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The [docs](https://docs.djangoproject.com/en/4.2/ref/django-admin/#django-admin-makemigrations) say:

--check¶
Makes makemigrations exit with a non-zero status when model changes without migrations are detected. Implies --dry-run.

which:

--dry-run¶
Shows what migrations would be made without actually writing any migrations files to disk. Using this option along with --verbosity 3 will also show the complete migrations files that would be written.

emphasis mine.

In CI, would be very useful to have a single command which both renders the missing/problematic migration and also exits non-zero; I think this should be makemigrations --check, as it's already documented as doing.

Alternatively, maybe --check should _not_ imply --dry-run at all in any sense - i.e. it should write to disk if --dry-run not given. I'm not sure I like that though, I think it makes sense that 'check' is idempotent.

Black for example has --diff rather than --dry-run, and both prevent writing to disk, but can be combined for an exit code as well as the change printed:

--check Don't write the files back, just return the

status. Return code 0 means nothing would
change. Return code 1 means some files would
be reformatted. Return code 123 means there
was an internal error.

--diff Don't write the files back, just output a

diff for each file on stdout.

(can't figure out this formatting...)

Change History (5)

comment:1 by David Sanders, 12 months ago

Resolution: duplicate
Status: newclosed

Hi Oliver,

Thanks for the report. The documentation was for a patch that was recently added to the 4.2.x branch with #34457. The patch was only merged 8 days ago and doesn't look like it's released yet. Please bear with us as we await its release :)

Marking as duplicate of #34457.

comment:2 by David Sanders, 12 months ago

Also FYI here's the release post for 4.2.8: https://docs.djangoproject.com/en/dev/releases/4.2.8/

Expected 4 December 2023

comment:3 by Oliver Ford, 12 months ago

Ah, what I was actually hoping for was the full contents of the file, not just the would-be file name, and model it affects.

But it seems I misunderstood, and --dry-run doesn't do that either. Is there already a feature request for a --dry-run --diff or something do you know?

i.e. to build in what would be worked around by _not_ using --dry-run, grepping out the filename and printing the contents.

comment:4 by David Sanders, 12 months ago

Is there already a feature request for a --dry-run --diff or something do you know?

I don't believe there is sorry 🤷‍♂️

If you want to write something yourself you can update makemigrations to simply write to sys.stdout:

@@ -360,9 +360,8 @@ class Command(BaseCommand):
                         # We just do this once per app
                         directory_created[app_label] = True
                     migration_string = writer.as_string()
-                    with open(writer.path, "w", encoding="utf-8") as fh:
-                        fh.write(migration_string)
-                        self.written_files.append(writer.path)
+                    sys.stdout.write(migration_string)
+                    self.written_files.append(writer.path)
                     if update_previous_migration_paths:
                         prev_path = update_previous_migration_paths[app_label]
                         rel_prev_path = self.get_relative_path(prev_path)

That will print both the log message that shows the written file + the contents. You can disable the logging easily with -v 0 👍

comment:5 by Jacob Walls, 12 months ago

There is a simple way to get the full migration file: you can run makemigrations --check --verbosity 3 (that is, once 4.2.8 is out).

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