From c6c2548d4d19b78a994ed42e5c588b582075d5d7 Mon Sep 17 00:00:00 2001 From: Ronen Lubin Date: Wed, 24 Jan 2024 10:36:57 +0200 Subject: [PATCH] add option to get ddl only from selected apps --- .../management/commands/atlas-provider-django.py | 13 ++++++++----- .../management/commands/migrations.py | 8 +++++--- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/atlas_provider_django/management/commands/atlas-provider-django.py b/atlas_provider_django/management/commands/atlas-provider-django.py index 7e11456..6caf1df 100644 --- a/atlas_provider_django/management/commands/atlas-provider-django.py +++ b/atlas_provider_django/management/commands/atlas-provider-django.py @@ -28,7 +28,7 @@ def __str__(self): return self.value -current_dialect = Dialect.sqlite +current_dialect = Dialect.mysql class MockSqliteSchemaEditor(SqliteSchemaEditor): @@ -166,20 +166,23 @@ class Command(BaseCommand): help = "Import Django migrations into Atlas" def add_arguments(self, parser): - parser.add_argument("--dialect", type=Dialect, choices=list(Dialect), help="The database dialect to use.", + parser.add_argument("--dialect", type=Dialect, choices=list(Dialect), + help="The database dialect to use, Default: mysql", default=Dialect.sqlite) + parser.add_argument("--apps", nargs="+", help="List of apps to get ddl for.") SqlMigrateCommand.handle = mock_handle def handle(self, *args, **options): global current_dialect current_dialect = options.get("dialect", Dialect.sqlite) - print(self.get_ddl()) + selected_apps = options.get("apps", None) + print(self.get_ddl(selected_apps)) # Load migrations and get the sql statements describing the migrations. - def get_ddl(self): + def get_ddl(self, selected_apps): ddl = "" - for app_name, migration_name in get_migrations(): + for app_name, migration_name in get_migrations(selected_apps): try: out = StringIO() call_command( diff --git a/atlas_provider_django/management/commands/migrations.py b/atlas_provider_django/management/commands/migrations.py index d43cb5e..dd540ca 100644 --- a/atlas_provider_django/management/commands/migrations.py +++ b/atlas_provider_django/management/commands/migrations.py @@ -1,14 +1,14 @@ from django.db.migrations.autodetector import MigrationAutodetector from django.db.migrations.state import ProjectState from django.db.migrations.loader import MigrationLoader -from django.apps import apps +from django.apps import apps as all_apps # Creates the migrations of the installed apps from empty baseline and returns them as a dictionary -def get_migrations(): +def get_migrations(apps=None): autodetector = MigrationAutodetector( ProjectState(), - ProjectState.from_apps(apps), + ProjectState.from_apps(all_apps), ) loader = MigrationLoader(None, ignore_no_migrations=True) changes = autodetector.changes( @@ -18,5 +18,7 @@ def get_migrations(): ) migrations = {} for app_label, app_migrations in changes.items(): + if apps and app_label not in apps: + continue migrations[(app_label, app_migrations[0].name)] = app_migrations[0] return migrations