2021-10-08 04:42:59 +00:00
|
|
|
''' cmd managed migration '''
|
2021-08-20 09:27:19 +00:00
|
|
|
import json
|
2022-07-07 13:55:13 +00:00
|
|
|
from migration.export import export_email_subscriptions, export_mdx, export_slug
|
2021-08-20 09:27:19 +00:00
|
|
|
from migration.tables.users import migrate as migrateUser
|
2021-12-08 10:29:04 +00:00
|
|
|
from migration.tables.users import migrate_2stage as migrateUser_2stage
|
2022-07-07 13:55:13 +00:00
|
|
|
from migration.tables.content_items import get_shout_slug, migrate as migrateShout
|
|
|
|
from migration.tables.topics import migrate as migrateTopic
|
2021-10-13 17:46:30 +00:00
|
|
|
from migration.tables.comments import migrate as migrateComment
|
2021-12-08 07:28:38 +00:00
|
|
|
from migration.tables.comments import migrate_2stage as migrateComment_2stage
|
2022-07-14 13:41:53 +00:00
|
|
|
from orm.base import local_session
|
|
|
|
from orm.community import Community
|
|
|
|
from orm.user import User
|
2021-10-16 13:53:46 +00:00
|
|
|
|
2021-10-08 04:42:59 +00:00
|
|
|
OLD_DATE = '2016-03-05 22:22:00.350000'
|
|
|
|
|
2022-07-07 13:55:13 +00:00
|
|
|
def users_handle(storage):
|
2021-11-23 07:16:42 +00:00
|
|
|
''' migrating users first '''
|
|
|
|
counter = 0
|
2021-12-08 10:29:04 +00:00
|
|
|
id_map = {}
|
2022-07-07 13:55:13 +00:00
|
|
|
print('[migration] migrating %d users' %(len(storage['users']['data'])))
|
|
|
|
for entry in storage['users']['data']:
|
2021-11-23 07:16:42 +00:00
|
|
|
oid = entry['_id']
|
|
|
|
user = migrateUser(entry)
|
2022-07-07 13:55:13 +00:00
|
|
|
storage['users']['by_oid'][oid] = user # full
|
2021-11-23 07:16:42 +00:00
|
|
|
del user['password']
|
|
|
|
del user['notifications']
|
|
|
|
del user['emailConfirmed']
|
|
|
|
del user['username']
|
|
|
|
del user['email']
|
2022-07-07 13:55:13 +00:00
|
|
|
storage['users']['by_slug'][user['slug']] = user # public
|
|
|
|
id_map[user['oid']] = user['slug']
|
2021-11-23 07:16:42 +00:00
|
|
|
counter += 1
|
2022-07-03 00:58:41 +00:00
|
|
|
ce = 0
|
2022-07-07 13:55:13 +00:00
|
|
|
for entry in storage['users']['data']:
|
2022-07-03 00:58:41 +00:00
|
|
|
ce += migrateUser_2stage(entry, id_map)
|
2022-07-08 07:12:32 +00:00
|
|
|
return storage
|
2021-11-23 07:16:42 +00:00
|
|
|
|
|
|
|
|
2022-07-07 13:55:13 +00:00
|
|
|
def topics_handle(storage):
|
2021-11-23 07:16:42 +00:00
|
|
|
''' topics from categories and tags '''
|
|
|
|
counter = 0
|
2022-07-07 13:55:13 +00:00
|
|
|
for t in (storage['topics']['tags'] + storage['topics']['cats']):
|
|
|
|
if t['slug'] in storage['replacements']:
|
|
|
|
t['slug'] = storage['replacements'][t['slug']]
|
|
|
|
topic = migrateTopic(t)
|
|
|
|
storage['topics']['by_oid'][t['_id']] = topic
|
|
|
|
storage['topics']['by_slug'][t['slug']] = topic
|
2022-07-01 06:39:19 +00:00
|
|
|
counter += 1
|
2022-07-07 13:55:13 +00:00
|
|
|
else:
|
|
|
|
print('[migration] topic ' + t['slug'] + ' ignored')
|
|
|
|
for oldslug, newslug in storage['replacements'].items():
|
|
|
|
if oldslug != newslug and oldslug in storage['topics']['by_slug']:
|
|
|
|
oid = storage['topics']['by_slug'][oldslug]['_id']
|
|
|
|
del storage['topics']['by_slug'][oldslug]
|
|
|
|
storage['topics']['by_oid'][oid] = storage['topics']['by_slug'][newslug]
|
|
|
|
print( '[migration] ' + str(counter) + ' topics migrated')
|
|
|
|
print( '[migration] ' + str(len(storage['topics']['by_oid'].values())) + ' topics by oid' )
|
|
|
|
print( '[migration] ' + str(len(storage['topics']['by_slug'].values())) + ' topics by slug' )
|
|
|
|
# raise Exception
|
2022-07-08 07:12:32 +00:00
|
|
|
return storage
|
2022-07-07 13:55:13 +00:00
|
|
|
|
|
|
|
def shouts_handle(storage):
|
2021-11-23 07:16:42 +00:00
|
|
|
''' migrating content items one by one '''
|
|
|
|
counter = 0
|
|
|
|
discours_author = 0
|
2022-07-03 00:58:41 +00:00
|
|
|
pub_counter = 0
|
2022-07-07 13:55:13 +00:00
|
|
|
for entry in storage['shouts']['data']:
|
|
|
|
# slug
|
|
|
|
slug = get_shout_slug(entry)
|
|
|
|
|
|
|
|
# single slug mode
|
|
|
|
if '-' in sys.argv and slug not in sys.argv: continue
|
|
|
|
|
|
|
|
# migrate
|
|
|
|
shout = migrateShout(entry, storage)
|
2022-07-08 07:12:32 +00:00
|
|
|
storage['shouts']['by_oid'][entry['_id']] = shout
|
|
|
|
storage['shouts']['by_slug'][shout['slug']] = shout
|
2022-07-07 13:55:13 +00:00
|
|
|
# shouts.topics
|
|
|
|
if not shout['topics']: print('[migration] no topics!')
|
|
|
|
|
|
|
|
# wuth author
|
|
|
|
author = shout['authors'][0].slug
|
|
|
|
if author =='discours': discours_author += 1
|
|
|
|
# print('[migration] ' + shout['slug'] + ' with author ' + author)
|
|
|
|
|
|
|
|
if entry.get('published'):
|
2022-07-13 16:01:48 +00:00
|
|
|
if 'mdx' in sys.argv: export_mdx(shout)
|
2022-07-07 13:55:13 +00:00
|
|
|
pub_counter += 1
|
|
|
|
|
|
|
|
# print main counter
|
|
|
|
counter += 1
|
|
|
|
line = str(counter+1) + ': ' + shout['slug'] + " @" + author
|
|
|
|
print(line)
|
|
|
|
|
2022-07-03 00:58:41 +00:00
|
|
|
print('[migration] ' + str(counter) + ' content items were migrated')
|
|
|
|
print('[migration] ' + str(pub_counter) + ' have been published')
|
|
|
|
print('[migration] ' + str(discours_author) + ' authored by @discours')
|
2022-07-08 07:12:32 +00:00
|
|
|
return storage
|
2021-11-23 07:16:42 +00:00
|
|
|
|
2022-07-07 13:55:13 +00:00
|
|
|
def comments_handle(storage):
|
2021-12-08 07:28:38 +00:00
|
|
|
id_map = {}
|
2022-07-07 13:55:13 +00:00
|
|
|
ignored_counter = 0
|
|
|
|
for oldcomment in storage['comments']['data']:
|
|
|
|
comment = migrateComment(oldcomment, storage)
|
2021-12-13 07:50:33 +00:00
|
|
|
if not comment:
|
2022-07-07 13:55:13 +00:00
|
|
|
print('[migration] comment ignored \n%r\n' % oldcomment)
|
|
|
|
ignored_counter += 1
|
2021-12-13 07:50:33 +00:00
|
|
|
continue
|
2021-12-08 07:28:38 +00:00
|
|
|
id = comment.get('id')
|
2022-07-07 13:55:13 +00:00
|
|
|
oid = comment.get('oid')
|
|
|
|
id_map[oid] = id
|
2022-07-08 07:12:32 +00:00
|
|
|
|
2022-07-07 13:55:13 +00:00
|
|
|
for comment in storage['comments']['data']: migrateComment_2stage(comment, id_map)
|
|
|
|
print('[migration] ' + str(len(id_map)) + ' comments migrated')
|
|
|
|
print('[migration] ' + str(ignored_counter) + ' comments ignored')
|
2022-07-08 07:12:32 +00:00
|
|
|
return storage
|
2021-11-23 07:16:42 +00:00
|
|
|
|
|
|
|
|
2022-07-07 13:55:13 +00:00
|
|
|
def bson_handle():
|
|
|
|
# decode bson # preparing data
|
|
|
|
from migration import bson2json
|
|
|
|
bson2json.json_tables()
|
|
|
|
|
|
|
|
def export_one(slug, storage):
|
|
|
|
topics_handle(storage)
|
|
|
|
users_handle(storage)
|
|
|
|
shouts_handle(storage)
|
|
|
|
export_slug(slug, storage)
|
|
|
|
|
|
|
|
def all_handle(storage):
|
|
|
|
print('[migration] everything!')
|
|
|
|
users_handle(storage)
|
|
|
|
topics_handle(storage)
|
|
|
|
shouts_handle(storage)
|
|
|
|
comments_handle(storage)
|
|
|
|
export_email_subscriptions()
|
2022-07-08 07:12:32 +00:00
|
|
|
print('[migration] done!')
|
2022-07-07 13:55:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def data_load():
|
|
|
|
storage = {
|
|
|
|
'content_items': {
|
|
|
|
'by_oid': {},
|
|
|
|
'by_slug': {},
|
|
|
|
},
|
|
|
|
'shouts': {
|
|
|
|
'by_oid': {},
|
|
|
|
'by_slug': {},
|
|
|
|
'data': []
|
|
|
|
},
|
|
|
|
'comments': {
|
|
|
|
'by_oid': {},
|
|
|
|
'by_slug': {},
|
|
|
|
'by_content': {},
|
|
|
|
'data': []
|
|
|
|
},
|
|
|
|
'topics': {
|
|
|
|
'by_oid': {},
|
|
|
|
'by_slug': {},
|
|
|
|
'cats': [],
|
|
|
|
'tags': [],
|
|
|
|
},
|
|
|
|
'users': {
|
|
|
|
'by_oid': {},
|
|
|
|
'by_slug': {},
|
|
|
|
'data': []
|
|
|
|
},
|
|
|
|
'replacements': json.loads(open('migration/tables/replacements.json').read())
|
|
|
|
}
|
|
|
|
users_data = []
|
|
|
|
tags_data = []
|
|
|
|
cats_data = []
|
|
|
|
comments_data = []
|
|
|
|
content_data = []
|
|
|
|
try:
|
|
|
|
users_data = json.loads(open('migration/data/users.json').read())
|
|
|
|
print('[migration] ' + str(len(users_data)) + ' users loaded')
|
|
|
|
tags_data = json.loads(open('migration/data/tags.json').read())
|
|
|
|
storage['topics']['tags'] = tags_data
|
|
|
|
print('[migration] ' + str(len(tags_data)) + ' tags loaded')
|
|
|
|
cats_data = json.loads(open('migration/data/content_item_categories.json').read())
|
|
|
|
storage['topics']['cats'] = cats_data
|
|
|
|
print('[migration] ' + str(len(cats_data)) + ' cats loaded')
|
|
|
|
comments_data = json.loads(open('migration/data/comments.json').read())
|
|
|
|
storage['comments']['data'] = comments_data
|
|
|
|
print('[migration] ' + str(len(comments_data)) + ' comments loaded')
|
|
|
|
content_data = json.loads(open('migration/data/content_items.json').read())
|
|
|
|
storage['shouts']['data'] = content_data
|
|
|
|
print('[migration] ' + str(len(content_data)) + ' content items loaded')
|
|
|
|
# fill out storage
|
|
|
|
for x in users_data:
|
|
|
|
storage['users']['by_oid'][x['_id']] = x
|
|
|
|
# storage['users']['by_slug'][x['slug']] = x
|
|
|
|
# no user.slug yet
|
|
|
|
print('[migration] ' + str(len(storage['users']['by_oid'].keys())) + ' users by oid')
|
|
|
|
for x in tags_data:
|
|
|
|
storage['topics']['by_oid'][x['_id']] = x
|
|
|
|
storage['topics']['by_slug'][x['slug']] = x
|
|
|
|
for x in cats_data:
|
|
|
|
storage['topics']['by_oid'][x['_id']] = x
|
|
|
|
storage['topics']['by_slug'][x['slug']] = x
|
|
|
|
print('[migration] ' + str(len(storage['topics']['by_slug'].keys())) + ' topics by slug')
|
|
|
|
for item in content_data:
|
|
|
|
slug = get_shout_slug(item)
|
|
|
|
storage['content_items']['by_slug'][slug] = item
|
|
|
|
storage['content_items']['by_oid'][item['_id']] = item
|
|
|
|
print('[migration] ' + str(len(content_data)) + ' content items')
|
|
|
|
for x in comments_data:
|
|
|
|
storage['comments']['by_oid'][x['_id']] = x
|
|
|
|
cid = x['contentItem']
|
|
|
|
storage['comments']['by_content'][cid] = x
|
|
|
|
ci = storage['content_items']['by_oid'].get(cid, {})
|
|
|
|
if 'slug' in ci: storage['comments']['by_slug'][ci['slug']] = x
|
|
|
|
print('[migration] ' + str(len(storage['comments']['by_content'].keys())) + ' with comments')
|
|
|
|
except Exception as e: raise e
|
|
|
|
storage['users']['data'] = users_data
|
|
|
|
storage['topics']['tags'] = tags_data
|
|
|
|
storage['topics']['cats'] = cats_data
|
|
|
|
storage['shouts']['data'] = content_data
|
|
|
|
storage['comments']['data'] = comments_data
|
|
|
|
return storage
|
2021-11-23 07:16:42 +00:00
|
|
|
|
2021-10-15 10:00:26 +00:00
|
|
|
if __name__ == '__main__':
|
2021-11-23 07:16:42 +00:00
|
|
|
import sys
|
|
|
|
if len(sys.argv) > 1:
|
2022-07-07 13:55:13 +00:00
|
|
|
cmd = sys.argv[1]
|
|
|
|
print('[migration] command: ' + cmd)
|
|
|
|
if cmd == 'bson':
|
|
|
|
bson_handle()
|
|
|
|
else:
|
|
|
|
storage = data_load()
|
|
|
|
if cmd == '-': export_one(sys.argv[2], storage)
|
|
|
|
else: all_handle(storage)
|
|
|
|
|
2021-11-23 07:16:42 +00:00
|
|
|
else:
|
2022-07-03 00:58:41 +00:00
|
|
|
print('usage: python migrate.py bson')
|
2022-07-07 13:55:13 +00:00
|
|
|
print('.. \t- <slug>')
|
2022-07-03 00:58:41 +00:00
|
|
|
print('.. \tall')
|