Skip to content

Commit

Permalink
fix: improve sort field migration (nocobase#4112)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenos authored Apr 20, 2024
1 parent 52893e2 commit 8f82954
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { MockServer, createMockServer } from '@nocobase/test';
import Migration from '../../migrations/20240420105949-add-sort-field';
import PluginCollectionManagerServer from '../../server';

describe('nocobase-admin-menu', () => {
let app: MockServer;

beforeEach(async () => {
app = await createMockServer({
plugins: ['nocobase'],
});
await app.version.update('0.21.0-alpha.12');
});

afterEach(async () => {
await app.destroy();
});

test('migration', async () => {
await app.db.getRepository('collections').create({
values: {
autoGenId: true,
sortable: true,
name: 'foo',
template: 'general',
view: false,
fields: [
{
type: 'string',
name: 'status',
},
{
type: 'sort',
name: 'sort1',
scopeKey: 'status',
},
{
type: 'sort',
name: 'sort3',
interface: 'sort3',
uiSchema: {
type: 'number',
title: 'Sort 3',
'x-component': 'InputNumber',
'x-component-props': { stringMode: true, step: '1' },
'x-validator': 'integer',
},
},
],
},
context: {},
});
const migration = new Migration({
db: app.db,
// @ts-ignore
app: app,
plugin: app.pm.get(PluginCollectionManagerServer),
});
await migration.up();
const sort1 = await app.db.getRepository('fields').findOne({
filter: {
collectionName: 'foo',
name: 'sort',
},
});
expect(sort1.interface).toBe('sort');
expect(sort1.hidden).toBeFalsy();
const sort2 = await app.db.getRepository('fields').findOne({
filter: {
collectionName: 'foo',
name: 'sort1',
},
});
expect(sort2.interface).toBe('sort');
expect(sort2.hidden).toBeFalsy();
const sort3 = await app.db.getRepository('fields').findOne({
filter: {
collectionName: 'foo',
name: 'sort3',
},
});
expect(sort3.get('uiSchema.title')).toBe('Sort 3');
});
});

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Migration } from '@nocobase/server';
import { CollectionRepository } from '../repositories';

export default class extends Migration {
on = 'afterLoad'; // 'beforeLoad' or 'afterLoad'
appVersion = '<0.21.0-alpha.13';

async up() {
const repository = this.db.getRepository<CollectionRepository>('collections');
await repository.load();
const collections = await this.db.getRepository('collections').find();
const fields = [];
for (const item of collections) {
const collection = this.db.getCollection(item.name);
collection.forEachField((field) => {
if (field.type === 'sort') {
fields.push({
collectionName: item.name,
name: field.name,
});
}
});
}
const fieldRepository = this.db.getRepository('fields');
for (const field of fields) {
this.app.log.info(`field path: ${field.collectionName}.${field.name}`);
const instance = await fieldRepository.findOne({
filter: field,
});
if (instance?.interface) {
continue;
}
await fieldRepository.updateOrCreate({
values: {
...field,
interface: 'sort',
type: 'sort',
hidden: false,
uiSchema: {
type: 'number',
title: field.name,
'x-component': 'InputNumber',
'x-component-props': { stringMode: true, step: '1' },
'x-validator': 'integer',
},
scopeKey: instance?.scopeKey,
},
filterKeys: ['collectionName', 'name'],
});
}
}
}

0 comments on commit 8f82954

Please sign in to comment.