ToolJet/server/data-migrations/1707466537651-MoveVisibilityDisabledStatesToProperties.ts

91 lines
2.8 KiB
TypeScript
Raw Normal View History

2025-02-25 06:52:50 +00:00
import { Component } from '@entities/component.entity';
import { processDataInBatches } from '@helpers/migration.helper';
2024-02-15 05:51:27 +00:00
import { EntityManager, MigrationInterface, QueryRunner } from 'typeorm';
2024-02-15 05:51:27 +00:00
export class MoveVisibilityDisabledStatesToProperties1707466537651 implements MigrationInterface {
2024-02-12 08:08:26 +00:00
public async up(queryRunner: QueryRunner): Promise<void> {
2024-02-15 05:51:27 +00:00
const componentTypes = ['TextInput', 'NumberInput', 'PasswordInput', 'Text'];
2024-02-12 08:08:26 +00:00
const batchSize = 100;
const entityManager = queryRunner.manager;
for (const componentType of componentTypes) {
await processDataInBatches(
entityManager,
async (entityManager: EntityManager) => {
return await entityManager.find(Component, {
where: { type: componentType },
2024-02-15 05:51:27 +00:00
order: { createdAt: 'ASC' },
2024-02-12 08:08:26 +00:00
});
},
async (entityManager: EntityManager, components: Component[]) => {
await this.processUpdates(entityManager, components);
},
batchSize
);
}
2024-02-12 08:08:26 +00:00
}
private async processUpdates(entityManager, components) {
for (const component of components) {
const properties = component.properties;
const styles = component.styles;
const general = component.general;
const generalStyles = component.generalStyles;
const validation = component.validation;
2024-02-12 08:08:26 +00:00
if (styles.visibility) {
properties.visibility = styles.visibility;
delete styles.visibility;
}
if (styles.disabledState) {
properties.disabledState = styles.disabledState;
delete styles.disabledState;
}
if (general?.tooltip) {
properties.tooltip = general?.tooltip;
delete general?.tooltip;
}
if (generalStyles?.boxShadow) {
styles.boxShadow = generalStyles?.boxShadow;
delete generalStyles?.boxShadow;
}
// Label and value
2024-02-15 05:51:27 +00:00
if (component.type !== 'Text') {
if (properties.label == undefined || null) {
2024-02-15 05:51:27 +00:00
properties.label = '';
}
if (styles.borderRadius == undefined || null) {
2024-02-15 05:51:27 +00:00
styles.borderRadius = { value: '{{4}}' };
}
}
2024-02-15 05:51:27 +00:00
// Moving 'minValue' from properties to validation
2024-02-15 05:51:27 +00:00
if (component.type == 'NumberInput') {
if (properties.minValue) {
validation.minValue = properties.minValue;
delete properties.minValue; // Removing 'minValue' from properties
}
if (properties.maxValue) {
validation.maxValue = properties.maxValue;
delete properties.maxValue; // Removing 'minValue' from properties
}
}
await entityManager.update(Component, component.id, {
properties,
styles,
general,
generalStyles,
validation,
});
}
2024-02-12 08:08:26 +00:00
}
2024-02-12 08:08:26 +00:00
public async down(queryRunner: QueryRunner): Promise<void> {}
}