From c18ed4283a30b05b386cc1ed3bfa7cff91f683be Mon Sep 17 00:00:00 2001 From: Fridolin Glatter Date: Fri, 29 Nov 2024 14:16:35 +0100 Subject: [PATCH] Make scalar.unit non-optional --- ixmp4/data/db/optimization/indexset/model.py | 2 +- ixmp4/data/db/optimization/scalar/model.py | 4 +- ...a8276ba0a_make_scalar_unit_non_optional.py | 41 +++++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 ixmp4/db/migrations/versions/d66a8276ba0a_make_scalar_unit_non_optional.py diff --git a/ixmp4/data/db/optimization/indexset/model.py b/ixmp4/data/db/optimization/indexset/model.py index 40a55b7e..e33fad58 100644 --- a/ixmp4/data/db/optimization/indexset/model.py +++ b/ixmp4/data/db/optimization/indexset/model.py @@ -19,7 +19,7 @@ class IndexSet(base.BaseModel): _data_type: types.OptimizationDataType _data: types.Mapped[list["IndexSetData"]] = db.relationship( - back_populates="indexset" + back_populates="indexset", order_by="IndexSetData.id" ) @property diff --git a/ixmp4/data/db/optimization/scalar/model.py b/ixmp4/data/db/optimization/scalar/model.py index c364f807..12e7326b 100644 --- a/ixmp4/data/db/optimization/scalar/model.py +++ b/ixmp4/data/db/optimization/scalar/model.py @@ -17,8 +17,8 @@ class Scalar(base.BaseModel): value: types.Float = db.Column(db.Float, nullable=True, unique=False) - unit: types.Mapped[Unit | None] = db.relationship() - unit__id: types.Mapped[int | None] = db.Column( + unit: types.Mapped[Unit] = db.relationship() + unit__id: types.Mapped[int] = db.Column( db.Integer, db.ForeignKey("unit.id"), index=True ) diff --git a/ixmp4/db/migrations/versions/d66a8276ba0a_make_scalar_unit_non_optional.py b/ixmp4/db/migrations/versions/d66a8276ba0a_make_scalar_unit_non_optional.py new file mode 100644 index 00000000..c055928e --- /dev/null +++ b/ixmp4/db/migrations/versions/d66a8276ba0a_make_scalar_unit_non_optional.py @@ -0,0 +1,41 @@ +# type: ignore +"""Make Scalar.unit non-optional + +Revision ID: d66a8276ba0a +Revises: 914991d09f59 +Create Date: 2024-11-29 14:14:42.857695 + +""" + +import sqlalchemy as sa +from alembic import op + +# Revision identifiers, used by Alembic. +revision = "d66a8276ba0a" +down_revision = "914991d09f59" +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table("optimization_scalar", schema=None) as batch_op: + batch_op.alter_column("unit__id", existing_type=sa.INTEGER(), nullable=False) + + with op.batch_alter_table("runmetaentry", schema=None) as batch_op: + batch_op.add_column(sa.Column("dtype", sa.String(length=20), nullable=False)) + batch_op.drop_column("type") + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table("runmetaentry", schema=None) as batch_op: + batch_op.add_column(sa.Column("type", sa.VARCHAR(length=20), nullable=False)) + batch_op.drop_column("dtype") + + with op.batch_alter_table("optimization_scalar", schema=None) as batch_op: + batch_op.alter_column("unit__id", existing_type=sa.INTEGER(), nullable=True) + + # ### end Alembic commands ###