1from logging.config import fileConfig 2 3from sqlalchemy import engine_from_config, pool 4 5from alembic import context 6 7# import backend models for autogenerate support 8from backend.config import settings 9from backend.models import ( # noqa: F401 10 Artist, 11 CopyrightScan, 12 Track, 13 TrackComment, 14 TrackLike, 15 UserPreferences, 16 UserSession, 17) 18from backend.models.database import Base 19 20# this is the Alembic Config object, which provides 21# access to the values within the .ini file in use. 22config = context.config 23 24# override sqlalchemy.url with value from settings 25config.set_main_option("sqlalchemy.url", settings.database.url) 26 27# Interpret the config file for Python logging. 28# This line sets up loggers basically. 29if config.config_file_name is not None: 30 fileConfig(config.config_file_name) 31 32# add your model's MetaData object here 33# for 'autogenerate' support 34target_metadata = Base.metadata 35 36# other values from the config, defined by the needs of env.py, 37# can be acquired: 38# my_important_option = config.get_main_option("my_important_option") 39# ... etc. 40 41 42def run_migrations_offline() -> None: 43 """Run migrations in 'offline' mode. 44 45 This configures the context with just a URL 46 and not an Engine, though an Engine is acceptable 47 here as well. By skipping the Engine creation 48 we don't even need a DBAPI to be available. 49 50 Calls to context.execute() here emit the given string to the 51 script output. 52 53 """ 54 url = config.get_main_option("sqlalchemy.url") 55 context.configure( 56 url=url, 57 target_metadata=target_metadata, 58 literal_binds=True, 59 dialect_opts={"paramstyle": "named"}, 60 ) 61 62 with context.begin_transaction(): 63 context.run_migrations() 64 65 66def run_migrations_online() -> None: 67 """Run migrations in 'online' mode. 68 69 In this scenario we need to create an Engine 70 and associate a connection with the context. 71 72 """ 73 connectable = engine_from_config( 74 config.get_section(config.config_ini_section, {}), 75 prefix="sqlalchemy.", 76 poolclass=pool.NullPool, 77 ) 78 79 with connectable.connect() as connection: 80 context.configure(connection=connection, target_metadata=target_metadata) 81 82 with context.begin_transaction(): 83 context.run_migrations() 84 85 86if context.is_offline_mode(): 87 run_migrations_offline() 88else: 89 run_migrations_online()