Merge branch 'hotfix/21.56.9' into master
[gitter.git] / scripts / import-chat-data.sh
blobebd76efb19d015547bf905c445d4cd38aaf43b21
1 #!/bin/bash -xe
3 ROOM_URI=$(echo $1 | tr '[:upper:]' '[:lower:]')
4 TO_ROOM_URI=$(echo $2 | tr '[:upper:]' '[:lower:]')
6 if [[ -z $ROOM_URI ]]; then
7 echo usage $0 ROOM_URI [TO_ROOM_URI]
8 exit 1
9 fi
11 if [[ -z $TO_ROOM_URI ]]; then
12 TO_ROOM_URI=$ROOM_URI
15 execute_local() {
16 mongo --quiet localhost/gitter --eval "rs.slaveOk(); $1"
19 execute_prod() {
20 mongo --quiet mongo-replica-02/gitter --eval "rs.slaveOk(); $1"
23 PROD_ROOM_ID=$(execute_prod "db.troupes.findOne({ lcUri: '$ROOM_URI' }, { _id: 1 })._id.valueOf()")
25 if [[ -z $PROD_ROOM_ID ]]; then
26 echo "Unable to find room in prod"
27 exit 1
30 DEV_ROOM_ID=$(execute_local "db.troupes.findOne({ lcUri: '$TO_ROOM_URI' }, { _id: 1 })._id.valueOf()")
31 if [[ -z $DEV_ROOM_ID ]]; then
32 echo "Unable to find room in dev"
33 exit 1
36 DEV_USER_ID=$(execute_local "db.users.findOne({}, { _id: 1 })._id.valueOf()")
37 if [[ -z $DEV_USER_ID ]]; then
38 echo "Unable to find a local userid"
39 exit 1
42 mongoexport -h mongo-replica-02 -d gitter -c chatmessages -q "{ toTroupeId: ObjectId('$PROD_ROOM_ID') }" --jsonArray > /tmp/chats.json
44 node -e "
45 var data = '';
46 process.stdin.setEncoding('utf8');
47 process.stdin.on('data', function (d) {
48 data = data + d;
49 });
50 process.stdin.on('end', function () {
51 // put all your code here
52 var array = JSON.parse(data);
53 array.forEach(function(d) {
54 if(d.toTroupeId) {
55 d.toTroupeId.\$oid = '$DEV_ROOM_ID';
58 if(d.fromUserId) {
59 d.fromUserId.\$oid = '$DEV_USER_ID';
61 });
62 console.log(JSON.stringify(array))
63 });
64 process.stdin.resume();
65 " < /tmp/chats.json > /tmp/chats2.json
68 mongoimport -h localhost -d gitter -c chatmessages --jsonArray < /tmp/chats2.json