Skip to content

Commit

Permalink
add option to get ddl only from selected apps
Browse files Browse the repository at this point in the history
  • Loading branch information
ronenlu committed Jan 24, 2024
1 parent 292a2da commit c6c2548
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __str__(self):
return self.value


current_dialect = Dialect.sqlite
current_dialect = Dialect.mysql


class MockSqliteSchemaEditor(SqliteSchemaEditor):
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 5 additions & 3 deletions atlas_provider_django/management/commands/migrations.py
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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

0 comments on commit c6c2548

Please sign in to comment.