Opened 5 years ago

Closed 5 years ago

#31318 closed Cleanup/optimization (fixed)

sqlmigrate doesn't allow inspecting migrations that have been squashed

Reported by: Adam Johnson Owned by: David Wobrock
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

This project for another ticket can be used to reproduce: https://github.com/adamchainz/django-unique-together-bug

When running sqlmigrate to pick up migration 0001 in this project, it complains that two migrations have that prefix:

$ python manage.py sqlmigrate testapp 0001
CommandError: More than one migration matches '0001' in app 'testapp'. Please be more specific.

But when trying to be more specific, it's not possible to load it:

$ python manage.py sqlmigrate testapp 0001_initial
Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/.../django/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/.../django/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/.../django/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/.../django/django/core/management/commands/sqlmigrate.py", line 30, in execute
    return super().execute(*args, **options)
  File "/.../django/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/.../django/django/core/management/commands/sqlmigrate.py", line 64, in handle
    plan = [(executor.loader.graph.nodes[targets[0]], options['backwards'])]
KeyError: ('testapp', '0001_initial')

It would be nice to:

A) catch this error and report a nice message as well
B) allow inspection of individual migrations that have been involved in a squash. Normally the workflow is to remove the individual migrations some time after committing the squash, but until that is done it could be useful to see their sql.

Change History (11)

comment:1 by David Wobrock, 5 years ago

Owner: changed from nobody to David Wobrock
Status: newassigned

Hi! This seems quite relevant to me, as in the company I work for, we'll probably start using squashed migration soon-ish and we use sqlmigrate extensively.

I'd like to tackle this change :)
It does not seem too complicated to allowed inspecting the SQL of replaced migrations.
I already wrote a small test that reproduces the behaviour described in the stacktrace.

Technically, I guess I should be able to find an elegant way to detect the migration was replaced and find it in the MigrationLoader.
Visually, should the command display that the inspected migration is replaced and by which squashed migration?

Thanks!
David

comment:2 by David Wobrock, 5 years ago

Has patch: set

comment:3 by Mariusz Felisiak, 5 years ago

Triage Stage: UnreviewedAccepted

comment:4 by Adam Johnson, 5 years ago

Sorry didn't see your comment until now, went through my spam box.

Visually, should the command display that the inspected migration is replaced and by which squashed migration?

I don't think that's necessary. The recommended procedure with a squashed migration is to drop the originals after every developer and deployment has received the squashed one. So the state where both exist in parallel doesn't live for long.

in reply to:  4 comment:5 by David Wobrock, 5 years ago

Replying to Adam (Chainz) Johnson:

Sorry didn't see your comment until now, went through my spam box.

Visually, should the command display that the inspected migration is replaced and by which squashed migration?

I don't think that's necessary. The recommended procedure with a squashed migration is to drop the originals after every developer and deployment has received the squashed one. So the state where both exist in parallel doesn't live for long.

Yep, that's what I expected and it's the strategy I took in the PR. Just showing the SQL queries, just as if no replacement existed.

comment:6 by Mariusz Felisiak, 5 years ago

Patch needs improvement: set

comment:7 by Mariusz Felisiak, 5 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:8 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

In b88ad1d3:

Refs #31318 -- Added tests for inspecting squashed migrations and ambiguous names in sqlmigrate.

comment:9 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

In 71c1b7f:

Refs #31318 -- Moved MigrationExecutor.collect_sql() to MigrationLoader.

collect_sql() is used only in sqlmigrate.

comment:10 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

In 271e108b:

Refs #31318 -- Optimized sqlmigrate by using MigrationLoader.

Only loader from MigrationExecutor was used.

comment:11 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In d8836570:

Fixed #31318 -- Allowed sqlmigrate to inspect squashed migrations.

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