Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove run_migrations_offline, make run_migrations_online sync #2239

Merged
merged 10 commits into from
Feb 4, 2025
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* Deprecate `use_dataset_filters` query parameter for `/project/{project_id}/dataset/{dataset_id}/images/query/` (\#2231).
* App:
* Add fractal-server version to logs (\#2228).

* Database:
* Remove `run_migrations_offline` from `env.py` and make `run_migrations_online` sync (\#2239).

# 2.12.0

Expand All @@ -22,6 +23,7 @@
* Drop V1 tests (\#2230).
* Update V2 tests to keep coverage stable (\#2230).


# 2.11.1

* Database
Expand Down
9 changes: 6 additions & 3 deletions fractal_server/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ def set_db(skip_init_data: bool = False):
from pathlib import Path
import fractal_server

# Check settings
settings = Inject(get_settings)
settings.check_db()

# Perform migrations
alembic_ini = Path(fractal_server.__file__).parent / "alembic.ini"
alembic_args = ["-c", alembic_ini.as_posix(), "upgrade", "head"]
print(f"START: Run alembic.config, with argv={alembic_args}")
Expand All @@ -138,12 +143,10 @@ def set_db(skip_init_data: bool = False):
if skip_init_data:
return

# Insert default group
# Create default group and user
print()
_create_first_group()
print()
# NOTE: It will be fixed with #1739
settings = Inject(get_settings)
asyncio.run(
_create_first_user(
email=settings.FRACTAL_DEFAULT_ADMIN_EMAIL,
Expand Down
79 changes: 16 additions & 63 deletions fractal_server/migrations/env.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import asyncio
from logging.config import fileConfig

from alembic import context
from sqlalchemy.engine import Connection
from sqlmodel import SQLModel

from fractal_server.config import get_settings
from fractal_server.migrations.naming_convention import NAMING_CONVENTION
from fractal_server.syringe import Inject

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
# Alembic Config object (provides access to the values within the .ini file)
config = context.config


Expand All @@ -20,77 +15,35 @@
fileConfig(config.config_file_name)


# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = SQLModel.metadata
target_metadata.naming_convention = NAMING_CONVENTION
# Importing `fractal_server.app.models` after defining
# Importing `fractal_server.app.models` *after* defining
# `SQLModel.metadata.naming_convention` in order to apply the naming convention
# when autogenerating migrations (see issue #1819).
from fractal_server.app import models # noqa

# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.


def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.

This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.

Calls to context.execute() here emit the given string to the
script output.

def run_migrations_online() -> None:
"""
settings = Inject(get_settings)
settings.check_db()
tcompa marked this conversation as resolved.
Show resolved Hide resolved
context.configure(
url=settings.DATABASE_ASYNC_URL,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
tcompa marked this conversation as resolved.
Show resolved Hide resolved
render_as_batch=True,
)

with context.begin_transaction():
context.run_migrations()


def do_run_migrations(connection: Connection) -> None:
context.configure(
connection=connection,
target_metadata=target_metadata,
render_as_batch=True,
)

with context.begin_transaction():
context.run_migrations()


async def run_migrations_online() -> None:
"""Run migrations in 'online' mode.
Run migrations in 'online' mode.

In this scenario we need to create an Engine
and associate a connection with the context.

"""
from fractal_server.app.db import DB

engine = DB.engine_async()
async with engine.connect() as connection:
await connection.run_sync(do_run_migrations)
engine = DB.engine_sync()
with engine.connect() as connection:
context.configure(
connection=connection,
target_metadata=target_metadata,
render_as_batch=True,
)

with context.begin_transaction():
context.run_migrations()

await engine.dispose()
engine.dispose()
tcompa marked this conversation as resolved.
Show resolved Hide resolved


if context.is_offline_mode():
run_migrations_offline()
else:
asyncio.run(run_migrations_online())
run_migrations_online()