core/services/schema.py

61 lines
2.9 KiB
Python
Raw Normal View History

2025-01-26 15:01:04 +00:00
from asyncio.log import logger
from typing import List
2025-01-26 15:01:04 +00:00
from ariadne import MutationType, ObjectType, QueryType, SchemaBindable
2025-02-10 15:04:08 +00:00
from services.db import create_table_if_not_exists, local_session
2025-01-26 15:01:04 +00:00
2023-10-06 09:51:07 +00:00
query = QueryType()
mutation = MutationType()
2025-04-26 08:45:16 +00:00
type_draft = ObjectType("Draft")
2025-06-30 18:25:26 +00:00
type_community = ObjectType("Community")
2025-06-30 18:46:53 +00:00
type_collection = ObjectType("Collection")
resolvers: List[SchemaBindable] = [query, mutation, type_draft, type_community, type_collection]
2025-01-26 15:01:04 +00:00
def create_all_tables() -> None:
2025-02-10 15:04:08 +00:00
"""Create all database tables in the correct order."""
2025-05-29 09:37:39 +00:00
from auth.orm import Author, AuthorBookmark, AuthorFollower, AuthorRating
2025-06-30 18:46:53 +00:00
from orm import collection, community, draft, notification, reaction, shout, topic
2025-02-10 15:04:08 +00:00
# Порядок важен - сначала таблицы без внешних ключей, затем зависимые таблицы
models_in_order = [
2025-02-11 09:24:02 +00:00
# user.User, # Базовая таблица auth
2025-05-16 06:23:48 +00:00
Author, # Базовая таблица
2025-02-10 15:04:08 +00:00
community.Community, # Базовая таблица
topic.Topic, # Базовая таблица
# Связи для базовых таблиц
2025-05-16 06:23:48 +00:00
AuthorFollower, # Зависит от Author
2025-02-10 15:04:08 +00:00
community.CommunityFollower, # Зависит от Community
topic.TopicFollower, # Зависит от Topic
# Черновики (теперь без зависимости от Shout)
draft.Draft, # Зависит только от Author
draft.DraftAuthor, # Зависит от Draft и Author
draft.DraftTopic, # Зависит от Draft и Topic
# Основные таблицы контента
shout.Shout, # Зависит от Author и Draft
shout.ShoutAuthor, # Зависит от Shout и Author
shout.ShoutTopic, # Зависит от Shout и Topic
# Реакции
reaction.Reaction, # Зависит от Author и Shout
shout.ShoutReactionsFollower, # Зависит от Shout и Reaction
# Дополнительные таблицы
2025-05-16 06:23:48 +00:00
AuthorRating, # Зависит от Author
AuthorBookmark, # Зависит от Author
2025-02-10 15:04:08 +00:00
notification.Notification, # Зависит от Author
notification.NotificationSeen, # Зависит от Notification
2025-06-30 18:46:53 +00:00
collection.Collection, # Зависит от Author
collection.ShoutCollection, # Зависит от Collection и Shout
2025-02-10 15:04:08 +00:00
# invite.Invite
]
with local_session() as session:
for model in models_in_order:
try:
create_table_if_not_exists(session.get_bind(), model)
2025-02-11 09:00:35 +00:00
# logger.info(f"Created or verified table: {model.__tablename__}")
2025-02-10 15:04:08 +00:00
except Exception as e:
table_name = getattr(model, "__tablename__", str(model))
logger.error(f"Error creating table {table_name}: {e}")
2025-02-11 09:00:35 +00:00
raise