From 0387ff60672c7423db049c1e5a168ead5d240f8f Mon Sep 17 00:00:00 2001 From: Federico Stagni Date: Thu, 18 Dec 2025 16:17:49 +0100 Subject: [PATCH] fix: added examples for table extensions and fields mods --- docs/dev/explanations/extensions.md | 7 ++++--- .../src/gubbins/db/sql/jobs/schema.py | 21 ++++++++++++++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/docs/dev/explanations/extensions.md b/docs/dev/explanations/extensions.md index be5c86647..e0e2bcf35 100644 --- a/docs/dev/explanations/extensions.md +++ b/docs/dev/explanations/extensions.md @@ -78,13 +78,14 @@ To develop, follow the same [instruction as `diracx`](../tutorials/getting-start The `gubbins-db` package contains the extension for the DB. -### New DB +### New SQL DB `lollygagDB` is a DB which is specific to `gubbins`, i.e. it does not modify or extend an existing `diracx` db -### Extended DB +### Extended SQL DB -`GubbinsJobDB` illustrates how to extend an existing `diracx` DB, add new methods, modify methods, add a table. +`GubbinsJobDB` illustrates how to extend an existing `diracx` SQL DB, add new methods, modify methods, add a table, adding a column to one table. +Modifying an existing column is instead not supported. A router test exists (`test_gubbins_job_manager.py`), even though no router is redefined. It is just to show that the correct DB is being loaded. diff --git a/extensions/gubbins/gubbins-db/src/gubbins/db/sql/jobs/schema.py b/extensions/gubbins/gubbins-db/src/gubbins/db/sql/jobs/schema.py index 8fabb17f4..aef66d2d5 100644 --- a/extensions/gubbins/gubbins-db/src/gubbins/db/sql/jobs/schema.py +++ b/extensions/gubbins/gubbins-db/src/gubbins/db/sql/jobs/schema.py @@ -1,14 +1,29 @@ from diracx.db.sql.job.db import JobDBBase -from diracx.db.sql.job.schema import str255 +from diracx.db.sql.job.schema import InputData, str255 from sqlalchemy import ForeignKey from sqlalchemy.orm import Mapped, mapped_column -# You need to inherit from the DeclarativeBase of the parent DB +# GubbinsInputData: Extends InputData by ADDING a column +# NOTE: Simple inheritance works here because we're ADDING a new column, +# not modifying an existing one. This is the preferred approach when possible. +class GubbinsInputData(InputData): + """Extended InputData table with Adler checksum support""" + + __tablename__ = "InputData" + __table_args__ = {"extend_existing": True} + + # New column for Adler checksum + adler_checksum: Mapped[str255] = mapped_column("AdlerChecksum", default="") + + +# Example of defining a new table +# NOTE: You need to inherit from the DeclarativeBase of the parent DB class GubbinsInfo(JobDBBase): """An extra table with respect to Vanilla diracx JobDB""" - __tablename__ = "GubbinsJobs" + __tablename__ = "GubbinsInfo" + __table_args__ = {"extend_existing": True} job_id: Mapped[int] = mapped_column( "JobID", ForeignKey("Jobs.JobID", ondelete="CASCADE"), primary_key=True