diff --git a/api/app/database/migrations/versions/27fa11120115_вынес_одинаковые_поля_в_базовую_модель.py b/api/app/database/migrations/versions/27fa11120115_вынес_одинаковые_поля_в_базовую_модель.py new file mode 100644 index 0000000..3cb7874 --- /dev/null +++ b/api/app/database/migrations/versions/27fa11120115_вынес_одинаковые_поля_в_базовую_модель.py @@ -0,0 +1,92 @@ +"""Вынес одинаковые поля в базовую модель + +Revision ID: 27fa11120115 +Revises: 70eb3c307702 +Create Date: 2025-02-20 09:51:06.021893 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = '27fa11120115' +down_revision: Union[str, None] = '70eb3c307702' +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('appointment_files', sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('appointment_files', sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('appointment_types', sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('appointment_types', sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('appointments', sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('appointments', sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('lens', sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('lens', sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('lens_issues', sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('lens_issues', sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('lens_types', sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('lens_types', sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('mailing', sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('mailing', sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('mailing_delivery_methods', sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('mailing_delivery_methods', sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('mailing_options', sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('mailing_options', sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('patients', sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('patients', sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('recipients', sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('recipients', sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('roles', sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('roles', sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('set_contents', sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('set_contents', sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('set_lens', sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('set_lens', sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('sets', sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('sets', sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('users', sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('users', sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('users', 'updated_at') + op.drop_column('users', 'created_at') + op.drop_column('sets', 'updated_at') + op.drop_column('sets', 'created_at') + op.drop_column('set_lens', 'updated_at') + op.drop_column('set_lens', 'created_at') + op.drop_column('set_contents', 'updated_at') + op.drop_column('set_contents', 'created_at') + op.drop_column('roles', 'updated_at') + op.drop_column('roles', 'created_at') + op.drop_column('recipients', 'updated_at') + op.drop_column('recipients', 'created_at') + op.drop_column('patients', 'updated_at') + op.drop_column('patients', 'created_at') + op.drop_column('mailing_options', 'updated_at') + op.drop_column('mailing_options', 'created_at') + op.drop_column('mailing_delivery_methods', 'updated_at') + op.drop_column('mailing_delivery_methods', 'created_at') + op.drop_column('mailing', 'updated_at') + op.drop_column('mailing', 'created_at') + op.drop_column('lens_types', 'updated_at') + op.drop_column('lens_types', 'created_at') + op.drop_column('lens_issues', 'updated_at') + op.drop_column('lens_issues', 'created_at') + op.drop_column('lens', 'updated_at') + op.drop_column('lens', 'created_at') + op.drop_column('appointments', 'updated_at') + op.drop_column('appointments', 'created_at') + op.drop_column('appointment_types', 'updated_at') + op.drop_column('appointment_types', 'created_at') + op.drop_column('appointment_files', 'updated_at') + op.drop_column('appointment_files', 'created_at') + # ### end Alembic commands ### diff --git a/api/app/domain/models/appointment_files.py b/api/app/domain/models/appointment_files.py index 8567af3..895b712 100644 --- a/api/app/domain/models/appointment_files.py +++ b/api/app/domain/models/appointment_files.py @@ -1,13 +1,12 @@ from sqlalchemy import Column, Integer, String, ForeignKey from sqlalchemy.orm import relationship -from app.domain.models import Base +from app.domain.models.base import BaseModel -class AppointmentFile(Base): +class AppointmentFile(BaseModel): __tablename__ = 'appointment_files' - id = Column(Integer, primary_key=True, autoincrement=True) file_path = Column(String, nullable=False) file_title = Column(String, nullable=False) diff --git a/api/app/domain/models/appointment_types.py b/api/app/domain/models/appointment_types.py index b7dc75f..e385ee9 100644 --- a/api/app/domain/models/appointment_types.py +++ b/api/app/domain/models/appointment_types.py @@ -1,13 +1,12 @@ from sqlalchemy import Column, Integer, VARCHAR from sqlalchemy.orm import relationship -from app.domain.models import Base +from app.domain.models.base import BaseModel -class AppointmentType(Base): +class AppointmentType(BaseModel): __tablename__ = 'appointment_types' - id = Column(Integer, primary_key=True, autoincrement=True) title = Column(VARCHAR(150), nullable=False, unique=True) appointments = relationship('Appointment', back_populates='type') diff --git a/api/app/domain/models/appointments.py b/api/app/domain/models/appointments.py index dc127e5..4e4718a 100644 --- a/api/app/domain/models/appointments.py +++ b/api/app/domain/models/appointments.py @@ -1,13 +1,12 @@ from sqlalchemy import Column, Integer, String, ForeignKey from sqlalchemy.orm import relationship -from app.domain.models import Base +from app.domain.models.base import BaseModel -class Appointment(Base): +class Appointment(BaseModel): __tablename__ = 'appointments' - id = Column(Integer, primary_key=True, autoincrement=True) results = Column(String) days_until_the_next_appointment = Column(Integer) diff --git a/api/app/domain/models/base.py b/api/app/domain/models/base.py new file mode 100644 index 0000000..9c4a4da --- /dev/null +++ b/api/app/domain/models/base.py @@ -0,0 +1,11 @@ +from sqlalchemy import Integer, Column, DateTime, func + +from app.domain.models import Base + + +class BaseModel(Base): + __abstract__ = True + + id = Column(Integer, primary_key=True, autoincrement=True) + created_at = Column(DateTime, server_default=func.now(), default=func.now, nullable=False) + updated_at = Column(DateTime, server_default=func.now(), default=func.now, onupdate=func.now, nullable=False) diff --git a/api/app/domain/models/lens.py b/api/app/domain/models/lens.py index d6a40cb..1dd5467 100644 --- a/api/app/domain/models/lens.py +++ b/api/app/domain/models/lens.py @@ -1,8 +1,9 @@ from enum import Enum as PyEnum + from sqlalchemy import Column, Integer, ForeignKey, Float, Enum, Boolean from sqlalchemy.orm import relationship -from app.domain.models import Base +from app.domain.models.base import BaseModel class SideEnum(PyEnum): @@ -10,10 +11,9 @@ class SideEnum(PyEnum): RIGHT = 'правая' -class Lens(Base): +class Lens(BaseModel): __tablename__ = 'lens' - id = Column(Integer, primary_key=True, autoincrement=True) tor = Column(Float, nullable=False) trial = Column(Float, nullable=False) esa = Column(Float, nullable=False) diff --git a/api/app/domain/models/lens_issues.py b/api/app/domain/models/lens_issues.py index 8e57f2f..b55f749 100644 --- a/api/app/domain/models/lens_issues.py +++ b/api/app/domain/models/lens_issues.py @@ -1,13 +1,12 @@ from sqlalchemy import Column, Integer, ForeignKey, Date from sqlalchemy.orm import relationship -from app.domain.models import Base +from app.domain.models.base import BaseModel -class LensIssue(Base): +class LensIssue(BaseModel): __tablename__ = 'lens_issues' - id = Column(Integer, primary_key=True, autoincrement=True) issue_date = Column(Date, nullable=False) patient_id = Column(Integer, ForeignKey('patients.id'), nullable=False) diff --git a/api/app/domain/models/lens_types.py b/api/app/domain/models/lens_types.py index b581183..b57af24 100644 --- a/api/app/domain/models/lens_types.py +++ b/api/app/domain/models/lens_types.py @@ -1,13 +1,12 @@ from sqlalchemy import Column, Integer, VARCHAR from sqlalchemy.orm import relationship -from app.domain.models import Base +from app.domain.models.base import BaseModel -class LensType(Base): +class LensType(BaseModel): __tablename__ = 'lens_types' - id = Column(Integer, primary_key=True, autoincrement=True) title = Column(VARCHAR(150), nullable=False, unique=True) lenses = relationship('Lens', back_populates='type') diff --git a/api/app/domain/models/mailing.py b/api/app/domain/models/mailing.py index f5ba465..1913348 100644 --- a/api/app/domain/models/mailing.py +++ b/api/app/domain/models/mailing.py @@ -1,14 +1,13 @@ from sqlalchemy import Column, Integer, String, DateTime, ForeignKey -from sqlalchemy.sql import func from sqlalchemy.orm import relationship +from sqlalchemy.sql import func -from app.domain.models import Base +from app.domain.models.base import BaseModel -class Mailing(Base): +class Mailing(BaseModel): __tablename__ = 'mailing' - id = Column(Integer, primary_key=True, autoincrement=True) text = Column(String, nullable=False) title = Column(String, nullable=False) datetime = Column(DateTime, nullable=False, default=func.utcnow) diff --git a/api/app/domain/models/mailing_delivery_methods.py b/api/app/domain/models/mailing_delivery_methods.py index 38dc20d..0584957 100644 --- a/api/app/domain/models/mailing_delivery_methods.py +++ b/api/app/domain/models/mailing_delivery_methods.py @@ -1,13 +1,12 @@ from sqlalchemy import Column, Integer, VARCHAR from sqlalchemy.orm import relationship -from app.domain.models import Base +from app.domain.models.base import BaseModel -class MailingDeliveryMethod(Base): +class MailingDeliveryMethod(BaseModel): __tablename__ = 'mailing_delivery_methods' - id = Column(Integer, primary_key=True, autoincrement=True) title = Column(VARCHAR(200), nullable=False) mailing = relationship('MailingOption', back_populates='method') diff --git a/api/app/domain/models/mailing_options.py b/api/app/domain/models/mailing_options.py index 17cb992..40a3a92 100644 --- a/api/app/domain/models/mailing_options.py +++ b/api/app/domain/models/mailing_options.py @@ -1,14 +1,12 @@ from sqlalchemy import Column, Integer, ForeignKey from sqlalchemy.orm import relationship -from app.domain.models import Base +from app.domain.models.base import BaseModel -class MailingOption(Base): +class MailingOption(BaseModel): __tablename__ = 'mailing_options' - id = Column(Integer, primary_key=True, autoincrement=True) - option_id = Column(Integer, ForeignKey('mailing_delivery_methods.id'), nullable=False) mailing_id = Column(Integer, ForeignKey('mailing.id'), nullable=False) diff --git a/api/app/domain/models/patients.py b/api/app/domain/models/patients.py index f133d8a..8bc1792 100644 --- a/api/app/domain/models/patients.py +++ b/api/app/domain/models/patients.py @@ -1,13 +1,12 @@ -from sqlalchemy import Column, Integer, VARCHAR, Date, String +from sqlalchemy import Column, VARCHAR, Date, String from sqlalchemy.orm import relationship -from app.domain.models import Base +from app.domain.models.base import BaseModel -class Patient(Base): +class Patient(BaseModel): __tablename__ = 'patients' - id = Column(Integer, primary_key=True, autoincrement=True) first_name = Column(VARCHAR(200), nullable=False) last_name = Column(VARCHAR(200), nullable=False) patronymic = Column(VARCHAR(200)) diff --git a/api/app/domain/models/recipients.py b/api/app/domain/models/recipients.py index 03d8a19..46fbce9 100644 --- a/api/app/domain/models/recipients.py +++ b/api/app/domain/models/recipients.py @@ -1,14 +1,12 @@ from sqlalchemy import Column, Integer, ForeignKey from sqlalchemy.orm import relationship -from app.domain.models import Base +from app.domain.models.base import BaseModel -class Recipient(Base): +class Recipient(BaseModel): __tablename__ = 'recipients' - id = Column(Integer, primary_key=True, autoincrement=True) - patient_id = Column(Integer, ForeignKey('patients.id'), nullable=False) mailing_id = Column(Integer, ForeignKey('mailing.id'), nullable=False) diff --git a/api/app/domain/models/roles.py b/api/app/domain/models/roles.py index 7404d32..98bcf91 100644 --- a/api/app/domain/models/roles.py +++ b/api/app/domain/models/roles.py @@ -1,13 +1,12 @@ from sqlalchemy import Column, Integer, VARCHAR from sqlalchemy.orm import relationship -from app.domain.models import Base +from app.domain.models.base import BaseModel -class Role(Base): +class Role(BaseModel): __tablename__ = 'roles' - id = Column(Integer, primary_key=True, autoincrement=True) title = Column(VARCHAR(150), nullable=False, unique=True) users = relationship('User', back_populates='role') diff --git a/api/app/domain/models/set_contents.py b/api/app/domain/models/set_contents.py index e4b6483..fb0bb25 100644 --- a/api/app/domain/models/set_contents.py +++ b/api/app/domain/models/set_contents.py @@ -1,13 +1,12 @@ from sqlalchemy import Column, Integer, ForeignKey from sqlalchemy.orm import relationship -from app.domain.models import Base +from app.domain.models.base import BaseModel -class SetContent(Base): +class SetContent(BaseModel): __tablename__ = 'set_contents' - id = Column(Integer, primary_key=True, autoincrement=True) tor = Column(Integer, nullable=False) trial = Column(Integer, nullable=False) esa = Column(Integer, nullable=False) diff --git a/api/app/domain/models/set_lens.py b/api/app/domain/models/set_lens.py index a5fe988..7efdc9b 100644 --- a/api/app/domain/models/set_lens.py +++ b/api/app/domain/models/set_lens.py @@ -1,14 +1,12 @@ from sqlalchemy import Column, Integer, ForeignKey from sqlalchemy.orm import relationship -from app.domain.models import Base +from app.domain.models.base import BaseModel -class SetLens(Base): +class SetLens(BaseModel): __tablename__ = 'set_lens' - id = Column(Integer, primary_key=True, autoincrement=True) - set_id = Column(Integer, ForeignKey('sets.id'), nullable=False) lens_id = Column(Integer, ForeignKey('lens.id'), nullable=False, unique=True) diff --git a/api/app/domain/models/sets.py b/api/app/domain/models/sets.py index 939e5da..d7a066b 100644 --- a/api/app/domain/models/sets.py +++ b/api/app/domain/models/sets.py @@ -1,13 +1,12 @@ from sqlalchemy import Column, Integer, VARCHAR from sqlalchemy.orm import relationship -from app.domain.models import Base +from app.domain.models.base import BaseModel -class Set(Base): +class Set(BaseModel): __tablename__ = 'sets' - id = Column(Integer, primary_key=True, autoincrement=True) title = Column(VARCHAR(150), nullable=False, unique=True) count = Column(Integer, nullable=False) diff --git a/api/app/domain/models/users.py b/api/app/domain/models/users.py index 3de7d14..ce09848 100644 --- a/api/app/domain/models/users.py +++ b/api/app/domain/models/users.py @@ -2,13 +2,12 @@ from sqlalchemy import Column, Integer, VARCHAR, ForeignKey, String from sqlalchemy.orm import relationship from werkzeug.security import check_password_hash, generate_password_hash -from app.domain.models import Base +from app.domain.models.base import BaseModel -class User(Base): +class User(BaseModel): __tablename__ = 'users' - id = Column(Integer, primary_key=True, autoincrement=True) first_name = Column(VARCHAR(200), nullable=False) last_name = Column(VARCHAR(200), nullable=False) patronymic = Column(VARCHAR(200))