-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only add indices if they don't exist anymore (syncing mechanism)
- Loading branch information
1 parent
4efb51f
commit 0a840b2
Showing
4 changed files
with
120 additions
and
25 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
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
"use strict"; | ||
var sequelize = require("sequelize"); | ||
var Utils = sequelize.Utils; | ||
|
||
function IndicesSyncer(sequelize, model, indices) { | ||
this.sequelize = sequelize; | ||
this.model = model; | ||
this.indices = indices; | ||
} | ||
|
||
/** | ||
* Checks whether syncing is necessary, then starts the | ||
* syncing process | ||
* @return {CustomEventEmitter} | ||
* @public | ||
*/ | ||
IndicesSyncer.prototype.sync = function() { | ||
var self = this; | ||
|
||
// No indices to sync, just pretend we're fine | ||
if (this.indices.length === 0) { | ||
return new Utils.CustomEventEmitter(function (emitter) { | ||
emitter.emit("success", self.model); | ||
}).run(); | ||
} | ||
|
||
// Fetch the current indices | ||
return new Utils.CustomEventEmitter(function (emitter) { | ||
self._fetchIndices(emitter); | ||
}).run(); | ||
}; | ||
|
||
/** | ||
* Fetches the currently existing indices, then syncs them | ||
* @param {CustomEventEmitter} emitter | ||
* @private | ||
*/ | ||
IndicesSyncer.prototype._fetchIndices = function(emitter) { | ||
var queryInterface = this.sequelize.queryInterface; | ||
var self = this; | ||
|
||
queryInterface.showIndex(this.model.tableName) | ||
.success(function (existingIndices) { | ||
self.existingIndices = existingIndices; | ||
|
||
self._syncIndices(emitter); | ||
}) | ||
.error(function (e) { | ||
emitter.emit("error", e); | ||
}); | ||
}; | ||
|
||
/** | ||
* Final step - sync the indices and pass the result of our | ||
* chain to the emitter | ||
* @param {CustomEventEmitter} emitter | ||
* @private | ||
*/ | ||
IndicesSyncer.prototype._syncIndices = function(emitter) { | ||
var chainer = new Utils.QueryChainer(); | ||
var queryInterface = this.sequelize.queryInterface; | ||
|
||
for (var i = 0, len = this.indices.length; i < len; i++) { | ||
var index = this.indices[i]; | ||
var indexName; | ||
|
||
// Build the index name | ||
// @TODO | ||
// Move the index name generation to sequelize maybe? | ||
if (index.options && index.options.indexName) { | ||
indexName = index.options.indexName; | ||
} else { | ||
indexName = Utils._.underscored(this.model.tableName + "_" + index.attributes.join("_")); | ||
} | ||
|
||
// Does the index already exist? | ||
var indexExists = | ||
this.existingIndices | ||
.filter(function (existingIndex) { | ||
return existingIndex.name === indexName; | ||
}).length > 0; | ||
|
||
if (!indexExists) { | ||
chainer.add(queryInterface.addIndex(this.model.tableName, index.attributes, index.options)); | ||
} | ||
} | ||
|
||
chainer | ||
.run() | ||
.proxy(emitter); | ||
}; | ||
|
||
module.exports = IndicesSyncer; |
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