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

View File

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

View File

@ -30,7 +30,7 @@ class ShoutTopic(Base):
id = None
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):
__tablename__ = "shout_rating"

View File

@ -1,35 +1,24 @@
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 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):
__tablename__ = "topic_subscription"
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)
createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at")
class Topic(Base):
__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")
body: str = Column(String, nullable=True, comment="Body")
pic: str = Column(String, nullable=True, comment="Picture")
cat_id: str = Column(String, nullable=True, comment="Old Category ID")
# 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)
children = Column(JSONType, nullable=True, comment="list of children topics")
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)
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):
__tablename__ = "user"
@ -59,7 +53,6 @@ class User(Base):
notifications = relationship(lambda: UserNotifications)
ratings = relationship(UserRating, foreign_keys=UserRating.user_id)
roles = relationship(lambda: Role, secondary=UserRole.__tablename__)
topics = relationship(lambda: Topic, secondary=UserTopics)
old_id: str = Column(String, nullable = True)
@staticmethod

View File

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