remove topic id; topic children as json column

This commit is contained in:
knst-kotov 2021-12-12 12:44:54 +03:00
parent e8780cfb86
commit d00488a458
7 changed files with 11 additions and 35 deletions

View File

@ -9,8 +9,7 @@ def migrate(entry):
createdBy: Int! # User createdBy: Int! # User
createdAt: DateTime! createdAt: DateTime!
value: String value: String
parents: [String] # NOTE: topic can have parent topics children: [String] # children topic
children: [String] # and children
} }
''' '''
topic_dict = { topic_dict = {
@ -18,9 +17,7 @@ def migrate(entry):
# 'createdBy': entry['createdBy'], # 'createdBy': entry['createdBy'],
# 'createdAt': date_parse(entry['createdAt']), # 'createdAt': date_parse(entry['createdAt']),
'title': entry['title'].lower(), 'title': entry['title'].lower(),
'parents': [],
'children': [], 'children': [],
'cat_id': entry['_id'],
'community' : Community.default_community.slug 'community' : Community.default_community.slug
} }
try: try:
@ -28,8 +25,8 @@ def migrate(entry):
topic = session.query(Topic).filter(Topic.slug == entry['slug']).first() topic = session.query(Topic).filter(Topic.slug == entry['slug']).first()
if not topic: if not topic:
topic = Topic.create(**topic_dict) topic = Topic.create(**topic_dict)
topic_dict['id'] = topic.id
return topic_dict
except Exception as e: except Exception as e:
print(e) print(e)
raise e raise e
topic_dict['cat_id'] = entry['_id']
return topic_dict

View File

@ -224,7 +224,7 @@ def migrate(entry, users_by_oid, topics_by_oid):
shout_dict['id'] = s.id shout_dict['id'] = s.id
shout_dict['topics'] = [] shout_dict['topics'] = []
for topic in r['topics']: for topic in r['topics']:
ShoutTopic.create(**{ 'shout': s.id, 'topic': topic['id'] }) ShoutTopic.create(**{ 'shout': s.id, 'topic': topic['slug'] })
shout_dict['topics'].append(topic['slug']) shout_dict['topics'].append(topic['slug'])
except Exception as e: except Exception as e:
raise e raise e

View File

@ -24,7 +24,6 @@ def migrate(entry):
# 'createdBy': entry['createdBy'], # 'createdBy': entry['createdBy'],
# 'createdAt': ts, # 'createdAt': ts,
'title': entry['title'].lower(), 'title': entry['title'].lower(),
'parents': [],
'children': [], 'children': [],
'community' : Community.default_community.slug 'community' : Community.default_community.slug
} }
@ -32,7 +31,6 @@ def migrate(entry):
with local_session() as session: with local_session() as session:
topic = session.query(Topic).filter(Topic.slug == entry['slug']).first() topic = session.query(Topic).filter(Topic.slug == entry['slug']).first()
if not topic: topic = Topic.create(**topic_dict) if not topic: topic = Topic.create(**topic_dict)
topic_dict['id'] = topic.id
except Exception as e: except Exception as e:
print(e) print(e)
raise e raise e

View File

@ -30,7 +30,7 @@ class ShoutTopic(Base):
id = None id = None
shout = Column(ForeignKey('shout.id'), primary_key = True) shout = Column(ForeignKey('shout.id'), primary_key = True)
topic = Column(ForeignKey('topic.id'), primary_key = True) topic = Column(ForeignKey('topic.slug'), primary_key = True)
class ShoutRating(Base): class ShoutRating(Base):
__tablename__ = "shout_rating" __tablename__ = "shout_rating"

View File

@ -1,35 +1,24 @@
from datetime import datetime from datetime import datetime
from sqlalchemy import Table, Column, Integer, String, ForeignKey, DateTime, UniqueConstraint from sqlalchemy import Table, Column, Integer, String, ForeignKey, DateTime, JSON as JSONType
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from orm.base import Base from orm.base import Base
Connection = Table('topic_connections',
Base.metadata,
Column('child', Integer, ForeignKey('topic.id')),
Column('parent', Integer, ForeignKey('topic.id')),
UniqueConstraint('parent', 'child', name='unique_usage')
)
class TopicSubscription(Base): class TopicSubscription(Base):
__tablename__ = "topic_subscription" __tablename__ = "topic_subscription"
id = None id = None
topic = Column(ForeignKey('topic.id'), primary_key = True) topic = Column(ForeignKey('topic.slug'), primary_key = True)
user = Column(ForeignKey('user.id'), primary_key = True) user = Column(ForeignKey('user.id'), primary_key = True)
createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at") createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at")
class Topic(Base): class Topic(Base):
__tablename__ = 'topic' __tablename__ = 'topic'
slug: str = Column(String, unique = True, nullable = False) id = None
slug: str = Column(String, primary_key = True)
title: str = Column(String, nullable=False, comment="Title") title: str = Column(String, nullable=False, comment="Title")
body: str = Column(String, nullable=True, comment="Body") body: str = Column(String, nullable=True, comment="Body")
pic: str = Column(String, nullable=True, comment="Picture") pic: str = Column(String, nullable=True, comment="Picture")
cat_id: str = Column(String, nullable=True, comment="Old Category ID") children = Column(JSONType, nullable=True, comment="list of children topics")
# list of Topics where the current node is the "other party" or "child"
parents = relationship(lambda: Topic, secondary=Connection, primaryjoin=slug==Connection.c.parent, secondaryjoin=slug==Connection.c.child, viewonly=True)
# list of Topics where the current node is the "parent"
children = relationship(lambda: Topic, secondary=Connection, primaryjoin=slug==Connection.c.child, secondaryjoin=slug==Connection.c.parent)
community = Column(ForeignKey("community.slug"), nullable=False, comment="Community") community = Column(ForeignKey("community.slug"), nullable=False, comment="Community")

View File

@ -33,12 +33,6 @@ class UserRole(Base):
user_id = Column(ForeignKey('user.id'), primary_key = True) user_id = Column(ForeignKey('user.id'), primary_key = True)
role_id = Column(ForeignKey('role.id'), primary_key = True) role_id = Column(ForeignKey('role.id'), primary_key = True)
UserTopics = Table("user_topics",
Base.metadata,
Column('user_id', Integer, ForeignKey('user.id'), primary_key = True),
Column('topic_id', Integer, ForeignKey('topic.id'), primary_key = True)
)
class User(Base): class User(Base):
__tablename__ = "user" __tablename__ = "user"
@ -59,7 +53,6 @@ class User(Base):
notifications = relationship(lambda: UserNotifications) notifications = relationship(lambda: UserNotifications)
ratings = relationship(UserRating, foreign_keys=UserRating.user_id) ratings = relationship(UserRating, foreign_keys=UserRating.user_id)
roles = relationship(lambda: Role, secondary=UserRole.__tablename__) roles = relationship(lambda: Role, secondary=UserRole.__tablename__)
topics = relationship(lambda: Topic, secondary=UserTopics)
old_id: str = Column(String, nullable = True) old_id: str = Column(String, nullable = True)
@staticmethod @staticmethod

View File

@ -235,7 +235,6 @@ type User {
ratings: [Rating] ratings: [Rating]
bio: String bio: String
notifications: [Int] notifications: [Int]
topics: [String] # user subscribed topics
communities: [Int] # user participating communities communities: [Int] # user participating communities
old_id: String old_id: String
} }