fix: add transaction to migrate options

This commit is contained in:
chenos 2020-12-08 14:35:50 +08:00
parent 276d218357
commit a44bab62fc
6 changed files with 19 additions and 9 deletions

View File

@ -2,6 +2,11 @@ import CollectionModel from '../models/collection';
const defaultValues = {
actions: [
{
type: 'filter',
name: 'filter',
title: '筛选',
},
{
type: 'list',
name: 'list',
@ -55,7 +60,7 @@ const defaultValues = {
name: 'table',
title: '列表',
template: 'Table',
actionNames: ['create', 'destroy'],
actionNames: ['filter', 'create', 'destroy'],
default: true,
},
],
@ -73,7 +78,7 @@ const defaultValues = {
export default async function (model: CollectionModel, options: any = {}) {
const { migrate = true } = options;
if (migrate) {
await model.migrate();
await model.migrate(options);
}
await model.updateAssociations(defaultValues, options);
}

View File

@ -14,7 +14,7 @@ export default async function (options: any = {}) {
transaction,
});
for (const field of fields) {
await field.migrate();
await field.migrate(options);
}
}
}

View File

@ -3,6 +3,6 @@ import FieldModel from '../models/field';
export default async function (model: FieldModel, options: any = {}) {
const { migrate = true } = options;
if (migrate) {
await model.migrate();
await model.migrate(options);
}
}

View File

@ -10,6 +10,7 @@ export const string = {
options: {
interface: 'string',
type: 'string',
filterable: true,
component: {
type: 'string',
},

View File

@ -74,6 +74,7 @@ export class CollectionModel extends BaseModel {
}
if (associationTableNames.length) {
await CollectionModel.load({
...opts,
where: {
name: {
[Op.in]: associationTableNames,
@ -88,7 +89,7 @@ export class CollectionModel extends BaseModel {
*
*/
async migrate(options: MigrateOptions = {}) {
const table = await this.loadTableOptions();
const table = await this.loadTableOptions(options);
return await table.sync({
force: false,
alter: {
@ -121,12 +122,14 @@ export class CollectionModel extends BaseModel {
* @param options
*/
static async load(options: LoadOptions = {}) {
const { reset = false, where = {} } = options;
const { reset = false, where = {}, transaction } = options;
const collections = await this.findAll({
transaction,
where,
});
for (const collection of collections) {
await collection.loadTableOptions({
transaction,
reset,
});
}

View File

@ -35,11 +35,12 @@ export class FieldModel extends BaseModel {
};
}
async migrate() {
if (!this.get('collection_name')) {
async migrate(options: any = {}) {
const collectionName = this.get('collection_name');
if (!collectionName) {
return false;
}
const table = this.database.getTable(this.get('collection_name'));
const table = this.database.getTable(collectionName);
table.addField(await this.getOptions());
await table.sync({
force: false,