File size: 6,126 Bytes
1e92f2d |
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
exports.up = function(r, conn) {
return (
// Create new tables, update old ones with receiveNotifications
Promise.all([
r
.table('usersChannels')
.filter({ isBlocked: false })
.update({ receiveNotifications: true })
.run(conn)
.catch(err => {
console.log(err);
throw err;
}),
r
.table('usersCommunities')
.filter({ isBlocked: false })
.update({ receiveNotifications: true })
.run(conn)
.catch(err => {
console.log(err);
throw err;
}),
r
.tableCreate('notifications')
.run(conn)
.catch(err => {
console.log(err);
throw err;
}),
r
.tableCreate('usersThreads')
.run(conn)
.catch(err => {
console.log(err);
throw err;
}),
r
.tableCreate('usersNotifications')
.run(conn)
.catch(err => {
console.log(err);
throw err;
}),
])
// Add secondary indexes to new tables
.then(() =>
Promise.all([
r
.table('notifications')
.indexCreate('modifiedAt', r.row('modifiedAt'))
.run(conn)
.catch(err => {
console.log(err);
throw err;
}),
r
.table('usersThreads')
.indexCreate('userId')
.run(conn)
.catch(err => {
console.log(err);
throw err;
}),
r
.table('usersThreads')
.indexCreate('threadId')
.run(conn)
.catch(err => {
console.log(err);
throw err;
}),
r
.table('usersNotifications')
.indexCreate('userId')
.run(conn)
.catch(err => {
console.log(err);
throw err;
}),
r
.table('notifications')
.indexCreate('contextId', r.row('context')('id'))
.run(conn)
.catch(err => {
console.log(err);
throw err;
}),
r
.table('usersNotifications')
.indexCreate('notificationId', r.row('notificationId'))
.run(conn)
.catch(err => {
console.log(err);
throw err;
}),
])
)
// Insert data into new tables that should be there
.then(() =>
Promise.all([
// Thread creators -> usersThreads
r
.table('threads')
.filter(r.row.hasFields('deletedAt').not())
.run(conn)
.then(cursor => cursor.toArray())
.then(threads =>
Promise.all(
threads.map(thread =>
r
.table('usersThreads')
.insert({
createdAt: r.now(),
isParticipant: true,
receiveNotifications: true,
threadId: thread.id,
userId: thread.creatorId,
})
.run(conn)
.catch(err => {
console.log(err);
throw err;
})
)
)
)
.catch(err => {
console.log(err);
throw err;
}),
// Thread participants -> usersThreads
r
.table('threads')
.filter(r.row.hasFields('deletedAt').not())
.run(conn)
.catch(err => {
console.log(err);
throw err;
})
.then(cursor => cursor.toArray())
.then(threads =>
Promise.all(
threads.map(thread => {
return Promise.all([
r
.table('messages')
.filter({ threadId: thread.id })
.map(message => message('senderId'))
.run(conn)
.then(cursor => cursor.toArray())
.catch(err => {
console.log(err);
throw err;
}),
thread,
]);
})
)
)
.then(threadsMessages =>
Promise.all(
threadsMessages.map(([threadSenders, thread]) => {
if (!threadSenders) return Promise.resolve();
let uniqueThreadSenders = threadSenders.filter(
(item, i, ar) => ar.indexOf(item) === i
);
return Promise.all(
uniqueThreadSenders.map(sender => {
return r
.table('usersThreads')
.insert({
createdAt: r.now(),
isParticipant: true,
receiveNotifications: true,
threadId: thread.id,
userId: sender,
})
.run(conn)
.catch(err => {
console.log(err);
throw err;
});
})
);
})
)
),
])
)
.catch(err => {
console.log(err);
throw err;
})
);
};
exports.down = function(r, conn) {
return Promise.all([
r.tableDrop('usersThreads').run(conn),
r.tableDrop('usersNotifications').run(conn),
r.tableDrop('notifications').run(conn),
r
.table('usersCommunities')
.update({ receiveNotifications: r.literal() })
.run(conn),
r
.table('usersCommunities')
.update({ receiveNotifications: r.literal() })
.run(conn),
]);
};
|