Skip to content

Commit

Permalink
feat: 改成scope扩展性更强
Browse files Browse the repository at this point in the history
  • Loading branch information
caizx committed Jul 20, 2020
1 parent 1cd0d51 commit c67a699
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
15 changes: 7 additions & 8 deletions lib/Migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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++;
Expand All @@ -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;
Expand All @@ -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
]);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@saas-plat/metaquery",
"version": "1.0.39",
"version": "1.0.40",
"description": "通过元数据定义查询对象提供数据服务",
"main": "lib/index.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions test/Migration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' ;
Expand Down Expand Up @@ -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';
Expand Down

0 comments on commit c67a699

Please sign in to comment.