|
| 1 | +import asyncio |
| 2 | +import typing as t |
| 3 | +from logging.config import fileConfig |
| 4 | + |
| 5 | +from alembic import context |
| 6 | +from ellar.app import current_injector |
| 7 | + |
| 8 | +from ellar_sqlalchemy.migrations import ( |
| 9 | + MultipleDatabaseAlembicEnvMigration, |
| 10 | + SingleDatabaseAlembicEnvMigration, |
| 11 | +) |
| 12 | +from ellar_sqlalchemy.services import EllarSQLAlchemyService |
| 13 | + |
| 14 | +db_service: EllarSQLAlchemyService = current_injector.get(EllarSQLAlchemyService) |
| 15 | + |
| 16 | +# this is the Alembic Config object, which provides |
| 17 | +# access to the values within the .ini file in use. |
| 18 | +config = context.config |
| 19 | + |
| 20 | +# Interpret the config file for Python logging. |
| 21 | +# This line sets up loggers basically. |
| 22 | +fileConfig(config.config_file_name) # type:ignore[arg-type] |
| 23 | +# logger = logging.getLogger("alembic.env") |
| 24 | + |
| 25 | + |
| 26 | +AlembicEnvMigrationKlass: t.Type[ |
| 27 | + t.Union[MultipleDatabaseAlembicEnvMigration, SingleDatabaseAlembicEnvMigration] |
| 28 | +] = ( |
| 29 | + MultipleDatabaseAlembicEnvMigration |
| 30 | + if len(db_service.engines) > 1 |
| 31 | + else SingleDatabaseAlembicEnvMigration |
| 32 | +) |
| 33 | + |
| 34 | + |
| 35 | +# other values from the config, defined by the needs of env.py, |
| 36 | +# can be acquired: |
| 37 | +# my_important_option = config.get_main_option("my_important_option") |
| 38 | +# ... etc. |
| 39 | + |
| 40 | + |
| 41 | +alembic_env_migration = AlembicEnvMigrationKlass(db_service) |
| 42 | + |
| 43 | +if context.is_offline_mode(): |
| 44 | + alembic_env_migration.run_migrations_offline(context) # type:ignore[arg-type] |
| 45 | +else: |
| 46 | + asyncio.get_event_loop().run_until_complete( |
| 47 | + alembic_env_migration.run_migrations_online(context) # type:ignore[arg-type] |
| 48 | + ) |
0 commit comments