diff --git a/migration/tables/content_item_categories.py b/migration/tables/content_item_categories.py index 3ab2ceec..f4b5f4ba 100644 --- a/migration/tables/content_item_categories.py +++ b/migration/tables/content_item_categories.py @@ -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 diff --git a/migration/tables/content_items.py b/migration/tables/content_items.py index 55fdc0df..11090040 100644 --- a/migration/tables/content_items.py +++ b/migration/tables/content_items.py @@ -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 diff --git a/migration/tables/tags.py b/migration/tables/tags.py index 1ce5523d..d2d79cb4 100644 --- a/migration/tables/tags.py +++ b/migration/tables/tags.py @@ -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 diff --git a/orm/shout.py b/orm/shout.py index 56b03da7..7839f45f 100644 --- a/orm/shout.py +++ b/orm/shout.py @@ -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" diff --git a/orm/topic.py b/orm/topic.py index 2e777c31..4ecc337e 100644 --- a/orm/topic.py +++ b/orm/topic.py @@ -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") - diff --git a/orm/user.py b/orm/user.py index f0db95a6..7cde7e3b 100644 --- a/orm/user.py +++ b/orm/user.py @@ -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 diff --git a/schema.graphql b/schema.graphql index 7caeacd6..f9fe6662 100644 --- a/schema.graphql +++ b/schema.graphql @@ -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 }