DogAcademyGame/database/db_session.py
2024-11-18 00:49:14 +03:00

24 lines
873 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# database/db_session.py
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from config import DATABASE_URL
from database.models import Base
import os
# Создание движка SQLAlchemy
engine = create_engine(DATABASE_URL, echo=True)
# Создание фабрики сессий
Session = sessionmaker(bind=engine)
def init_db():
"""Инициализация базы данных: создание файла и таблиц."""
if not os.path.exists("database/DogAcademy.db"):
print("База данных не найдена. Создаём новую...")
Base.metadata.create_all(bind=engine)
else:
print("База данных уже существует.")
def get_session():
"""Возвращает сессию для работы с базой данных."""
return Session()