Add an optional models argument to automigrate/autoupdate
This commit is contained in:
parent
0a5c0ff77d
commit
d3bf5c20ac
|
@ -412,27 +412,39 @@ DataSource.prototype.defineProperty = function (model, prop, params) {
|
||||||
* Drop each model table and re-create.
|
* Drop each model table and re-create.
|
||||||
* This method make sense only for sql connectors.
|
* This method make sense only for sql connectors.
|
||||||
*
|
*
|
||||||
|
* @param {String} or {[String]} Models to be migrated, if not present, apply to all models
|
||||||
|
*
|
||||||
* @warning All data will be lost! Use autoupdate if you need your data.
|
* @warning All data will be lost! Use autoupdate if you need your data.
|
||||||
*/
|
*/
|
||||||
DataSource.prototype.automigrate = function (cb) {
|
DataSource.prototype.automigrate = function (models, cb) {
|
||||||
this.freeze();
|
this.freeze();
|
||||||
if (this.connector.automigrate) {
|
if (this.connector.automigrate) {
|
||||||
this.connector.automigrate(cb);
|
this.connector.automigrate(models, cb);
|
||||||
} else if (cb) {
|
} else {
|
||||||
cb();
|
if ((!cb) && ('function' === typeof models)) {
|
||||||
|
cb = models;
|
||||||
|
models = undefined;
|
||||||
|
}
|
||||||
|
cb && process.nextTick(cb);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update existing database tables.
|
* Update existing database tables.
|
||||||
* This method make sense only for sql connectors.
|
* This method make sense only for sql connectors.
|
||||||
|
*
|
||||||
|
* @param {String} or {[String]} Models to be migrated, if not present, apply to all models
|
||||||
*/
|
*/
|
||||||
DataSource.prototype.autoupdate = function (cb) {
|
DataSource.prototype.autoupdate = function (models, cb) {
|
||||||
this.freeze();
|
this.freeze();
|
||||||
if (this.connector.autoupdate) {
|
if (this.connector.autoupdate) {
|
||||||
this.connector.autoupdate(cb);
|
this.connector.autoupdate(models, cb);
|
||||||
} else if (cb) {
|
} else {
|
||||||
cb();
|
if ((!cb) && ('function' === typeof models)) {
|
||||||
|
cb = models;
|
||||||
|
models = undefined;
|
||||||
|
}
|
||||||
|
cb && process.nextTick(cb);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
23
lib/sql.js
23
lib/sql.js
|
@ -267,11 +267,27 @@ BaseSQL.prototype.disconnect = function disconnect() {
|
||||||
this.client.end();
|
this.client.end();
|
||||||
};
|
};
|
||||||
|
|
||||||
BaseSQL.prototype.automigrate = function (cb) {
|
/**
|
||||||
|
* Recreate the tables for the given models
|
||||||
|
* @param models A model name or an array of model names, if not present, apply to all models defined in the connector
|
||||||
|
* @param cb Callback function
|
||||||
|
*/
|
||||||
|
BaseSQL.prototype.automigrate = function (models, cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var wait = 0;
|
var wait = 0;
|
||||||
Object.keys(this._models).forEach(function (model) {
|
if ((!cb) && ('function' === typeof models)) {
|
||||||
wait += 1;
|
cb = models;
|
||||||
|
models = undefined;
|
||||||
|
}
|
||||||
|
// First argument is a model name
|
||||||
|
if ('string' === typeof models) {
|
||||||
|
models = [models];
|
||||||
|
}
|
||||||
|
|
||||||
|
models = models || Object.keys(this._models);
|
||||||
|
models.forEach(function (model) {
|
||||||
|
if (model in self._models) {
|
||||||
|
wait++;
|
||||||
self.dropTable(model, function () {
|
self.dropTable(model, function () {
|
||||||
// console.log('drop', model);
|
// console.log('drop', model);
|
||||||
self.createTable(model, function (err) {
|
self.createTable(model, function (err) {
|
||||||
|
@ -280,6 +296,7 @@ BaseSQL.prototype.automigrate = function (cb) {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
if (wait === 0) cb();
|
if (wait === 0) cb();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue