Skip to content

intial schema migrations setup for sqlalchemy models - #8532

Draft
auvipy wants to merge 3 commits into
mainfrom
mig
Draft

auvipy wants to merge 3 commits into
mainfrom
mig

Conversation

@auvipy

@auvipy auvipy commented Sep 21, 2023

Copy link
Copy Markdown
Member

using alembic

@codecov

codecov Bot commented Sep 21, 2023 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.79%. Comparing base (6b1fad3) to head (eedb0fb).
⚠️ Report is 501 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #8532   +/-   ##
=======================================
  Coverage   87.78%   87.79%           
=======================================
  Files         153      153           
  Lines       19432    19432           
  Branches     2231     2231           
=======================================
+ Hits        17059    17060    +1     
  Misses       2076     2076           
+ Partials      297      296    -1     
Flag Coverage Δ
unittests 87.77% <ø> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@Nusnus

Nusnus commented Sep 21, 2023

Copy link
Copy Markdown
Member

👑

@auvipy

auvipy commented Sep 21, 2023

Copy link
Copy Markdown
Member Author

I need to get my hands dirty with this for a few more days to make it fully ready.

@auvipy auvipy mentioned this pull request Oct 30, 2023
14 of 16 tasks
@ThiefMaster

Copy link
Copy Markdown
Contributor

Is this really something that should be on the celery side?

  • If I do not use alembic and just use create_all() to create my database tables, then I just need to make my SA aware of the models (ie by calling a function from celery that creates them using my application's declarative base)
  • If I use alembic, I'm already maintaining my own revisions, so I would do the same as above, and use alembic to autogenerate the revision to create the celery-related models (if that's even needed: It's only needed when adding celery at a later stage, or when using alembic to create the initial database instead of letting SA's create_all() do this)

@auvipy

auvipy commented Nov 5, 2023

Copy link
Copy Markdown
Member Author

I will share more context here.

@auvipy

auvipy commented Nov 7, 2023

Copy link
Copy Markdown
Member Author

Is this really something that should be on the celery side?

* If I do not use alembic and just use `create_all()` to create my database tables, then I just need to make my SA aware of the models (ie by calling a function from celery that creates them using my application's declarative base)

* If I use alembic, I'm already maintaining my own revisions, so I would do the same as above, and use alembic to autogenerate the revision to create the celery-related models (if that's even needed: It's only needed when adding celery at a later stage, or when using alembic to create the initial database instead of letting SA's `create_all()` do this)

we got a regression report and had to revert #8475 (comment) so thought that adding migration is the safer bet. what do you think?

@ThiefMaster

Copy link
Copy Markdown
Contributor

I think DB changes should be clearly documented in the changelog. But executing them extremely dependent on the environment.

Note: This is assuming that you do not maintain your completely separate sqlalchemy environment. If you do, then migrations in celery make sense (because it's completely independent of the application's database usage). But if you defer this to the application's sqlalchemy environment, then IMHO what I mentioned in my previous comment applies.

@auvipy

auvipy commented Nov 7, 2023

Copy link
Copy Markdown
Member Author

is using multi db schema migrations a feasible option?

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds initial Alembic scaffolding intended to support schema migrations for Celery’s SQLAlchemy database result backend.

Changes:

  • Introduces an Alembic environment (env.py) and revision script template (script.py.mako) under celery/backends/database/migrations/.
  • Adds an Alembic configuration file at celery/backends/database/alembic.ini.
  • Adds a minimal migrations README.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
celery/backends/database/migrations/script.py.mako Adds Alembic revision script template used for generated migrations.
celery/backends/database/migrations/env.py Adds Alembic environment configuration for online/offline migrations.
celery/backends/database/migrations/README Adds placeholder README for the migrations directory.
celery/backends/database/alembic.ini Adds Alembic INI configuration pointing at the migrations directory.

@@ -0,0 +1,76 @@
from logging.config import fileConfig

from alembic import context

Copilot AI Mar 5, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

env.py imports alembic, but the repository’s sqlalchemy extra currently only depends on SQLAlchemy. If these migrations are meant for end users, Alembic needs to be declared as an optional dependency (e.g., in the SQLAlchemy extra or a new migrations extra), otherwise running the migration environment will fail with ModuleNotFoundError in a standard install.

Suggested change
from alembic import context
try:
from alembic import context
except ModuleNotFoundError as exc:
raise ModuleNotFoundError(
"Alembic is required to run the Celery database migrations. "
"Please install the 'alembic' package or the appropriate optional "
"dependency group for this project before invoking env.py."
) from exc

Copilot uses AI. Check for mistakes.
Comment on lines +3 to +16
[alembic]
# path to migration scripts
script_location = migrations

# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
# Uncomment the line below if you want the files to be prepended with date and time
# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
# for all available tokens
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s

# sys.path path, will be prepended to sys.path if present.
# defaults to the current working directory.
prepend_sys_path = .

Copilot AI Mar 5, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

script_location = migrations is interpreted relative to the current working directory unless %(here)s is used, which makes alembic -c celery/backends/database/alembic.ini ... sensitive to where it’s invoked from. Consider using script_location = %(here)s/migrations (and similarly anchoring prepend_sys_path) so the config works reliably from any CWD.

Copilot uses AI. Check for mistakes.
Comment on lines +1 to +12
"""${message}

Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
${imports if imports else ""}

Copilot AI Mar 5, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR adds non-Python migration assets (alembic.ini, migrations/script.py.mako, migrations/README). In this repo, non-.py package files are only shipped when explicitly included (e.g., via MANIFEST.in/package data). As-is, these files are likely to be missing from sdists/wheels, making migrations unusable for installed users. Please ensure these files are included in the distribution packaging.

Copilot uses AI. Check for mistakes.
@@ -0,0 +1 @@
Generic single-database configuration. No newline at end of file

Copilot AI Mar 5, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The migrations directory doesn’t include a versions/ subdirectory. Alembic typically expects migrations/versions to exist when generating/applying revisions; without it, alembic revision may fail or have nowhere to place new revisions. If this is meant to be a working init, add migrations/versions/ (with an empty .gitkeep if needed) or document the required setup steps here.

Suggested change
Generic single-database configuration.
Generic single-database Alembic configuration.
This directory is intended to host Alembic migrations for the database backend.
Alembic expects a ``versions/`` subdirectory under the migrations root where
revision files are stored. For this path, that means:
celery/backends/database/migrations/versions/
Before running ``alembic revision`` or ``alembic upgrade`` against this
environment, make sure the ``versions/`` directory exists. In a typical setup
you would either:
* Create the directory manually, optionally adding an empty placeholder file
(for example ``.gitkeep``) so it can be tracked in version control, or
* Use your Alembic configuration to initialize the environment and let it
create and manage revision files inside ``migrations/versions/``.
Adjust these steps as needed to match your project's Alembic configuration.

Copilot uses AI. Check for mistakes.
Comment on lines +15 to +20
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = None

Copilot AI Mar 5, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

target_metadata is set to None, so Alembic autogenerate won’t see Celery’s SQLAlchemy models and alembic revision --autogenerate will produce empty migrations. If the intent is to generate migrations for the result backend tables, set target_metadata to the declarative base metadata (and ensure the model modules are imported so tables are registered).

Copilot uses AI. Check for mistakes.

This branch has not been deployed

No deployments
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.

4 participants