ToolJet/server/data-migrations/1625814801430-UpdateDefinitionsForEvents.ts

63 lines
2.2 KiB
TypeScript
Raw Normal View History

2025-02-25 06:52:50 +00:00
import { AppVersion } from '@entities/app_version.entity';
2022-07-15 09:47:18 +00:00
import { MigrationInterface, QueryRunner } from 'typeorm';
export class UpdateDefinitionsForEvents1625814801430 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const entityManager = queryRunner.manager;
const queryBuilder = queryRunner.connection.createQueryBuilder();
const appVersionRepository = entityManager.getRepository(AppVersion);
const appVersions = await appVersionRepository.find();
2022-07-15 09:47:18 +00:00
for (const version of appVersions) {
const definition = version['definition'];
2022-07-15 09:47:18 +00:00
if (definition) {
const components = definition['components'];
2022-07-15 09:47:18 +00:00
for (const componentId of Object.keys(components)) {
const component = components[componentId];
const events = component.component.definition.events;
2022-07-15 09:47:18 +00:00
if (events) {
const newEvents = [];
2022-07-15 09:47:18 +00:00
for (const eventId of Object.keys(events)) {
const actionId = events[eventId]['actionId'];
2022-07-15 09:47:18 +00:00
if (actionId) {
const newEvent = { ...events[eventId]['options'], actionId, eventId };
newEvents.push(newEvent);
} else {
2022-07-15 09:47:18 +00:00
if (eventId === 'onBulkUpdate' && Object.keys(events[eventId]?.options || {}).length != 0) {
const newEvent = { ...events[eventId]['options'], actionId: 'run-query', eventId };
newEvents.push(newEvent);
}
}
}
component.component.definition.events = newEvents;
components[componentId] = {
...component,
component: {
...component.component,
definition: {
...component.component.definition,
2022-07-15 09:47:18 +00:00
events: newEvents,
},
},
};
}
}
definition['components'] = components;
version.definition = definition;
2022-07-15 09:47:18 +00:00
await queryBuilder.update(AppVersion).set({ definition }).where('id = :id', { id: version.id }).execute();
}
}
}
2022-07-15 09:47:18 +00:00
public async down(queryRunner: QueryRunner): Promise<void> {}
}