-
-
Notifications
You must be signed in to change notification settings - Fork 58
Fir for Add sparse options for index #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 4 commits
79f2fdc
f6e47e7
ce222c6
7e79705
7b0f4ad
f71fdc3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -154,17 +154,29 @@ var MongodbDriver = Base.extend({ | |
* Adds an index to a collection | ||
* | ||
* @param collectionName - The collection to add the index to | ||
* @param indexName - The name of the index to add | ||
* @param columns - The columns to add an index on | ||
* @param unique - A boolean whether this creates a unique index | ||
* @param indexOptions - An object of options to be used as Index options | ||
* @param callback | ||
*/ | ||
addIndex: function(collectionName, indexName, columns, unique, callback) { | ||
|
||
var options = { | ||
indexName: indexName, | ||
columns: columns, | ||
unique: unique | ||
}; | ||
addIndex: function(collectionName, indexOptions, callback) { | ||
var options = {}; | ||
if (indexOptions.constructor.name == 'Object') { | ||
options = { | ||
name: indexOptions.name, | ||
columns: indexOptions.columns, | ||
unique: indexOptions.unique | ||
}; | ||
if (indexOptions.hasOwnProperty('sparse')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would be the benefit over There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, lets be a bit more specific: What is the default value of sparse? If it is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But https://docs.mongodb.com/manual/core/index-sparse/#create-a-sparse-index As |
||
options.sparse = indexOptions.sparse | ||
} | ||
} else { | ||
log.warn('addIndex can now accept Object for passing index Options.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove that log, this is an extension of the API not a deprecation though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To add, this information is more a target for documentation rather than adding some logs here :) That piece of information will go here https://github.com/db-migrate/english-docs/blob/master/API/NoSQL.md#addindexcollectionname-indexname-columns-unique-callback |
||
options = { | ||
name: arguments[1], | ||
columns: arguments[2], | ||
unique: arguments[3] | ||
}; | ||
callback = arguments[4]; | ||
} | ||
|
||
return this._run('createIndex', collectionName, options) | ||
.nodeify(callback); | ||
|
@@ -313,7 +325,7 @@ var MongodbDriver = Base.extend({ | |
db[command](collection, options.newCollection, callbackFunction); | ||
break; | ||
case 'createIndex': | ||
db[command](collection, options.columns, {name: options.indexName, unique: options.unique}, callbackFunction); | ||
db[command](collection, options.columns, options, callbackFunction); | ||
break; | ||
case 'dropIndex': | ||
db.collection(collection)[command](options.indexName, callbackFunction); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,7 +122,7 @@ vows.describe('mongodb').addBatch({ | |
return this.callback(err); | ||
} | ||
|
||
db.addIndex('event', 'event_title', 'title', false, this.callback); | ||
db.addIndex('event', { name: 'event_title', columns: 'title', unique: false, sparse: true }, this.callback); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
}.bind(this)); | ||
}, | ||
|
||
|
@@ -230,7 +230,7 @@ vows.describe('mongodb').addBatch({ | |
return this.callback(err); | ||
} | ||
|
||
db.addIndex('event', 'event_title', 'title', false, function(err, data) { | ||
db.addIndex('event', { name: 'event_title', columns: 'title', unique: false }, function(err, data) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Last thing missing here: You have deleted the old tests, but to actually cover the old and new functionality, the old and the new tests have to coexist, rather than one gets rewritten. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But these are internal calls made within the test case hence I changed them to use the updates style. I have also added a separate test case for addIndex for the previous style. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, this wasn't clear from the diff. Please don't adjust tests in the future and instead add directly new ones instead :) |
||
|
||
if(err) { | ||
return this.callback(err); | ||
|
@@ -299,7 +299,7 @@ vows.describe('mongodb').addBatch({ | |
return this.callback(err); | ||
} | ||
|
||
db.addIndex('event', 'event_title', 'title', false, function(err, data) { | ||
db.addIndex('event', { name: 'event_title', columns: 'title', unique: false }, function(err, data) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
|
||
if(err) { | ||
return this.callback(err); | ||
|
@@ -360,4 +360,51 @@ vows.describe('mongodb').addBatch({ | |
} | ||
} | ||
} | ||
}).export(module); | ||
}) | ||
.addBatch({ | ||
'addIndex-backward-compatibility': { | ||
topic: function() { | ||
db.createCollection('event', function(err, collection) { | ||
if(err) { | ||
return this.callback(err); | ||
} | ||
|
||
db.addIndex('event', 'event_title','title', false, this.callback); | ||
}.bind(this)); | ||
}, | ||
|
||
teardown: function() { | ||
db.dropCollection('event', this.callback); | ||
}, | ||
|
||
'preserves case': { | ||
topic: function() { | ||
db._getCollectionNames(this.callback); | ||
}, | ||
|
||
'of the functions original table': function(err, tables) { | ||
var index = 0; | ||
assert.isNotNull(tables); | ||
assert.equal(tables.length, 2); // Should be 2 b/c of the system collection | ||
|
||
if( tables[0].collectionName === 'system.indexes' ) | ||
index = 1; | ||
|
||
assert.equal(tables[index].collectionName, 'event'); | ||
} | ||
}, | ||
|
||
'has resulting index metadata': { | ||
topic: function() { | ||
db._getIndexes('event', this.callback); | ||
}, | ||
|
||
'with additional index': function(err, indexes) { | ||
assert.isDefined(indexes); | ||
assert.isNotNull(indexes); | ||
assert.include(indexes, 'event_title'); | ||
} | ||
} | ||
} | ||
}) | ||
.export(module); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any reason for not using
typeof
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also please always use type safe comparisions and only use non type safe where you really need to fallback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typeof []
is Object andtypeof {}
is also Object. Henceconstructor.name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not enough of a reason for me though.
[]
in javascript is an object just as{}
is, the only difference are some specifics, but even with those an array is still an object in javascript, though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, but for this case we expect the options to be of type
{}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still would go for
typeof
instead, or can you call out a case where a passed array would result in an unwanted behavior?