diff --git a/lib/Migration.js b/lib/Migration.js index d050820..b8d62ac 100644 --- a/lib/Migration.js +++ b/lib/Migration.js @@ -117,15 +117,15 @@ exports.Migration = class Migration { } } - async up(fromVersion, toVersion) { + async up(scope) { this.upCounter = 0; for (const tableType of this.migrationTypes) { debug('start %s data migration...', tableType.name); - await this._migrationTables(tableType, fromVersion, toVersion); + await this._migrationTables(tableType, scope); } } - async _migrationTables(model, fromVersion, toVersion) { + async _migrationTables(model, scope) { const actions = [model.schema.get('name') + '.migrate', model.schema.get('schema').type + '.migrate', 'migrate' @@ -149,7 +149,7 @@ exports.Migration = class Migration { const NewModel = this.newTypes.find(type => type.name === model.name); if (!NewModel) { // 执行自定义规则 - await this._migrateAction(actions, oldData, fromVersion, toVersion); + await this._migrateAction(actions, oldData, scope); debug('data delete...', doc._id); await doc.remove(); this.upCounter++; @@ -172,7 +172,7 @@ exports.Migration = class Migration { const upDoc = await NewModel.findById(doc._id); //debug(doc._id,NewModel.collection.name,doc.collection.name) // 执行自定义规则 - await this._migrateAction(actions, oldData, fromVersion, toVersion, upDoc); + await this._migrateAction(actions, oldData, scope, upDoc); // 需要更新 if (upDoc.isModified()) { isUpdated = true; @@ -191,15 +191,14 @@ exports.Migration = class Migration { this._onAction = handle; } - async _migrateAction(actions, data, fromVersion, toVersion, doc) { + async _migrateAction(actions, data, scope, doc) { if (!this._onAction) { return data; } await this._onAction([ ...actions.map(action => new Action(action, data, { - from: fromVersion, - to: toVersion + ...scope })), doc ]); diff --git a/package.json b/package.json index 5c4509c..490f034 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@saas-plat/metaquery", - "version": "1.0.39", + "version": "1.0.40", "description": "通过元数据定义查询对象提供数据服务", "main": "lib/index.js", "scripts": { diff --git a/test/Migration.test.js b/test/Migration.test.js index 6d6ec6e..f33b5a4 100644 --- a/test/Migration.test.js +++ b/test/Migration.test.js @@ -114,7 +114,7 @@ describe('数据迁移', () => { } } }) - await migration.up('v1','v2'); + await migration.up({from:'v1',to:'v2'}); // [`rule update_sciprt1{ // when{ // e: Action e.name == 'DataTable2.migrate' ; @@ -198,7 +198,7 @@ describe('数据迁移', () => { datamigration.onAction(()=>{ throw 'error' }) - await datamigration.up('v1','v2'); + await datamigration.up({from:'v1',to:'v2'}); // [`rule update_sciprt1{ // when{ // e: Action e.name == 'DataTable2.migrate';