-
Notifications
You must be signed in to change notification settings - Fork 0
Show new migrations #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,14 @@ def add_arguments(self, parser): | |
| '"default" database.' | ||
| ), | ||
| ) | ||
| parser.add_argument( | ||
| "--unapplied", | ||
| "-u", | ||
| action="store_true", | ||
| help=( | ||
| "Like `--list`, but only shows unapplied migrations" | ||
| ), | ||
| ) | ||
|
|
||
| formats = parser.add_mutually_exclusive_group() | ||
| formats.add_argument( | ||
|
|
@@ -55,15 +63,18 @@ def add_arguments(self, parser): | |
|
|
||
| def handle(self, *args, **options): | ||
| self.verbosity = options["verbosity"] | ||
|
|
||
| self.show_unapplied_only = options["unapplied"] | ||
| # Get the database we're operating from | ||
| db = options["database"] | ||
| connection = connections[db] | ||
|
|
||
| if options["format"] == "plan": | ||
| return self.show_plan(connection, options["app_label"]) | ||
| else: | ||
| return self.show_list(connection, options["app_label"]) | ||
| return self.show_list(connection, | ||
| options["app_label"], | ||
| show_unapplied_only=self.show_unapplied_only, | ||
| ) | ||
|
|
||
| def _validate_app_names(self, loader, app_names): | ||
| has_bad_names = False | ||
|
|
@@ -76,7 +87,7 @@ def _validate_app_names(self, loader, app_names): | |
| if has_bad_names: | ||
| sys.exit(2) | ||
|
|
||
| def show_list(self, connection, app_names=None): | ||
| def show_list(self, connection, app_names=None, show_unapplied_only=False): | ||
| """ | ||
| Show a list of all migrations on the system, or only those of | ||
| some named apps. | ||
|
|
@@ -95,8 +106,8 @@ def show_list(self, connection, app_names=None): | |
| # For each app, print its migrations in order from oldest (roots) to | ||
| # newest (leaves). | ||
| for app_name in app_names: | ||
| self.stdout.write(app_name, self.style.MIGRATE_LABEL) | ||
| shown = set() | ||
| outputs = [] | ||
| for node in graph.leaf_nodes(app_name): | ||
| for plan_node in graph.forwards_plan(node): | ||
| if plan_node not in shown and plan_node[0] == app_name: | ||
|
|
@@ -123,12 +134,20 @@ def show_list(self, connection, app_names=None): | |
| "%Y-%m-%d %H:%M:%S" | ||
| ) | ||
| ) | ||
| self.stdout.write(output) | ||
| if not show_unapplied_only: | ||
| outputs.append(output) | ||
| else: | ||
| self.stdout.write(" [ ] %s" % title) | ||
| output = " [ ] %s" % title | ||
| outputs.append(output) | ||
| shown.add(plan_node) | ||
| if outputs: | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ideally I'd separate out the concerns of fetching the plans, determining what to print, and the print format into separate functions. There is some duplication between the |
||
| self.stdout.write(app_name, self.style.MIGRATE_LABEL) | ||
| for output in outputs: | ||
| self.stdout.write(output) | ||
|
|
||
| # If we didn't print anything, then a small message | ||
| if not shown: | ||
| self.stdout.write(app_name, self.style.MIGRATE_LABEL) | ||
| self.stdout.write(" (no migrations)", self.style.ERROR) | ||
|
|
||
| def show_plan(self, connection, app_names=None): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels a bit hacky, and the existing code could also use some refactoring to seperate out some concerns.
But I want to be able to not print an app label if there aren't associated migrations to print out with it.