fix: sub table field migrate after update associations

This commit is contained in:
chenos 2020-12-21 14:15:58 +08:00
parent 93d8426c84
commit e0f10db125
4 changed files with 36 additions and 49 deletions

View File

@ -2,14 +2,25 @@ import FieldModel from '../models/field';
export default async function (model: FieldModel, options: any = {}) { export default async function (model: FieldModel, options: any = {}) {
const { migrate = true } = options; const { migrate = true } = options;
const Collection = model.database.getModel('collections');
if (model.get('interface') === 'subTable') {
const target = model.get('target');
if (target) {
await Collection.import({
name: target,
internal: true,
developerMode: true,
}, options);
}
}
if (migrate) { if (migrate) {
await model.migrate(options); await model.migrate(options);
} }
if (model.get('collection_name') && model.get('parent_id')) { // if (model.get('collection_name') && model.get('parent_id')) {
const parent = await model.getParent({ // const parent = await model.getParent({
...options, // ...options,
}); // });
const Collection = model.database.getModel('collections'); // const Collection = model.database.getModel('collections');
await Collection.load({...options, where: {name: parent.get('collection_name')}}); // await Collection.load({...options, where: {name: parent.get('collection_name')}});
} // }
} }

View File

@ -5,12 +5,11 @@ export default async function (model: FieldModel, options: any = {}) {
if (migrate) { if (migrate) {
await model.migrate(options); await model.migrate(options);
} }
if (model.get('collection_name') && model.get('parent_id')) { // if (model.get('collection_name') && model.get('parent_id')) {
const parent = await model.getParent({ // const parent = await model.getParent({
...options, // ...options,
}); // });
const Collection = model.database.getModel('collections'); // const Collection = model.database.getModel('collections');
console.log('fields-after-update', parent.get('collection_name')); // await Collection.load({...options, where: {name: parent.get('collection_name')}});
await Collection.load({...options, where: {name: parent.get('collection_name')}}); // }
}
} }

View File

@ -4,26 +4,6 @@ import _ from 'lodash';
export default async function (model: FieldModel, options) { export default async function (model: FieldModel, options) {
// 生成随机 name 要放最后 // 生成随机 name 要放最后
// model.generateNameIfNull(); // model.generateNameIfNull();
const Collection = model.database.getModel('collections');
if (model.get('interface') === 'subTable') {
const target = model.get('target');
if (target) {
const collection = await Collection.findOne({
...options,
where: {
name: target,
},
});
if (!collection) {
await Collection.create({
name: target,
internal: true,
developerMode: true,
}, options);
}
await Collection.load({...options, where: {name: model.get('name')}})
}
}
// 如果 collection_name 不存在 // 如果 collection_name 不存在
if (!model.get('collection_name') && model.get('parent_id')) { if (!model.get('collection_name') && model.get('parent_id')) {
const parent = await model.getParent({ const parent = await model.getParent({
@ -31,20 +11,6 @@ export default async function (model: FieldModel, options) {
}); });
const target = parent.get('target'); const target = parent.get('target');
if (target) { if (target) {
const collection = await Collection.findOne({
...options,
where: {
name: target,
},
});
if (!collection) {
await Collection.create({
name: target,
internal: true,
developerMode: true,
}, options);
}
await Collection.load({...options, where: {name: parent.get('name')}})
model.set('collection_name', target); model.set('collection_name', target);
} }
} }

View File

@ -14,6 +14,17 @@ export default async function (this: Application, options = {}) {
directory: path.resolve(__dirname, 'collections'), directory: path.resolve(__dirname, 'collections'),
}); });
database.addHook('afterUpdateAssociations', async function(model, options) {
if (model instanceof models.FieldModel) {
if (model.get('interface') === 'subTable') {
const { migrate = true } = options;
const Collection = model.database.getModel('collections');
await Collection.load({...options, where: {name: model.get('collection_name')}});
migrate && await model.migrate(options);
}
}
});
Object.keys(hooks).forEach(modelName => { Object.keys(hooks).forEach(modelName => {
const Model = database.getModel(modelName); const Model = database.getModel(modelName);
Object.keys(hooks[modelName]).forEach(hookKey => { Object.keys(hooks[modelName]).forEach(hookKey => {