diff --git a/alembic.ini b/alembic.ini index 51765ea..18015ea 100644 --- a/alembic.ini +++ b/alembic.ini @@ -1,40 +1,42 @@ [alembic] # A comma separated list of paths to migration scripts to be run. By default, Alembic will look for versions # in the "versions" directory within the "migrations" folder. You can adjust this path based on your setup. script_location = migrations # The database URL to use for your database connections. You can also use environment variables here. # You should replace this with your actual database URL, for example: # sql_connection_url = postgresql://username:password@localhost/dbname sqlalchemy.url = sqlite:///./test.db # If you're using a database like PostgreSQL, it might look like: # sqlalchemy.url = postgresql://username:password@localhost/dbname # Enables logging of database migrations to stdout (useful for debugging) # logging config for Alembic log_file_config = true # Uncomment this if you want to use a logging config (e.g., to save logs to a file). # loggers = ['sqlalchemy.engine'] # Uncomment this if you'd like to run migrations in "offline" mode, where no live connection is made to the DB # and instead, the migrations are written to a file. # offline = true +version_locations = ./migrations/versions/storage, ./migrations/versions/auth + # You can also set this if you want the migrations to apply automatically in your production environment. # Uncomment below to auto-generate migrations. # autogenerate = true # Add any additional configurations for Alembic. Below is an example of logging setup: loggers = ['root', 'sqlalchemy', 'alembic'] databases = storage_db, auth_db [DEFAULT] script_location = migrations [storage_db] version_locations = ./migrations/versions/storage [auth_db] version_locations = ./migrations/versions/auth diff --git a/migrations/env.py b/migrations/env.py index a937331..3f71a39 100644 --- a/migrations/env.py +++ b/migrations/env.py @@ -1,97 +1,98 @@ import os import sys from alembic import context from application.process import process from sqlalchemy import engine_from_config, pool from sqlmodel import SQLModel # Add the `xcap` directory to the Python path so we can import our modules sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../"))) from xcap.configuration import DatabaseConfig as XCAPDatabaseConfig from xcap.db.manager import Base from xcap.db.models import XCAP, Subscriber, Watcher +process.configuration.subdirectory = 'openxcap' process.configuration.local_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "../")) class DatabaseConfig(XCAPDatabaseConfig): authentication_db_uri = '' storage_db_uri = '' config = context.config target_metadata = Base.metadata db_name = config.config_ini_section def include_object(object, name, type_, reflected, compare_to): model_class = next( (cls for cls in SQLModel.__subclasses__() if getattr(cls, "__tablename__", None) == name), None ) model_database = getattr(model_class, "__database__", None) if type_ == 'foreign_key_constraint' and compare_to and ( compare_to.elements[0].target_fullname == db_name + '.' + object.elements[0].target_fullname or db_name + '.' + compare_to.elements[0].target_fullname == object.elements[ 0].target_fullname): return False if type_ == 'table': if model_database == db_name or model_database is None: return True elif model_database == db_name or model_database is None: return True else: return False def run_migrations_online(): """Run migrations in 'online' mode.""" if db_name == 'auth_db': config.set_main_option("sqlalchemy.url", DatabaseConfig.authentication_db_uri) else: config.set_main_option("sqlalchemy.url", DatabaseConfig.storage_db_uri) connectable = engine_from_config( config.get_section(config.config_ini_section), prefix='sqlalchemy.', poolclass=pool.NullPool ) with connectable.connect() as connection: context.configure( connection=connection, target_metadata=target_metadata, include_object=include_object ) with context.begin_transaction(): context.run_migrations() def run_migrations_offline(): """Run migrations in 'offline' mode.""" if db_name == 'auth_db': url = DatabaseConfig.authentication_db_uri else: url = DatabaseConfig.storage_db_uri context.configure( url=url, target_metadata=target_metadata, include_object=include_object, literal_binds=True ) with context.begin_transaction(): context.run_migrations() if context.is_offline_mode(): run_migrations_offline() else: run_migrations_online()