From 979f27bfd06ec926b01fe2d0b56ee782c4095073 Mon Sep 17 00:00:00 2001 From: Dyakov Roman Date: Mon, 20 Mar 2023 18:11:46 +0300 Subject: [PATCH] Fix SQLAlchemy 2.0 --- calendar_backend/models/db.py | 78 ++++++++++---------- migrations/versions/fe04c8baa5ab_fix_sa20.py | 60 +++++++++++++++ 2 files changed, 99 insertions(+), 39 deletions(-) create mode 100644 migrations/versions/fe04c8baa5ab_fix_sa20.py diff --git a/calendar_backend/models/db.py b/calendar_backend/models/db.py index c31cb836..2fdf4362 100644 --- a/calendar_backend/models/db.py +++ b/calendar_backend/models/db.py @@ -18,12 +18,12 @@ class Credentials(BaseDbModel): """User credentials""" id: Mapped[int] = mapped_column(Integer, primary_key=True) - group: Mapped[int] = mapped_column(String, nullable=False) - email: Mapped[int] = mapped_column(String, nullable=False) - scope: Mapped[int] = mapped_column(JSON, nullable=False) - token: Mapped[int] = mapped_column(JSON, nullable=False) - create_ts: Mapped[int] = mapped_column(DateTime, nullable=False, default=datetime.utcnow) - update_ts: Mapped[int] = mapped_column(DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow) + group: Mapped[str] = mapped_column(String, nullable=False) + email: Mapped[str] = mapped_column(String, nullable=False) + scope: Mapped[JSON] = mapped_column(JSON, nullable=False) + token: Mapped[JSON] = mapped_column(JSON, nullable=False) + create_ts: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow) + update_ts: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow) class Direction(str, Enum): @@ -36,7 +36,7 @@ class Room(BaseDbModel): direction: Mapped[Direction] = mapped_column(DbEnum(Direction, native_enum=False), nullable=True) building: Mapped[str] = mapped_column(String, nullable=True) building_url: Mapped[str] = mapped_column(String, nullable=True) - is_deleted: Mapped[bool] = mapped_column(Boolean, default=False) + is_deleted: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False) events: Mapped[list[Event]] = relationship( "Event", @@ -51,9 +51,9 @@ class Lecturer(BaseDbModel): first_name: Mapped[str] = mapped_column(String, nullable=False) middle_name: Mapped[str] = mapped_column(String, nullable=False) last_name: Mapped[str] = mapped_column(String, nullable=False) - avatar_id: Mapped[int] = mapped_column(Integer, ForeignKey("photo.id")) + avatar_id: Mapped[int] = mapped_column(Integer, ForeignKey("photo.id"), nullable=True) description: Mapped[str] = mapped_column(Text, nullable=True) - is_deleted: Mapped[bool] = mapped_column(Boolean, default=False) + is_deleted: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False) avatar: Mapped[Photo] = relationship( "Photo", @@ -98,9 +98,9 @@ def last_photo(self) -> Photo | None: class Group(BaseDbModel): - name: Mapped[int] = mapped_column(String, nullable=False) - number: Mapped[int] = mapped_column(String, nullable=False, unique=True) - is_deleted: Mapped[int] = mapped_column(Boolean, default=False) + name: Mapped[str] = mapped_column(String, nullable=False) + number: Mapped[str] = mapped_column(String, nullable=False, unique=True) + is_deleted: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False) events: Mapped[list[Event]] = relationship( "Event", @@ -111,11 +111,11 @@ class Group(BaseDbModel): class Event(BaseDbModel): - name: Mapped[int] = mapped_column(String, nullable=False) - group_id: Mapped[int] = mapped_column(Integer, ForeignKey("group.id")) - start_ts: Mapped[int] = mapped_column(DateTime, nullable=False) - end_ts: Mapped[int] = mapped_column(DateTime, nullable=False) - is_deleted: Mapped[int] = mapped_column(Boolean, default=False) + name: Mapped[str] = mapped_column(String, nullable=False) + group_id: Mapped[int] = mapped_column(Integer, ForeignKey("group.id"), nullable=True) + start_ts: Mapped[datetime] = mapped_column(DateTime, nullable=False) + end_ts: Mapped[datetime] = mapped_column(DateTime, nullable=False) + is_deleted: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False) room: Mapped[list[Room]] = relationship( "Room", @@ -144,20 +144,20 @@ class Event(BaseDbModel): class EventsLecturers(BaseDbModel): - event_id: Mapped[int] = mapped_column(Integer, ForeignKey("event.id")) - lecturer_id: Mapped[int] = mapped_column(Integer, ForeignKey("lecturer.id")) + event_id: Mapped[int] = mapped_column(Integer, ForeignKey("event.id"), nullable=False) + lecturer_id: Mapped[int] = mapped_column(Integer, ForeignKey("lecturer.id"), nullable=False) class EventsRooms(BaseDbModel): - event_id: Mapped[int] = mapped_column(Integer, ForeignKey("event.id")) - room_id: Mapped[int] = mapped_column(Integer, ForeignKey("room.id")) + event_id: Mapped[int] = mapped_column(Integer, ForeignKey("event.id"), nullable=False) + room_id: Mapped[int] = mapped_column(Integer, ForeignKey("room.id"), nullable=False) class Photo(BaseDbModel): - lecturer_id: Mapped[int] = mapped_column(Integer, ForeignKey("lecturer.id")) - link: Mapped[int] = mapped_column(String, unique=True) - approve_status: Mapped[int] = mapped_column(DbEnum(ApproveStatuses, native_enum=False), nullable=False) - is_deleted: Mapped[int] = mapped_column(Boolean, default=False) + lecturer_id: Mapped[int] = mapped_column(Integer, ForeignKey("lecturer.id"), nullable=False) + link: Mapped[str] = mapped_column(String, unique=True, nullable=False) + approve_status: Mapped[ApproveStatuses] = mapped_column(DbEnum(ApproveStatuses, native_enum=False), nullable=False) + is_deleted: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False) lecturer: Mapped[Lecturer] = relationship( "Lecturer", @@ -169,13 +169,13 @@ class Photo(BaseDbModel): class CommentLecturer(BaseDbModel): - lecturer_id: Mapped[int] = mapped_column(Integer, ForeignKey("lecturer.id")) - author_name: Mapped[int] = mapped_column(String, nullable=False) - text: Mapped[int] = mapped_column(String, nullable=False) - approve_status: Mapped[int] = mapped_column(DbEnum(ApproveStatuses, native_enum=False), nullable=False) - create_ts: Mapped[int] = mapped_column(DateTime, default=datetime.utcnow()) - update_ts: Mapped[int] = mapped_column(DateTime, default=datetime.utcnow(), onupdate=datetime.utcnow()) - is_deleted: Mapped[int] = mapped_column(Boolean, default=False) + lecturer_id: Mapped[int] = mapped_column(Integer, ForeignKey("lecturer.id"), nullable=False) + author_name: Mapped[str] = mapped_column(String, nullable=False) + text: Mapped[str] = mapped_column(String, nullable=False) + approve_status: Mapped[ApproveStatuses] = mapped_column(DbEnum(ApproveStatuses, native_enum=False), nullable=False) + create_ts: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow()) + update_ts: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow(), onupdate=datetime.utcnow()) + is_deleted: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False) lecturer: Mapped[Lecturer] = relationship( "Lecturer", @@ -186,13 +186,13 @@ class CommentLecturer(BaseDbModel): class CommentEvent(BaseDbModel): - event_id: Mapped[int] = mapped_column(Integer, ForeignKey("event.id")) - author_name: Mapped[int] = mapped_column(String, nullable=False) - text: Mapped[int] = mapped_column(String, nullable=False) - approve_status: Mapped[int] = mapped_column(DbEnum(ApproveStatuses, native_enum=False), nullable=False) - create_ts: Mapped[int] = mapped_column(DateTime, default=datetime.utcnow()) - update_ts: Mapped[int] = mapped_column(DateTime, default=datetime.utcnow(), onupdate=datetime.utcnow()) - is_deleted: Mapped[int] = mapped_column(Boolean, default=False) + event_id: Mapped[int] = mapped_column(Integer, ForeignKey("event.id"), nullable=False) + author_name: Mapped[str] = mapped_column(String, nullable=False) + text: Mapped[str] = mapped_column(String, nullable=False) + approve_status: Mapped[ApproveStatuses] = mapped_column(DbEnum(ApproveStatuses, native_enum=False), nullable=False) + create_ts: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow()) + update_ts: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow(), onupdate=datetime.utcnow()) + is_deleted: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False) event: Mapped[Event] = relationship( "Event", diff --git a/migrations/versions/fe04c8baa5ab_fix_sa20.py b/migrations/versions/fe04c8baa5ab_fix_sa20.py new file mode 100644 index 00000000..08fb663c --- /dev/null +++ b/migrations/versions/fe04c8baa5ab_fix_sa20.py @@ -0,0 +1,60 @@ +"""Fix sa20 + +Revision ID: fe04c8baa5ab +Revises: 3948c45f9977 +Create Date: 2023-03-20 18:10:29.098467 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision = 'fe04c8baa5ab' +down_revision = '3948c45f9977' +branch_labels = None +depends_on = None + + +def upgrade(): + op.alter_column('comment_event', 'event_id', existing_type=sa.INTEGER(), nullable=False) + op.alter_column('comment_event', 'create_ts', existing_type=postgresql.TIMESTAMP(), nullable=False) + op.alter_column('comment_event', 'update_ts', existing_type=postgresql.TIMESTAMP(), nullable=False) + op.alter_column('comment_event', 'is_deleted', existing_type=sa.BOOLEAN(), nullable=False) + op.alter_column('comment_lecturer', 'lecturer_id', existing_type=sa.INTEGER(), nullable=False) + op.alter_column('comment_lecturer', 'create_ts', existing_type=postgresql.TIMESTAMP(), nullable=False) + op.alter_column('comment_lecturer', 'update_ts', existing_type=postgresql.TIMESTAMP(), nullable=False) + op.alter_column('comment_lecturer', 'is_deleted', existing_type=sa.BOOLEAN(), nullable=False) + op.alter_column('event', 'is_deleted', existing_type=sa.BOOLEAN(), nullable=False) + op.alter_column('events_lecturers', 'event_id', existing_type=sa.INTEGER(), nullable=False) + op.alter_column('events_lecturers', 'lecturer_id', existing_type=sa.INTEGER(), nullable=False) + op.alter_column('events_rooms', 'event_id', existing_type=sa.INTEGER(), nullable=False) + op.alter_column('events_rooms', 'room_id', existing_type=sa.INTEGER(), nullable=False) + op.alter_column('group', 'is_deleted', existing_type=sa.BOOLEAN(), nullable=False) + op.alter_column('lecturer', 'is_deleted', existing_type=sa.BOOLEAN(), nullable=False) + op.alter_column('photo', 'lecturer_id', existing_type=sa.INTEGER(), nullable=False) + op.alter_column('photo', 'link', existing_type=sa.VARCHAR(), nullable=False) + op.alter_column('photo', 'is_deleted', existing_type=sa.BOOLEAN(), nullable=False) + op.alter_column('room', 'is_deleted', existing_type=sa.BOOLEAN(), nullable=False) + + +def downgrade(): + op.alter_column('room', 'is_deleted', existing_type=sa.BOOLEAN(), nullable=True) + op.alter_column('photo', 'is_deleted', existing_type=sa.BOOLEAN(), nullable=True) + op.alter_column('photo', 'link', existing_type=sa.VARCHAR(), nullable=True) + op.alter_column('photo', 'lecturer_id', existing_type=sa.INTEGER(), nullable=True) + op.alter_column('lecturer', 'is_deleted', existing_type=sa.BOOLEAN(), nullable=True) + op.alter_column('group', 'is_deleted', existing_type=sa.BOOLEAN(), nullable=True) + op.alter_column('events_rooms', 'room_id', existing_type=sa.INTEGER(), nullable=True) + op.alter_column('events_rooms', 'event_id', existing_type=sa.INTEGER(), nullable=True) + op.alter_column('events_lecturers', 'lecturer_id', existing_type=sa.INTEGER(), nullable=True) + op.alter_column('events_lecturers', 'event_id', existing_type=sa.INTEGER(), nullable=True) + op.alter_column('event', 'is_deleted', existing_type=sa.BOOLEAN(), nullable=True) + op.alter_column('comment_lecturer', 'is_deleted', existing_type=sa.BOOLEAN(), nullable=True) + op.alter_column('comment_lecturer', 'update_ts', existing_type=postgresql.TIMESTAMP(), nullable=True) + op.alter_column('comment_lecturer', 'create_ts', existing_type=postgresql.TIMESTAMP(), nullable=True) + op.alter_column('comment_lecturer', 'lecturer_id', existing_type=sa.INTEGER(), nullable=True) + op.alter_column('comment_event', 'is_deleted', existing_type=sa.BOOLEAN(), nullable=True) + op.alter_column('comment_event', 'update_ts', existing_type=postgresql.TIMESTAMP(), nullable=True) + op.alter_column('comment_event', 'create_ts', existing_type=postgresql.TIMESTAMP(), nullable=True) + op.alter_column('comment_event', 'event_id', existing_type=sa.INTEGER(), nullable=True)