forked from nocobase/nocobase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve sort field migration (nocobase#4112)
- Loading branch information
Showing
3 changed files
with
136 additions
and
90 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
...-collection-manager/src/server/__tests__/migrations/20240420105949-add-sort-field.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
90 changes: 0 additions & 90 deletions
90
...base/plugin-collection-manager/src/server/migrations/20240410214249-migrate-sort-field.ts
This file was deleted.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
...nocobase/plugin-collection-manager/src/server/migrations/20240420105949-add-sort-field.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
}); | ||
} | ||
} | ||
} |