From ec3481fbcc41b5ee79aaf82f70bf04bfc79b98c7 Mon Sep 17 00:00:00 2001 From: Rodrigo Nascimento Date: Thu, 4 Jun 2015 20:58:16 -0300 Subject: [PATCH] Init migrations --- server/startup/indexes.coffee | 18 ++++++++++++------ server/startup/migrations/v4.coffee | 28 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 server/startup/migrations/v4.coffee diff --git a/server/startup/indexes.coffee b/server/startup/indexes.coffee index bae065df2d5..696227f618e 100644 --- a/server/startup/indexes.coffee +++ b/server/startup/indexes.coffee @@ -1,8 +1,14 @@ Meteor.startup -> Meteor.defer -> - ChatMessage._ensureIndex({ 'expireAt': 1 }, { expireAfterSeconds: 0 }) - ChatMessage._ensureIndex({ 'rid': 1 }) - ChatMessage._ensureIndex({ 'rid': 1, 'ts': 1 }) - ChatSubscription._ensureIndex({ 'u._id': 1 }) - ChatSubscription._ensureIndex({ 'ts': 1 }) - ChatSubscription._ensureIndex({ 'rid': 1, 'u._id': 1 }, {unique: true}) + try ChatRoom._ensureIndex { 'name': 1 }, { unique: 1, sparse: 1 } catch e then console.log e + try ChatRoom._ensureIndex { 'u._id': 1 } catch e then console.log e + + try ChatSubscription._ensureIndex { 'rid': 1, 'u._id': 1 }, { unique: 1 } catch e then console.log e + try ChatSubscription._ensureIndex { 'u._id': 1, 'name': 1, 't': 1 }, { unique: 1 } catch e then console.log e + try ChatSubscription._ensureIndex { 'open': 1 } catch e then console.log e + try ChatSubscription._ensureIndex { 'alert': 1 } catch e then console.log e + try ChatSubscription._ensureIndex { 'unread': 1 } catch e then console.log e + try ChatSubscription._ensureIndex { 'ts': 1 } catch e then console.log e + + try ChatMessage._ensureIndex { 'rid': 1, 'ts': 1 } catch e then console.log e + try ChatMessage._ensureIndex { 'expireAt': 1 }, { expireAfterSeconds: 0 } catch e then console.log e diff --git a/server/startup/migrations/v4.coffee b/server/startup/migrations/v4.coffee new file mode 100644 index 00000000000..5fa0f4561b9 --- /dev/null +++ b/server/startup/migrations/v4.coffee @@ -0,0 +1,28 @@ +# Meteor.startup -> +# Migrations.add +# version: 4 +# up: -> + + try ChatMessage._dropIndex 'rid_1' + try ChatSubscription._dropIndex 'u._id_1' + + + console.log 'Fixing ChatSubscription uid' + ChatSubscription.update({rn: {$exists: true}}, {$rename: {rn: 'name'}}, {multi: true}) + + ChatRoom.find({name: ''}).forEach (item) -> + name = Random.id().toLowerCase() + ChatRoom.update item._id, {$set: {name: name}} + ChatSubscription.update {rid: item._id}, {$set: {name: name}}, {multi: true} + + ChatRoom.find().forEach (room) -> + ChatRoom.find({name: room.name, _id: {$ne: room._id}}).forEach (item) -> + name = room.name + '-' + Random.id(2).toLowerCase() + ChatRoom.update item._id, {$set: {name: name}} + ChatSubscription.update {rid: item._id}, {$set: {name: name}}, {multi: true} + + ChatSubscription.find().forEach (item) -> + ChatSubscription.find({'u._id': item.u._id, name: item.name, t: item.t, _id: {$ne: item._id}}).forEach (subItem) -> + ChatSubscription.delete subItem._id + + console.log 'End'