mirror of
https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git
synced 2025-08-14 00:25:46 +02:00
миграции
This commit is contained in:
@@ -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;")
|
@@ -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')
|
@@ -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_tokens_token'), 'tokens', ['token'], unique=True)
|
||||
op.create_index(op.f('ix_tokens_user_id'), 'tokens', ['user_id'], unique=False)
|
||||
|
||||
# Создаем индексы
|
||||
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)
|
||||
|
||||
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')
|
@@ -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()
|
||||
|
||||
# Импортируем все модели, чтобы Alembic мог их обнаружить
|
||||
__all__ = ["Base", "Employee", "Request", "Token"]
|
Reference in New Issue
Block a user