fix migration

This commit is contained in:
knst-kotov 2021-10-16 16:53:46 +03:00
parent 00942e985b
commit 205e990aa9
4 changed files with 34 additions and 17 deletions

View File

@ -12,6 +12,9 @@ from migration.utils import DateTimeEncoder
from orm import Community from orm import Community
from dateutil.parser import parse as date_parse from dateutil.parser import parse as date_parse
from orm.base import local_session
from orm import User
IMG_REGEX = r"\!\[(.*?)\]\((data\:image\/(png|jpeg|jpg);base64\,(.*?))\)" IMG_REGEX = r"\!\[(.*?)\]\((data\:image\/(png|jpeg|jpg);base64\,(.*?))\)"
OLD_DATE = '2016-03-05 22:22:00.350000' OLD_DATE = '2016-03-05 22:22:00.350000'
@ -26,6 +29,17 @@ if __name__ == '__main__':
users_by_oid = {} users_by_oid = {}
users_by_slug = {} users_by_slug = {}
with local_session() as session:
default_user = session.query(User).filter(User.id == 0).first()
if not default_user:
default_user = User.create(id = 0, email = "discours@discours.io", username = "discours", slug = "default", old_id = 0)
user_id_map = {}
with local_session() as session:
users = session.query(User).all()
for user in users:
user_id_map[user.old_id] = user.id
tags_data = json.loads(open('migration/data/tags.json').read()) tags_data = json.loads(open('migration/data/tags.json').read())
print(str(len(tags_data)) + ' tags loaded') print(str(len(tags_data)) + ' tags loaded')
@ -111,12 +125,16 @@ if __name__ == '__main__':
print('migrating %d topics...' % limit) print('migrating %d topics...' % limit)
counter = 0 counter = 0
for cat in cats_data: for cat in cats_data:
old_id = cat["createdBy"]
cat["createdBy"] = user_id_map[old_id]
try: topic = migrateCategory(cat) try: topic = migrateCategory(cat)
except Exception as e: raise e except Exception as e: raise e
topics_by_cat[topic['cat_id']] = topic topics_by_cat[topic['cat_id']] = topic
topics_by_slug[topic['slug']] = topic topics_by_slug[topic['slug']] = topic
counter += 1 counter += 1
for tag in tags_data: for tag in tags_data:
old_id = tag["createdBy"]
tag["createdBy"] = user_id_map.get(old_id, 0)
topic = migrateTag(tag) topic = migrateTag(tag)
topics_by_tag[topic['tag_id']] = topic topics_by_tag[topic['tag_id']] = topic
if not topics_by_slug.get(topic['slug']): topics_by_slug[topic['slug']] = topic if not topics_by_slug.get(topic['slug']): topics_by_slug[topic['slug']] = topic
@ -288,17 +306,15 @@ if __name__ == '__main__':
elif cmd == "topics": elif cmd == "topics":
topics(export_topics, topics_by_slug, topics_by_cat, topics_by_tag, cats_data, tags_data) topics(export_topics, topics_by_slug, topics_by_cat, topics_by_tag, cats_data, tags_data)
elif cmd == "shouts": elif cmd == "shouts":
try: Community.create(**{
Community.create(**{ 'id' : 0,
'slug': 'discours.io', 'slug': 'discours.io',
'name': 'Дискурс', 'name': 'Дискурс',
'pic': 'https://discours.io/images/logo-min.svg', 'pic': 'https://discours.io/images/logo-min.svg',
'createdBy': '0', 'createdBy': '0',
'createdAt': date_parse(OLD_DATE) 'createdAt': date_parse(OLD_DATE)
}) })
except Exception: shouts(content_data, shouts_by_slug, shouts_by_oid) # NOTE: listens limit
pass
shouts(shouts_by_slug, shouts_by_oid) # NOTE: listens limit
elif cmd == "export_shouts": elif cmd == "export_shouts":
export_shouts(shouts_by_slug, export_articles, export_authors, content_dict) export_shouts(shouts_by_slug, export_articles, export_authors, content_dict)
elif cmd == "all": elif cmd == "all":
@ -314,7 +330,7 @@ if __name__ == '__main__':
bson2json.json_tables() bson2json.json_tables()
elif cmd == 'slug': elif cmd == 'slug':
export_slug(sys.argv[2], export_articles, export_authors, content_dict) export_slug(sys.argv[2], export_articles, export_authors, content_dict)
export_finish(export_articles, export_authors, export_topics, export_comments) #export_finish(export_articles, export_authors, export_topics, export_comments)
else: else:
print(''' print('''
usage: python migrate.py bson usage: python migrate.py bson

View File

@ -15,7 +15,7 @@ def migrate(entry):
''' '''
topic_dict = { topic_dict = {
'slug': entry['slug'], 'slug': entry['slug'],
'createdBy': entry['createdBy'], # NOTE: uses an old user id 'createdBy': entry['createdBy'],
'createdAt': date_parse(entry['createdAt']), 'createdAt': date_parse(entry['createdAt']),
'title': entry['title'].lower(), 'title': entry['title'].lower(),
'parents': [], 'parents': [],

View File

@ -21,7 +21,7 @@ def migrate(entry):
ts = datetime.fromtimestamp(entry['createdAt']/1000) ts = datetime.fromtimestamp(entry['createdAt']/1000)
topic_dict = { topic_dict = {
'slug': entry['slug'], 'slug': entry['slug'],
'createdBy': 0, 'createdBy': entry['createdBy'],
'createdAt': ts, 'createdAt': ts,
'title': entry['title'].lower(), 'title': entry['title'].lower(),
'parents': [], 'parents': [],

View File

@ -15,3 +15,4 @@ python-frontmatter
transliterate transliterate
requests requests
bcrypt bcrypt
bs4