forked from parse-community/parse-server
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
index on unique-indexes: c454180 Revert "Log objects rather than JSON…
… stringified objects (parse-community#1922)"
- Loading branch information
1 parent
c454180
commit 8342f3d
Showing
7 changed files
with
149 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ let defaultColumns = require('../src/Controllers/SchemaController').defaultColum | |
const requiredUserFields = { fields: Object.assign({}, defaultColumns._Default, defaultColumns._User) }; | ||
|
||
|
||
describe('miscellaneous', function() { | ||
fdescribe('miscellaneous', function() { | ||
it('create a GameScore object', function(done) { | ||
var obj = new Parse.Object('GameScore'); | ||
obj.set('score', 1337); | ||
|
@@ -80,44 +80,53 @@ describe('miscellaneous', function() { | |
.catch(done); | ||
}); | ||
|
||
it('ensure that email is uniquely indexed', done => { | ||
let numCreated = 0; | ||
let numFailed = 0; | ||
|
||
let user1 = new Parse.User(); | ||
user1.setPassword('asdf'); | ||
user1.setUsername('u1'); | ||
user1.setEmail('[email protected]'); | ||
let p1 = user1.signUp(); | ||
p1.then(user => { | ||
numCreated++; | ||
expect(numCreated).toEqual(1); | ||
}, error => { | ||
numFailed++; | ||
expect(numFailed).toEqual(1); | ||
expect(error.code).toEqual(Parse.Error.EMAIL_TAKEN); | ||
}); | ||
fit('ensure that email is uniquely indexed', done => { | ||
DatabaseAdapter._indexBuildsCompleted('test') | ||
.then(() => { | ||
let numCreated = 0; | ||
let numFailed = 0; | ||
|
||
let user1 = new Parse.User(); | ||
user1.setPassword('asdf'); | ||
user1.setUsername('u1'); | ||
user1.setEmail('[email protected]'); | ||
let p1 = user1.signUp(); | ||
p1.then(user => { | ||
numCreated++; | ||
expect(numCreated).toEqual(1); | ||
}, error => { | ||
numFailed++; | ||
console.log(error); | ||
expect(numFailed).toEqual(1); | ||
expect(error.code).toEqual(Parse.Error.EMAIL_TAKEN); | ||
}); | ||
|
||
let user2 = new Parse.User(); | ||
user2.setPassword('asdf'); | ||
user2.setUsername('u2'); | ||
user2.setEmail('[email protected]'); | ||
let p2 = user2.signUp(); | ||
p2.then(user => { | ||
numCreated++; | ||
expect(numCreated).toEqual(1); | ||
}, error => { | ||
numFailed++; | ||
expect(numFailed).toEqual(1); | ||
expect(error.code).toEqual(Parse.Error.EMAIL_TAKEN); | ||
}); | ||
let user2 = new Parse.User(); | ||
user2.setPassword('asdf'); | ||
user2.setUsername('u2'); | ||
user2.setEmail('[email protected]'); | ||
let p2 = user2.signUp(); | ||
p2.then(user => { | ||
numCreated++; | ||
expect(numCreated).toEqual(1); | ||
}, error => { | ||
numFailed++; | ||
console.log(error); | ||
expect(numFailed).toEqual(1); | ||
expect(error.code).toEqual(Parse.Error.EMAIL_TAKEN); | ||
}); | ||
|
||
Parse.Promise.all([p1, p2]) | ||
.then(() => { | ||
fail('one of the users should not have been created'); | ||
done(); | ||
Parse.Promise.all([p1, p2]) | ||
.then(() => { | ||
fail('one of the users should not have been created'); | ||
done(); | ||
}) | ||
.catch(done); | ||
}) | ||
.catch(done); | ||
.catch(error => { | ||
fail('index build failed') | ||
done(); | ||
}); | ||
}); | ||
|
||
it('ensure that if people already have duplicate users, they can still sign up new users', done => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,10 +17,16 @@ | |
|
||
import DatabaseController from './Controllers/DatabaseController'; | ||
import MongoStorageAdapter from './Adapters/Storage/Mongo/MongoStorageAdapter'; | ||
import log from './logger'; | ||
|
||
var SchemaController = require('./Controllers/SchemaController'); | ||
|
||
let dbConnections = {}; | ||
let appDatabaseURIs = {}; | ||
let appDatabaseOptions = {}; | ||
let indexBuildCreationPromises = {}; | ||
|
||
const requiredUserFields = { fields: { ...SchemaController.defaultColumns._Default, ...SchemaController.defaultColumns._User } }; | ||
|
||
function setAppDatabaseURI(appId, uri) { | ||
appDatabaseURIs[appId] = uri; | ||
|
@@ -49,6 +55,11 @@ function destroyAllDataPermanently() { | |
throw 'Only supported in test environment'; | ||
} | ||
|
||
//Super janky. Will be removed in a later PR. | ||
function _indexBuildsCompleted(appId) { | ||
return indexBuildCreationPromises[appId]; | ||
} | ||
|
||
function getDatabaseConnection(appId: string, collectionPrefix: string) { | ||
if (dbConnections[appId]) { | ||
return dbConnections[appId]; | ||
|
@@ -62,6 +73,57 @@ function getDatabaseConnection(appId: string, collectionPrefix: string) { | |
|
||
dbConnections[appId] = new DatabaseController(new MongoStorageAdapter(mongoAdapterOptions), {appId: appId}); | ||
|
||
// Kick off unique index build in the background (or ensure the unique index already exists) | ||
// A bit janky, will be fixed in a later PR. | ||
let p1 = dbConnections[appId].adapter.ensureUniqueness('_User', ['username'], requiredUserFields) | ||
.catch(error => { | ||
log.warn('Unable to ensure uniqueness for usernames: ', error); | ||
return Promise.reject(); | ||
}); | ||
|
||
let p2 = dbConnections[appId].adapter.ensureUniqueness('_User', ['email'], requiredUserFields) | ||
.catch(error => { | ||
log.warn('Unabled to ensure uniqueness for user email addresses: ', error); | ||
return Promise.reject(); | ||
}) | ||
|
||
indexBuildCreationPromises[appId] = p1.then(() => p2) | ||
.then(() => console.log('index build success')) | ||
.then(() => { | ||
let numCreated = 0; | ||
let numFailed = 0; | ||
|
||
let user1 = new Parse.User(); | ||
user1.setPassword('asdf'); | ||
user1.setUsername('u1'); | ||
user1.setEmail('[email protected]'); | ||
let p1 = user1.signUp(); | ||
p1.then(user => { | ||
numCreated++; | ||
console.log(numCreated) | ||
}, error => { | ||
numFailed++; | ||
console.log(error); | ||
console.log(numFailed) | ||
console.log(error.code) | ||
}); | ||
|
||
let user2 = new Parse.User(); | ||
user2.setPassword('asdf'); | ||
user2.setUsername('u2'); | ||
user2.setEmail('[email protected]'); | ||
let p2 = user2.signUp(); | ||
p2.then(user => { | ||
numCreated++; | ||
console.log(numCreated) | ||
}, error => { | ||
numFailed++; | ||
console.log(error); | ||
console.log(numFailed) | ||
console.log(error.code) | ||
}); | ||
}) | ||
|
||
return dbConnections[appId]; | ||
} | ||
|
||
|
@@ -71,4 +133,5 @@ module.exports = { | |
setAppDatabaseURI: setAppDatabaseURI, | ||
clearDatabaseSettings: clearDatabaseSettings, | ||
destroyAllDataPermanently: destroyAllDataPermanently, | ||
_indexBuildsCompleted, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters