mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-07 22:49:26 +08:00
chore: migrate old version unixtimestamp fields (#5967)
* chore: migrate old version unixtimestamp fields * chore: migration * fix: test
This commit is contained in:
parent
20a4e14d4b
commit
98fb2063bc
@ -0,0 +1,101 @@
|
||||
import { Database, MigrationContext } from '@nocobase/database';
|
||||
import { MockServer } from '@nocobase/test';
|
||||
import { createApp } from '..';
|
||||
import Migrator from '../../migrations/20241230000001-update-bigint-to-unixtimestamp';
|
||||
|
||||
describe('update bigint to unixTimestamp', () => {
|
||||
let app: MockServer;
|
||||
let db: Database;
|
||||
|
||||
beforeEach(async () => {
|
||||
app = await createApp();
|
||||
db = app.db;
|
||||
await app.db.sync();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await app.destroy();
|
||||
});
|
||||
|
||||
it('should update bigInt fields with unixTimestamp interface', async () => {
|
||||
const collectionsRepository = db.getRepository('collections');
|
||||
const fieldsRepository = db.getRepository('fields');
|
||||
|
||||
// Create a test collection
|
||||
await collectionsRepository.create({
|
||||
values: {
|
||||
name: 'test',
|
||||
title: 'Test Collection',
|
||||
},
|
||||
});
|
||||
|
||||
const oldUiSchema = {
|
||||
'x-component-props': {
|
||||
accuracy: 'second',
|
||||
showTime: true,
|
||||
},
|
||||
type: 'number',
|
||||
'x-component': 'UnixTimestamp',
|
||||
title: 'unix-time',
|
||||
};
|
||||
|
||||
// Create test fields
|
||||
await fieldsRepository.create({
|
||||
values: {
|
||||
collectionName: 'test',
|
||||
type: 'bigInt',
|
||||
name: 'timestamp_field',
|
||||
interface: 'unixTimestamp',
|
||||
uiSchema: oldUiSchema,
|
||||
},
|
||||
});
|
||||
|
||||
// Create a control field that should not be updated
|
||||
await fieldsRepository.create({
|
||||
values: {
|
||||
collectionName: 'test',
|
||||
type: 'bigInt',
|
||||
name: 'normal_bigint',
|
||||
interface: 'integer',
|
||||
},
|
||||
});
|
||||
|
||||
// Run the migration
|
||||
const migration = new Migrator({ db } as MigrationContext);
|
||||
migration.context.app = app;
|
||||
await migration.up();
|
||||
|
||||
// Verify the results
|
||||
const updatedField = await fieldsRepository.findOne({
|
||||
filter: {
|
||||
name: 'timestamp_field',
|
||||
collectionName: 'test',
|
||||
},
|
||||
});
|
||||
|
||||
const controlField = await fieldsRepository.findOne({
|
||||
filter: {
|
||||
name: 'normal_bigint',
|
||||
collectionName: 'test',
|
||||
},
|
||||
});
|
||||
|
||||
// Check if the target field was updated
|
||||
expect(updatedField.get('type')).toBe('unixTimestamp');
|
||||
|
||||
// Check if the uiSchema was updated correctly
|
||||
const updatedUiSchema = updatedField.get('uiSchema');
|
||||
expect(updatedUiSchema).toMatchObject({
|
||||
...oldUiSchema,
|
||||
'x-component-props': {
|
||||
...oldUiSchema['x-component-props'],
|
||||
showTime: true,
|
||||
dateFormat: 'YYYY-MM-DD',
|
||||
timeFormat: 'HH:mm:ss',
|
||||
},
|
||||
});
|
||||
|
||||
// Check if the control field remained unchanged
|
||||
expect(controlField.get('type')).toBe('bigInt');
|
||||
});
|
||||
});
|
@ -0,0 +1,43 @@
|
||||
/**
|
||||
* This file is part of the NocoBase (R) project.
|
||||
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
||||
* Authors: NocoBase Team.
|
||||
*
|
||||
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
||||
* For more information, please refer to: https://www.nocobase.com/agreement.
|
||||
*/
|
||||
|
||||
/* istanbul ignore file -- @preserve */
|
||||
|
||||
import { Migration } from '@nocobase/server';
|
||||
|
||||
export default class extends Migration {
|
||||
on = 'afterLoad';
|
||||
|
||||
async up() {
|
||||
const Field = this.context.db.getRepository('fields');
|
||||
const fields = await Field.find({
|
||||
filter: {
|
||||
interface: 'unixTimestamp',
|
||||
type: 'bigInt',
|
||||
},
|
||||
});
|
||||
|
||||
for (const field of fields) {
|
||||
// Update field type to unixTimestamp
|
||||
const uiSchema = field.get('uiSchema');
|
||||
|
||||
uiSchema['x-component-props'] = {
|
||||
picker: 'date',
|
||||
dateFormat: 'YYYY-MM-DD',
|
||||
timeFormat: 'HH:mm:ss',
|
||||
showTime: uiSchema['x-component-props']?.showTime || true,
|
||||
accuracy: uiSchema['x-component-props']?.accuracy || 'second',
|
||||
};
|
||||
|
||||
field.set('type', 'unixTimestamp');
|
||||
field.set('uiSchema', uiSchema);
|
||||
await field.save();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user