From 83fd3073ec700b480974d68d15831a7bf905e753 Mon Sep 17 00:00:00 2001 From: Matt Gerega Date: Tue, 26 Mar 2024 14:02:17 -0400 Subject: [PATCH] Fixed formatting --- ...4_03_26_0948-aafcd7fb0e84_spool_weights.py | 23 ++++++--- ...49-304a32906234_spool_weight_population.py | 48 ++++++++++++------- spoolman/database/spool.py | 8 +--- 3 files changed, 51 insertions(+), 28 deletions(-) diff --git a/migrations/versions/2024_03_26_0948-aafcd7fb0e84_spool_weights.py b/migrations/versions/2024_03_26_0948-aafcd7fb0e84_spool_weights.py index 0d834be..ec14929 100644 --- a/migrations/versions/2024_03_26_0948-aafcd7fb0e84_spool_weights.py +++ b/migrations/versions/2024_03_26_0948-aafcd7fb0e84_spool_weights.py @@ -8,8 +8,8 @@ import sqlalchemy as sa from alembic import op # revision identifiers, used by Alembic. -revision = 'aafcd7fb0e84' -down_revision = 'b8881bdb716c' +revision = "aafcd7fb0e84" +down_revision = "b8881bdb716c" branch_labels = None depends_on = None @@ -17,14 +17,25 @@ depends_on = None def upgrade() -> None: """Perform the upgrade.""" # ### commands auto generated by Alembic - please adjust! ### - op.add_column('spool', sa.Column('initial_weight', sa.Float(), nullable=True, comment="The initial total weight of the spool (gross weight).")) - op.add_column('spool', sa.Column('empty_weight', sa.Float(), nullable=True, comment="The weight of the empty spool (tare weight).")) + op.add_column( + "spool", + sa.Column( + "initial_weight", + sa.Float(), + nullable=True, + comment="The initial total weight of the spool (gross weight).", + ), + ) + op.add_column( + "spool", + sa.Column("empty_weight", sa.Float(), nullable=True, comment="The weight of the empty spool (tare weight)."), + ) # ### end Alembic commands ### def downgrade() -> None: """Perform the downgrade.""" # ### commands auto generated by Alembic - please adjust! ### - op.drop_column('spool', 'empty_weight') - op.drop_column('spool', 'initial_weight') + op.drop_column("spool", "empty_weight") + op.drop_column("spool", "initial_weight") # ### end Alembic commands ### diff --git a/migrations/versions/2024_03_26_1349-304a32906234_spool_weight_population.py b/migrations/versions/2024_03_26_1349-304a32906234_spool_weight_population.py index 73861be..fc30c42 100644 --- a/migrations/versions/2024_03_26_1349-304a32906234_spool_weight_population.py +++ b/migrations/versions/2024_03_26_1349-304a32906234_spool_weight_population.py @@ -8,29 +8,46 @@ import sqlalchemy as sa from alembic import op # revision identifiers, used by Alembic. -revision = '304a32906234' -down_revision = 'aafcd7fb0e84' +revision = "304a32906234" +down_revision = "aafcd7fb0e84" branch_labels = None depends_on = None def upgrade() -> None: - filament = sa.Table('filament', + """Pre-populate the spool weights.""" + """This must be done in a separate migration because""" + """of cockroachdb's execution of alembic migrations""" + filament = sa.Table( + "filament", sa.MetaData(), - sa.Column('id', sa.Integer, primary_key=True), - sa.Column('weight', sa.Float(), nullable=True), - sa.Column('spool_weight', sa.Float(), nullable=True)) - + sa.Column("id", sa.Integer, primary_key=True), + sa.Column("weight", sa.Float(), nullable=True), + sa.Column("spool_weight", sa.Float(), nullable=True), + ) + spool = sa.Table( - 'spool', + "spool", sa.MetaData(), - sa.Column('id', sa.Integer, primary_key=True), - sa.Column('filament_id', sa.Integer), - sa.Column('initial_weight', sa.Float(), nullable=False, comment="The initial total weight of the spool (gross weight)."), - sa.Column('empty_weight', sa.Float(), nullable=False, comment="The weight of the empty spool (tare weight).") - ) - - initial_weight = sa.select((filament.c.weight + filament.c.spool_weight).label("initial_weight")).where(filament.c.id == spool.c.filament_id).scalar_subquery() + sa.Column("id", sa.Integer, primary_key=True), + sa.Column("filament_id", sa.Integer), + sa.Column( + "initial_weight", + sa.Float(), + nullable=False, + ), + sa.Column( + "empty_weight", + sa.Float(), + nullable=False, + ), + ) + + initial_weight = ( + sa.select((filament.c.weight + filament.c.spool_weight).label("initial_weight")) + .where(filament.c.id == spool.c.filament_id) + .scalar_subquery() + ) empty_weight = sa.select(filament.c.spool_weight).where(filament.c.id == spool.c.filament_id).scalar_subquery() set_initial_weight = sa.update(spool).values(initial_weight=initial_weight) @@ -43,4 +60,3 @@ def upgrade() -> None: def downgrade() -> None: """Perform the downgrade.""" - pass diff --git a/spoolman/database/spool.py b/spoolman/database/spool.py index ca384d8..7da98ec 100644 --- a/spoolman/database/spool.py +++ b/spoolman/database/spool.py @@ -194,12 +194,8 @@ async def update( if k == "filament_id": spool.filament = await filament.get_by_id(db, v) # If there is no initial_weight, calculate it from the filament weight - if ( - spool.initial_weight is None - and spool.empty_weight is None - and spool.filament.weight is not None - ): - spool_weight = (spool.empty_weight if spool.empty_weight is not None else 0) + if spool.initial_weight is None and spool.empty_weight is None and spool.filament.weight is not None: + spool_weight = spool.empty_weight if spool.empty_weight is not None else 0 spool.initial_weight = spool.filament.weight + spool_weight spool.empty_weight = spool_weight