-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration.py
executable file
·87 lines (59 loc) · 1.99 KB
/
migration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!./__venv__/bin/python3.6
import time
import random
import logging
from flask import Flask
from pymongo import MongoClient
from urllib.parse import urlsplit
from bson import ObjectId
from server.model import Model
logger = logging.getLogger(__name__)
app = Flask(__name__)
app.config.from_pyfile('server/config.py')
parsed = urlsplit(app.config['MONGODB_URI'])
mongo_client = MongoClient(app.config['MONGODB_URI'])
db = mongo_client[parsed.path[1:]]
model = Model(db=db)
def add_user_ids():
collection = db['screams']
screams = collection.find()
for scream in screams:
if 'user_id' in scream:
continue
scream['user_id'] = None
collection.update_one({'_id': scream['_id']}, {'$set': scream})
collection = db['feedbacks']
feedbacks = collection.find()
for feedback in feedbacks:
if 'user_id' in feedback:
continue
feedback['user_id'] = None
collection.update_one({'_id': feedback['_id']}, {'$set': feedback})
def rename_scream_image_attachment():
collection = db['screams']
screams = collection.find()
for scream in screams:
if 'image' in scream:
scream['attachment'] = scream['image']
collection.update_one({'_id': scream['_id']}, {'$unset': {'image': ''}})
collection.update_one({'_id': scream['_id']}, {'$set': scream})
def add_scream_popularity():
collection = db['screams']
screams = collection.find()
for scream in screams:
if 'popularity' in scream:
continue
scream['popularity'] = 0
collection.update_one({'_id': scream['_id']}, {'$set': scream})
def add_scream_image():
collection = db['screams']
screams = collection.find()
for scream in screams:
if 'image' in scream:
continue
scream['image'] = None
collection.update_one({'_id': scream['_id']}, {'$set': scream})
add_scream_image()
add_scream_popularity()
rename_scream_image_attachment()
add_user_ids()