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

DM-48385: Add Alembic integration #88

Merged
merged 14 commits into from
Jan 13, 2025
Merged

DM-48385: Add Alembic integration #88

merged 14 commits into from
Jan 13, 2025

Conversation

jonathansick
Copy link
Member

@jonathansick jonathansick commented Jan 10, 2025

This adds Alembic integration for managing Times Square's SQL database schema, using the methodology developed in Safir. This includes the initial migration, tooling to create migrations, and modifications to the FastAPI and Arq workers to check that the database is up to date. This will be released as version 0.14.0.

This Alembic integration is described in https://safir.lsst.io/user-guide/database/schema.html#set-up-alembic

- Includes Alembic configuration files, and adds those Alembic files to
  the Dockerfile to run online migrations.
- Modifies the init CLI command to ensure the database is stamped with
  the current schema
- Includes addition CLI commands for applying the alembic migration and
  for verifying that hte DB schema is current
- Updates the start up of the FastAPI service and the Arq workers to
  check that the database is current
This initial Alembic migration is created via:

  docker-compose -f docker-compose.yaml up
  tox run -e alembic -- revision --autogenerate -m "Initial schema."

While doing this we discovered some tweaks to ruff formatting:

- Adjusted template
- Added ruff configurations to pyproject.toml
Since the dockerfile "knows" where the alembic configuration is, we'll
add the env var configuration there rather than putting it as a command
line default.
jonathansick added a commit to lsst-sqre/phalanx that referenced this pull request Jan 13, 2025
This job is based on the same one developed for Gafaelfaawr and will be
part of a semi-manual process for running Alembic migrations when needed
(e.g. following
https://phalanx.lsst.io/applications/gafaelfawr/manage-schema.html#updating-the-database-schema).

See also the Times Square PR: lsst-sqre/times-square#88
It turns out stamp_database needs to run from a sync context because
it'll run its own asyncio even loop to call `run_async_migrations`.
Therefore we're following Gafaelfawr's pattern of making init sync but
then running an extracted database initialization function via
asyncio.run.
This test from
https://safir.lsst.io/user-guide/database/schema.html#testing-database-migrations
ensures that the SQLAlchemy models are consistent with the current
revision of the Alembic migrations.
- Link to deployments
- Link to documentation sites
@jonathansick jonathansick marked this pull request as ready for review January 13, 2025 22:21
@jonathansick jonathansick merged commit 8423216 into main Jan 13, 2025
4 checks passed
@jonathansick jonathansick deleted the tickets/DM-48385 branch January 13, 2025 22:22
sebastian-aranda pushed a commit to lsst-sqre/phalanx that referenced this pull request Jan 14, 2025
This job is based on the same one developed for Gafaelfaawr and will be
part of a semi-manual process for running Alembic migrations when needed
(e.g. following
https://phalanx.lsst.io/applications/gafaelfawr/manage-schema.html#updating-the-database-schema).

See also the Times Square PR: lsst-sqre/times-square#88
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant