From 0b654d8b02e14a4c9740fc8ff085b5337ca695df Mon Sep 17 00:00:00 2001 From: tonyrewin Date: Tue, 31 May 2022 15:19:05 +0300 Subject: [PATCH] migration script fix --- .gitignore | 4 +- README.md | 1 + migration/tables/content_item_categories.py | 10 +-- migration/tables/tags.py | 69 +++++++++++---------- 4 files changed, 46 insertions(+), 38 deletions(-) diff --git a/.gitignore b/.gitignore index a4f8206e..65200daa 100644 --- a/.gitignore +++ b/.gitignore @@ -137,4 +137,6 @@ Pipfile.lock migration/data migration/content/**/*.md -.obsidian \ No newline at end of file +.obsidian + +*.zip \ No newline at end of file diff --git a/README.md b/README.md index 8b30a670..52e5363c 100644 --- a/README.md +++ b/README.md @@ -34,3 +34,4 @@ Also see `Dockerfile` # How to do an authorized request Put the header 'Auth' with token from signInQuery or registerQuery. + diff --git a/migration/tables/content_item_categories.py b/migration/tables/content_item_categories.py index 2380f4f2..1eae2191 100644 --- a/migration/tables/content_item_categories.py +++ b/migration/tables/content_item_categories.py @@ -16,16 +16,18 @@ def migrate(entry): 'slug': entry['slug'], # 'createdBy': entry['createdBy'], # 'createdAt': date_parse(entry['createdAt']), - 'title': entry['title'], #.lower(), + 'title': entry['title'].replace(' ', ' '), #.lower(), 'children': [], 'community' : Community.default_community.slug, 'body' : entry.get('description') } try: with local_session() as session: - topic = session.query(Topic).filter(Topic.slug == entry['slug']).first() - if not topic: - topic = Topic.create(**topic_dict) + topic = session.query(Topic).filter(Topic.slug == topic_dict['slug']).first() + if not topic: + topic = session.query(Topic).filter(Topic.title == topic_dict['title']).first() + if not topic: + topic = Topic.create(**topic_dict) except Exception as e: print(e) raise e diff --git a/migration/tables/tags.py b/migration/tables/tags.py index 000766ab..0a6d743c 100644 --- a/migration/tables/tags.py +++ b/migration/tables/tags.py @@ -5,36 +5,39 @@ from orm import Topic, Community from dateutil.parser import parse as date_parse def migrate(entry): - ''' - type Topic { - slug: String! # ID - createdBy: Int! # User - createdAt: DateTime! - title: String - parents: [String] # NOTE: topic can have parent topics - children: [String] # and children - } - ''' - if type(entry['createdAt']) == type(''): - ts = date_parse(entry['createdAt']) - else: - ts = datetime.fromtimestamp(entry['createdAt']/1000) - topic_dict = { - 'slug': entry['slug'], - # 'createdBy': entry['createdBy'], - # 'createdAt': ts, - 'title': entry['title'], # .lower(), - 'children': [], - 'community' : Community.default_community.slug, - 'body' : entry.get('description') - } - try: - with local_session() as session: - topic = session.query(Topic).filter(Topic.slug == entry['slug']).first() - if not topic: topic = Topic.create(**topic_dict) - except Exception as e: - print(e) - raise e - - topic_dict['tag_id'] = entry['_id'] - return topic_dict + ''' + type Topic { + slug: String! # ID + createdBy: Int! # User + createdAt: DateTime! + title: String + parents: [String] # NOTE: topic can have parent topics + children: [String] # and children + } + ''' + if type(entry['createdAt']) == type(''): + ts = date_parse(entry['createdAt']) + else: + ts = datetime.fromtimestamp(entry['createdAt']/1000) + topic_dict = { + 'slug': entry['slug'], + # 'createdBy': entry['createdBy'], + # 'createdAt': ts, + 'title': entry['title'].replace(' ', ' '), # .lower(), + 'children': [], + 'community' : Community.default_community.slug, + 'body' : entry.get('description') + } + try: + with local_session() as session: + topic = session.query(Topic).filter(Topic.slug == topic_dict['slug']).first() + if not topic: + topic = session.query(Topic).filter(Topic.title == topic_dict['title']).first() + if not topic: + topic = Topic.create(**topic_dict) + except Exception as e: + print(e) + raise e + + topic_dict['tag_id'] = entry['_id'] + return topic_dict