Migration dependencies between optional apps

Hello everyone. I have a question about writing migrations. I’ve currently got two separate apps, let’s call them AppOne and AppTwo.

Due to the way we structure our system, both of these apps are optional dependencies. So some sites will have just AppOne installed, others have just AppTwo, some have both AppOne and AppTwo.

The snag I’m hitting is making sure the migrations for both of these apps run in the correct order. Normally I’d use the dependencies or run_before lists, however because the apps are optional this doesn’t work. A site with only one of the two apps installed will error due to the reference to an unavailable migration in the other app.

Is there a way to have stuff in a migration’s dependencies that might not be installed? Is there some other means of guaranteeing a particular run order on migrations for unrelated apps? I thought routers might be able to help with this, but there doesn’t seem to be anything in there to control migration order.

This might be a dumb question. But can’t you programatically set the dependencies based on AppOne or AppTwo being on INSTALLED_APPS?