Files
core/storage/schema.py

98 lines
3.2 KiB
Python
Raw Normal View History

2025-01-26 18:01:04 +03:00
from asyncio.log import logger
2025-07-02 22:30:21 +03:00
from enum import Enum
2025-01-26 18:01:04 +03:00
2025-07-02 22:30:21 +03:00
from ariadne import (
MutationType,
ObjectType,
QueryType,
SchemaBindable,
load_schema_from_path,
)
from storage.db import engine
2025-01-26 18:01:04 +03:00
2025-07-02 22:30:21 +03:00
# Создаем основные типы
2023-10-06 12:51:07 +03:00
query = QueryType()
mutation = MutationType()
2025-04-26 11:45:16 +03:00
type_draft = ObjectType("Draft")
2025-06-30 21:25:26 +03:00
type_community = ObjectType("Community")
2025-06-30 21:46:53 +03:00
type_collection = ObjectType("Collection")
2025-07-02 22:30:21 +03:00
type_author = ObjectType("Author")
# Загружаем определения типов из файлов схемы
type_defs = load_schema_from_path("schema/")
# Список всех типов для схемы
resolvers: SchemaBindable | type[Enum] | list[SchemaBindable | type[Enum]] = [
query,
mutation,
type_draft,
type_community,
type_collection,
type_author,
]
2025-01-26 18:01:04 +03:00
def create_all_tables() -> None:
"""Create all database tables using SQLAlchemy's standard approach."""
try:
# Импортируем все модели чтобы они были зарегистрированы в Base.metadata
# Получаем Base с зарегистрированными моделями
from orm.base import BaseModel as Base
# Проверяем что все критические таблицы зарегистрированы
required_tables = [
"author",
"community",
"community_author",
"community_follower",
"draft",
"draft_author",
"draft_topic",
"shout",
"shout_author",
"shout_topic",
"shout_reactions_followers",
"topic",
"topic_followers",
"reaction",
"invite",
"notification",
"collection",
"author_follower",
"author_rating",
"author_bookmark",
]
registered_tables = list(Base.metadata.tables.keys())
missing_tables = [table for table in required_tables if table not in registered_tables]
if missing_tables:
logger.warning(f"Missing tables in Base.metadata: {missing_tables}")
logger.info(f"Available tables: {registered_tables}")
# Создаем все таблицы стандартным способом SQLAlchemy
logger.info("Creating all database tables...")
Base.metadata.create_all(bind=engine)
# Проверяем результат
from sqlalchemy import inspect
inspector = inspect(engine)
created_tables = inspector.get_table_names()
logger.info(f"✅ Created tables: {created_tables}")
# Проверяем критически важные таблицы
missing_created = [table for table in required_tables if table not in created_tables]
if missing_created:
error_msg = f"Failed to create critical tables: {missing_created}"
logger.error(f"❌ Missing critical tables: {missing_created}")
raise RuntimeError(error_msg)
logger.info("✅ All critical tables created successfully")
except Exception as e:
logger.error(f"❌ Error creating database tables: {e}")
raise