mirror of
https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git
synced 2025-08-14 00:25:46 +02:00
Интеграция базы данных Postgresql v2
This commit is contained in:
37
backend/alembic.ini
Normal file
37
backend/alembic.ini
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
[alembic]
|
||||||
|
script_location = alembic
|
||||||
|
sqlalchemy.url = postgresql://support_user:support_password@localhost:5432/support_db
|
||||||
|
|
||||||
|
[loggers]
|
||||||
|
keys = root,sqlalchemy,alembic
|
||||||
|
|
||||||
|
[handlers]
|
||||||
|
keys = console
|
||||||
|
|
||||||
|
[formatters]
|
||||||
|
keys = generic
|
||||||
|
|
||||||
|
[logger_root]
|
||||||
|
level = WARN
|
||||||
|
handlers = console
|
||||||
|
qualname =
|
||||||
|
|
||||||
|
[logger_sqlalchemy]
|
||||||
|
level = WARN
|
||||||
|
handlers =
|
||||||
|
qualname = sqlalchemy.engine
|
||||||
|
|
||||||
|
[logger_alembic]
|
||||||
|
level = INFO
|
||||||
|
handlers =
|
||||||
|
qualname = alembic
|
||||||
|
|
||||||
|
[handler_console]
|
||||||
|
class = StreamHandler
|
||||||
|
args = (sys.stderr,)
|
||||||
|
level = NOTSET
|
||||||
|
formatter = generic
|
||||||
|
|
||||||
|
[formatter_generic]
|
||||||
|
format = %(levelname)-5.5s [%(name)s] %(message)s
|
||||||
|
datefmt = %H:%M:%S
|
61
backend/alembic/env.py
Normal file
61
backend/alembic/env.py
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
from logging.config import fileConfig
|
||||||
|
from sqlalchemy import engine_from_config
|
||||||
|
from sqlalchemy import pool
|
||||||
|
from alembic import context
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
# Add the parent directory to sys.path
|
||||||
|
sys.path.append(str(Path(__file__).resolve().parents[1]))
|
||||||
|
|
||||||
|
# Import your models
|
||||||
|
from app.schemas import tables
|
||||||
|
from app.database import Base
|
||||||
|
|
||||||
|
# this is the Alembic Config object
|
||||||
|
config = context.config
|
||||||
|
|
||||||
|
# Interpret the config file for Python logging
|
||||||
|
if config.config_file_name is not None:
|
||||||
|
fileConfig(config.config_file_name)
|
||||||
|
|
||||||
|
target_metadata = Base.metadata
|
||||||
|
|
||||||
|
def get_url():
|
||||||
|
return os.getenv("DATABASE_URL", "postgresql://support_user:support_password@localhost:5432/support_db")
|
||||||
|
|
||||||
|
def run_migrations_offline() -> None:
|
||||||
|
url = get_url()
|
||||||
|
context.configure(
|
||||||
|
url=url,
|
||||||
|
target_metadata=target_metadata,
|
||||||
|
literal_binds=True,
|
||||||
|
dialect_opts={"paramstyle": "named"},
|
||||||
|
)
|
||||||
|
|
||||||
|
with context.begin_transaction():
|
||||||
|
context.run_migrations()
|
||||||
|
|
||||||
|
def run_migrations_online() -> None:
|
||||||
|
configuration = config.get_section(config.config_ini_section)
|
||||||
|
configuration["sqlalchemy.url"] = get_url()
|
||||||
|
connectable = engine_from_config(
|
||||||
|
configuration,
|
||||||
|
prefix="sqlalchemy.",
|
||||||
|
poolclass=pool.NullPool,
|
||||||
|
)
|
||||||
|
|
||||||
|
with connectable.connect() as connection:
|
||||||
|
context.configure(
|
||||||
|
connection=connection,
|
||||||
|
target_metadata=target_metadata
|
||||||
|
)
|
||||||
|
|
||||||
|
with context.begin_transaction():
|
||||||
|
context.run_migrations()
|
||||||
|
|
||||||
|
if context.is_offline_mode():
|
||||||
|
run_migrations_offline()
|
||||||
|
else:
|
||||||
|
run_migrations_online()
|
50
backend/alembic/versions/initial_migration.py
Normal file
50
backend/alembic/versions/initial_migration.py
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
"""Initial migration
|
||||||
|
|
||||||
|
Revision ID: initial_migration
|
||||||
|
Create Date: 2024-03-14 12:00:00.000000
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from app.models.request import RequestStatus, RequestPriority
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = 'initial_migration'
|
||||||
|
down_revision = None
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
def upgrade() -> None:
|
||||||
|
# Create employees table
|
||||||
|
op.create_table(
|
||||||
|
'employees',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('first_name', sa.String(), nullable=False),
|
||||||
|
sa.Column('last_name', sa.String(), nullable=False),
|
||||||
|
sa.Column('department', sa.String(), nullable=False),
|
||||||
|
sa.Column('office', sa.String(), nullable=False),
|
||||||
|
sa.Column('password', sa.String(), 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)
|
||||||
|
|
||||||
|
# Create requests table
|
||||||
|
op.create_table(
|
||||||
|
'requests',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('employee_id', sa.Integer(), nullable=True),
|
||||||
|
sa.Column('department', sa.String(), nullable=False),
|
||||||
|
sa.Column('request_type', sa.String(), nullable=False),
|
||||||
|
sa.Column('priority', sa.Enum(RequestPriority), nullable=False),
|
||||||
|
sa.Column('status', sa.Enum(RequestStatus), nullable=False, server_default='new'),
|
||||||
|
sa.Column('description', sa.String(), nullable=True),
|
||||||
|
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)
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
op.drop_table('requests')
|
||||||
|
op.drop_table('employees')
|
Reference in New Issue
Block a user