diff --git a/packages/plugins/@nocobase/plugin-data-source-main/src/server/__tests__/migrations/update-bigint-to-unixtimestamp.test.ts b/packages/plugins/@nocobase/plugin-data-source-main/src/server/__tests__/migrations/update-bigint-to-unixtimestamp.test.ts new file mode 100644 index 0000000000..9c8935e140 --- /dev/null +++ b/packages/plugins/@nocobase/plugin-data-source-main/src/server/__tests__/migrations/update-bigint-to-unixtimestamp.test.ts @@ -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'); + }); +}); diff --git a/packages/plugins/@nocobase/plugin-data-source-main/src/server/migrations/20241230000001-update-bigint-to-unixtimestamp.ts b/packages/plugins/@nocobase/plugin-data-source-main/src/server/migrations/20241230000001-update-bigint-to-unixtimestamp.ts new file mode 100644 index 0000000000..11fd772172 --- /dev/null +++ b/packages/plugins/@nocobase/plugin-data-source-main/src/server/migrations/20241230000001-update-bigint-to-unixtimestamp.ts @@ -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(); + } + } +}