From 6dceccc2a9b8484a084cc6ec8d9963f9501aeabe Mon Sep 17 00:00:00 2001 From: MoonTestUse1 Date: Sat, 4 Jan 2025 03:49:39 +0600 Subject: [PATCH] =?UTF-8?q?=D0=BC=D0=B8=D0=B3=D1=80=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/alembic/versions/add_admin_user.py | 27 -------- backend/alembic/versions/add_created_at.py | 24 ------- ...initial_schema.py => initial_migration.py} | 67 +++++++++---------- backend/app/db/base.py | 9 ++- 4 files changed, 41 insertions(+), 86 deletions(-) delete mode 100644 backend/alembic/versions/add_admin_user.py delete mode 100644 backend/alembic/versions/add_created_at.py rename backend/alembic/versions/{initial_schema.py => initial_migration.py} (58%) diff --git a/backend/alembic/versions/add_admin_user.py b/backend/alembic/versions/add_admin_user.py deleted file mode 100644 index edd1c7f..0000000 --- a/backend/alembic/versions/add_admin_user.py +++ /dev/null @@ -1,27 +0,0 @@ -"""add admin user - -Revision ID: add_admin_user -Revises: initial_schema -Create Date: 2024-01-03 21:00:00.000000 - -""" -from alembic import op -import sqlalchemy as sa -from datetime import datetime - -# revision identifiers, used by Alembic. -revision = 'add_admin_user' -down_revision = 'initial_schema' -branch_labels = None -depends_on = None - -def upgrade() -> None: - # Добавляем админа с id = -1 - op.execute(""" - INSERT INTO employees (id, first_name, last_name, department, office, hashed_password) - VALUES (-1, 'Admin', 'Admin', 'IT', 'HQ', '$2b$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewKyNiAYEPFr.6Ja') - ON CONFLICT (id) DO NOTHING; - """) - -def downgrade() -> None: - op.execute("DELETE FROM employees WHERE id = -1;") \ No newline at end of file diff --git a/backend/alembic/versions/add_created_at.py b/backend/alembic/versions/add_created_at.py deleted file mode 100644 index 147e287..0000000 --- a/backend/alembic/versions/add_created_at.py +++ /dev/null @@ -1,24 +0,0 @@ -"""add created_at column - -Revision ID: add_created_at -Revises: add_admin_user -Create Date: 2024-01-03 21:30:00.000000 - -""" -from alembic import op -import sqlalchemy as sa - -# revision identifiers, used by Alembic. -revision = 'add_created_at' -down_revision = 'add_admin_user' -branch_labels = None -depends_on = None - -def upgrade() -> None: - # Добавляем колонку created_at - op.add_column('employees', - sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False) - ) - -def downgrade() -> None: - op.drop_column('employees', 'created_at') \ No newline at end of file diff --git a/backend/alembic/versions/initial_schema.py b/backend/alembic/versions/initial_migration.py similarity index 58% rename from backend/alembic/versions/initial_schema.py rename to backend/alembic/versions/initial_migration.py index cde47f5..2f546cd 100644 --- a/backend/alembic/versions/initial_schema.py +++ b/backend/alembic/versions/initial_migration.py @@ -1,20 +1,20 @@ -"""initial schema +"""initial migration -Revision ID: initial_schema +Revision ID: initial_migration Revises: -Create Date: 2024-01-03 20:45:00.000000 +Create Date: 2024-01-03 22:30:00.000000 """ from alembic import op import sqlalchemy as sa # revision identifiers, used by Alembic. -revision = 'initial_schema' +revision = 'initial_migration' down_revision = None branch_labels = None depends_on = None -def upgrade() -> None: +def upgrade(): # Создаем таблицу employees op.create_table( 'employees', @@ -24,53 +24,52 @@ def upgrade() -> None: sa.Column('department', sa.String(), nullable=False), sa.Column('office', sa.String(), nullable=False), sa.Column('hashed_password', sa.String(), nullable=False), - sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), + sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()')), sa.PrimaryKeyConstraint('id') ) - + op.create_index(op.f('ix_employees_id'), 'employees', ['id'], unique=False) + # Создаем таблицу requests op.create_table( 'requests', sa.Column('id', sa.Integer(), nullable=False), - sa.Column('title', sa.String(), nullable=False), + sa.Column('department', sa.String(), nullable=False), + sa.Column('request_type', sa.String(), nullable=False), sa.Column('description', sa.String(), nullable=False), - sa.Column('status', sa.String(), nullable=False), sa.Column('priority', sa.String(), nullable=False), + sa.Column('status', sa.String(), nullable=False), sa.Column('employee_id', sa.Integer(), nullable=False), - sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), - sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True), - sa.ForeignKeyConstraint(['employee_id'], ['employees.id'], ondelete='CASCADE'), + sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()')), + sa.ForeignKeyConstraint(['employee_id'], ['employees.id'], ), sa.PrimaryKeyConstraint('id') ) - + op.create_index(op.f('ix_requests_id'), 'requests', ['id'], unique=False) + op.create_index(op.f('ix_requests_department'), 'requests', ['department'], unique=False) + op.create_index(op.f('ix_requests_request_type'), 'requests', ['request_type'], unique=False) + op.create_index(op.f('ix_requests_status'), 'requests', ['status'], unique=False) + # Создаем таблицу tokens op.create_table( 'tokens', sa.Column('id', sa.Integer(), nullable=False), - sa.Column('access_token', sa.String(), nullable=False), - sa.Column('employee_id', sa.Integer(), nullable=False), - sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), - sa.Column('expires_at', sa.DateTime(timezone=True), nullable=False), - sa.ForeignKeyConstraint(['employee_id'], ['employees.id'], ondelete='CASCADE'), + sa.Column('token', sa.String(), nullable=False), + sa.Column('user_id', sa.Integer(), nullable=False), + sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()')), sa.PrimaryKeyConstraint('id') ) - - # Создаем индексы - op.create_index(op.f('ix_employees_id'), 'employees', ['id'], unique=False) - op.create_index(op.f('ix_employees_last_name'), 'employees', ['last_name'], unique=False) - op.create_index(op.f('ix_requests_id'), 'requests', ['id'], unique=False) - op.create_index(op.f('ix_tokens_access_token'), 'tokens', ['access_token'], unique=True) - op.create_index(op.f('ix_tokens_id'), 'tokens', ['id'], unique=False) + op.create_index(op.f('ix_tokens_token'), 'tokens', ['token'], unique=True) + op.create_index(op.f('ix_tokens_user_id'), 'tokens', ['user_id'], unique=False) -def downgrade() -> None: - # Удаляем индексы - op.drop_index(op.f('ix_tokens_id'), table_name='tokens') - op.drop_index(op.f('ix_tokens_access_token'), table_name='tokens') - op.drop_index(op.f('ix_requests_id'), table_name='requests') - op.drop_index(op.f('ix_employees_last_name'), table_name='employees') - op.drop_index(op.f('ix_employees_id'), table_name='employees') - - # Удаляем таблицы +def downgrade(): + op.drop_index(op.f('ix_tokens_user_id'), table_name='tokens') + op.drop_index(op.f('ix_tokens_token'), table_name='tokens') op.drop_table('tokens') + + op.drop_index(op.f('ix_requests_status'), table_name='requests') + op.drop_index(op.f('ix_requests_request_type'), table_name='requests') + op.drop_index(op.f('ix_requests_department'), table_name='requests') + op.drop_index(op.f('ix_requests_id'), table_name='requests') op.drop_table('requests') + + op.drop_index(op.f('ix_employees_id'), table_name='employees') op.drop_table('employees') \ No newline at end of file diff --git a/backend/app/db/base.py b/backend/app/db/base.py index ea230e7..b7016cb 100644 --- a/backend/app/db/base.py +++ b/backend/app/db/base.py @@ -1,4 +1,11 @@ """Base class for SQLAlchemy models""" from sqlalchemy.orm import declarative_base +from app.db.base_class import Base +from app.models.employee import Employee +from app.models.request import Request +from app.models.token import Token -Base = declarative_base() \ No newline at end of file +Base = declarative_base() + +# Импортируем все модели, чтобы Alembic мог их обнаружить +__all__ = ["Base", "Employee", "Request", "Token"] \ No newline at end of file