-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add returned_date field in loan module (#352)
This change is required for the PR aeecleclair/Titan#310. It should be merged after #360
- Loading branch information
1 parent
0a3d4a1
commit 78196c8
Showing
5 changed files
with
61 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"""Add a returned date to the loan table | ||
Revision ID: 2fcadbe2f0ad | ||
Revises: f17e6182b0a9 | ||
Create Date: 2024-03-01 23:33:20.431056 | ||
""" | ||
|
||
from collections.abc import Sequence | ||
from datetime import UTC, datetime | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "2fcadbe2f0ad" | ||
down_revision: str | None = "3f9843f165e9" | ||
branch_labels: str | Sequence[str] | None = None | ||
depends_on: str | Sequence[str] | None = None | ||
|
||
|
||
def upgrade() -> None: | ||
# Schema migration | ||
op.add_column("loan", sa.Column("returned_date", sa.Date(), nullable=True)) | ||
|
||
# Data migration | ||
t_loan = sa.Table( | ||
"loan", | ||
sa.MetaData(), | ||
sa.Column("id", sa.String()), | ||
sa.Column("returned", sa.Boolean()), | ||
sa.Column("returned_date", sa.DateTime(timezone=True)), | ||
) | ||
|
||
conn = op.get_bind() | ||
res = conn.execute(sa.select(t_loan.c.id).where(t_loan.c.returned)).fetchall() | ||
for id_ in res: | ||
conn.execute( | ||
t_loan.update() | ||
.where(t_loan.c.id == id_) | ||
.values(returned_date=datetime.now(UTC)), | ||
) | ||
|
||
|
||
def downgrade() -> None: | ||
# Schema migration | ||
op.drop_column("loan", "returned_date") |