Skip to content

Commit

Permalink
Add unique index to userdevices to prevent duplicate device registrat… (
Browse files Browse the repository at this point in the history
#196)

* Add unique index to userdevices to prevent duplicate device registrations

This also requires a manual effort when upgrading to remove duplicated
entries, until the unique index can be enforced by the database.

Fixes #195

Signed-off-by: Florian Schmidt <florian.schmidt.welzow@t-online.de>

* Also check for duplicate androidregistrationid and iosdevicetoken

Signed-off-by: Florian Schmidt <florian.schmidt.welzow@t-online.de>
  • Loading branch information
FlorianSW authored and digitaldan committed Sep 16, 2018
1 parent cea8d8f commit 81a414e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -477,3 +477,14 @@ http://YOUR-AWS-EC2-PUBLIC-DNS
```

You should be ready with your openHAB Cloud installation!

# Release-Notes
## 1.0.5
* When upgrading from older versions, please run the `./scripts/deleteDuplicateUserDevices.js`
script, start openhab-cloud once (and shut it down again) and then execute the following
statement in your MongoDB collection:
```
use <YOUR_DB>
db.userdevices.reIndex()
```
This is necessary to ensure a unique index on the collection.
6 changes: 3 additions & 3 deletions models/userdevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ var mongoose = require('mongoose'),

var UserDeviceSchema = new Schema({
owner: {type: ObjectId, required: true},
androidRegistration: {type: String},
iosDeviceToken: {type: String},
androidRegistration: {type: String, unique: true},
iosDeviceToken: {type: String, unique: true},
deviceType: {type: String},
deviceModel: {type: String},
deviceId: {type: String},
deviceId: {type: String, unique: true},
globalLocation: {type: [Number], index: '2d'},
globalAltitude: {type: Number},
globalAccuracy: {type: Number},
Expand Down
48 changes: 48 additions & 0 deletions scripts/deleteDuplicateUserDevices.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const mongoose = require('mongoose'),
logger = require('../logger.js'),
config = require('../config.json'),
UserDevice = require('../models/userdevice'),
system = require('../system'),
MongoConnect = require('../system/mongoconnect');

system.setConfiguration(config);

const mongoConnect = new MongoConnect(system);
mongoConnect.connect(mongoose);

logger.info('Looking for all registered devices...');
function deleteDuplicateUserDevices(err, devices) {
if (err) {
logger.error('Could not load all devices to fix duplicates.', err);
}

logger.info('Found ' + devices.length + ' devices...');
const alreadyRegisteredDeviceId = [];
const alreadyRegisteredAndroidRegistration = [];
const alreadyRegisteredIOSToken = [];
devices.forEach(function(device) {
if (
!alreadyRegisteredDeviceId.includes(device.deviceId) &&
!alreadyRegisteredAndroidRegistration.includes(device.androidRegistration) &&
!alreadyRegisteredIOSToken.includes(device.iosDeviceToken)
) {
alreadyRegisteredDeviceId.push(device.deviceId);
alreadyRegisteredAndroidRegistration.push(device.androidRegistration);
alreadyRegisteredIOSToken.push(device.iosDeviceToken);
return;
}

logger.info('Remove duplicated device with deviceId ' + device.deviceId + ' and ID ' + device.id);
device.remove();
});

if (alreadyRegisteredDeviceId.length === devices.length) {
logger.info('No duplicated devices found.');
} else {
logger.info('All duplicated devices should be removed now.');
}

process.exit(0);
}

UserDevice.find({}, deleteDuplicateUserDevices);

0 comments on commit 81a414e

Please sign in to comment.