Skip to content

Commit

Permalink
Hotfix/fix for missing query reassociations (ToolJet#2471)
Browse files Browse the repository at this point in the history
* Reassociate run query events and queries in backfill for new versioning system

* Promote the order of execution for AddApplicationIconColumn

* Do not create application_icon column if it already exists

* Consider table toggle actions while migrating
  • Loading branch information
sherfin94 authored Mar 9, 2022
1 parent 45a3844 commit 4db86f0
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 23 deletions.
9 changes: 9 additions & 0 deletions server/migrations/1639734070614-AddApplicationIconColumn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddApplicationIconColumn1639734070614 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "apps" ADD COLUMN IF NOT EXISTS "icon" VARCHAR(255) DEFAULT NULL`);
}

public async down(queryRunner: QueryRunner): Promise<void> {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Organization } from 'src/entities/organization.entity';
import { EntityManager, MigrationInterface, QueryRunner } from 'typeorm';
import { AppModule } from 'src/app.module';
import { Credential } from 'src/entities/credential.entity';
import { cloneDeep } from 'lodash';

export class BackfillDataSourcesAndQueriesForAppVersions1639734070615 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
Expand Down Expand Up @@ -88,10 +89,8 @@ export class BackfillDataSourcesAndQueriesForAppVersions1639734070615 implements
) {
if (restAppVersions.length == 0) return;

const oldDataSourceToNewMapping = {};
const newDataQueries = [];

for (const appVersion of restAppVersions) {
const oldDataSourceToNewMapping = {};
for (const dataSource of dataSources) {
const convertedOptions = this.convertToArrayOfKeyValuePairs(dataSource.options);
const newOptions = await dataSourcesService.parseOptionsForCreate(convertedOptions, entityManager);
Expand All @@ -110,21 +109,29 @@ export class BackfillDataSourcesAndQueriesForAppVersions1639734070615 implements
oldDataSourceToNewMapping[dataSource.id] = newDataSource.id;
}

const newDataQueries = [];
const dataQueryMapping = {};
for (const dataQuery of dataQueries) {
const dataQueryParams = {
name: dataQuery.name,
kind: dataQuery.kind,
options: dataQuery.options,
options: cloneDeep(dataQuery.options),
dataSourceId: oldDataSourceToNewMapping[dataQuery.dataSourceId],
appId: dataQuery.appId,
appVersionId: appVersion.id,
};
const newDataQuery = await entityManager.save(entityManager.create(DataQuery, dataQueryParams));
const newDataQuery = await entityManager.save(entityManager.create(DataQuery, { ...dataQueryParams }));
newDataQueries.push(newDataQuery);
dataQueryMapping[dataQuery.id] = newDataQuery.id;
}
for (const newQuery of newDataQueries) {
const newOptions = this.replaceDataQueryOptionsWithNewDataQueryIds(newQuery.options, dataQueryMapping);
newQuery.options = newOptions;
await entityManager.save(newQuery);
}
appVersion.definition = this.replaceDataQueryIdWithinDefinitions(appVersion.definition, dataQueryMapping);
await entityManager.save(appVersion);
}
console.log(`New data sources created: ${Object.values(oldDataSourceToNewMapping)}`);
console.log(`New data queries created: ${newDataQueries.map((q) => q.id)}`);
}

convertToArrayOfKeyValuePairs(options): Array<object> {
Expand All @@ -138,6 +145,68 @@ export class BackfillDataSourcesAndQueriesForAppVersions1639734070615 implements
});
}

replaceDataQueryOptionsWithNewDataQueryIds(options, dataQueryMapping) {
if (options && options.events) {
const replacedEvents = options.events.map((event) => {
if (event.queryId) {
event.queryId = dataQueryMapping[event.queryId];
}
return event;
});
options.events = replacedEvents;
}
return options;
}

replaceDataQueryIdWithinDefinitions(definition, dataQueryMapping) {
if (definition?.components) {
for (const id of Object.keys(definition.components)) {
const component = definition.components[id].component;

if (component?.definition?.events) {
const replacedComponentEvents = component.definition.events.map((event) => {
if (event.queryId) {
event.queryId = dataQueryMapping[event.queryId];
}
return event;
});
component.definition.events = replacedComponentEvents;
}

if (component?.definition?.properties?.actions?.value) {
for (const value of component.definition.properties.actions.value) {
if (value?.events) {
const replacedComponentActionEvents = value.events.map((event) => {
if (event.queryId) {
event.queryId = dataQueryMapping[event.queryId];
}
return event;
});
value.events = replacedComponentActionEvents;
}
}
}

if (component?.component === 'Table') {
for (const column of component?.definition?.properties?.columns?.value ?? []) {
if (column?.events) {
const replacedComponentActionEvents = column.events.map((event) => {
if (event.queryId) {
event.queryId = dataQueryMapping[event.queryId];
}
return event;
});
column.events = replacedComponentActionEvents;
}
}
}

definition.components[id].component = component;
}
}
return definition;
}

async setNewCredentialValueFromOldValue(newOptions: any, oldOptions: any, entityManager: EntityManager) {
const newOptionsWithCredentials = this.convertToArrayOfKeyValuePairs(newOptions).filter((opt) => opt['encrypted']);

Expand Down
16 changes: 0 additions & 16 deletions server/migrations/1641809680591-AddApplicationIconColumn.ts

This file was deleted.

15 changes: 15 additions & 0 deletions server/src/services/app_import_export.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,21 @@ export class AppImportExportService {
}
}
}

if (component?.component === 'Table') {
for (const column of component?.definition?.properties?.columns?.value ?? []) {
if (column?.events) {
const replacedComponentActionEvents = column.events.map((event) => {
if (event.queryId) {
event.queryId = dataQueryMapping[event.queryId];
}
return event;
});
column.events = replacedComponentActionEvents;
}
}
}

definition.components[id].component = component;
}
}
Expand Down

0 comments on commit 4db86f0

Please sign in to comment.