ToolJet/server/data-migrations/1639734070615-BackfillDataSourcesAndQueriesForAppVersions.ts

243 lines
8.9 KiB
TypeScript
Raw Normal View History

import { NestFactory } from '@nestjs/core';
2025-02-25 06:52:50 +00:00
import { App } from '@entities/app.entity';
import { AppVersion } from '@entities/app_version.entity';
import { DataSource } from '@entities/data_source.entity';
import { Organization } from '@entities/organization.entity';
import { EntityManager, MigrationInterface, QueryRunner } from 'typeorm';
2025-02-25 06:52:50 +00:00
import { Credential } from '@entities/credential.entity';
import { cloneDeep } from 'lodash';
2025-02-25 06:52:50 +00:00
import { AppModule } from '@modules/app/module';
import { TOOLJET_EDITIONS, getImportPath } from '@modules/app/constants';
2025-02-25 06:52:50 +00:00
import { IDataSourcesUtilService } from '@modules/data-sources/interfaces/IUtilService';
import { getTooljetEdition } from '@helpers/utils.helper';
export class BackfillDataSourcesAndQueriesForAppVersions1639734070615 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const entityManager = queryRunner.manager;
const organizations = await entityManager.find(Organization, {
select: ['id', 'name'],
});
2025-02-25 06:52:50 +00:00
2025-08-03 07:09:18 +00:00
if (organizations.length === 0) {
console.log('No organizations found, skipping backfill.');
return;
}
2025-02-25 06:52:50 +00:00
const nestApp = await NestFactory.createApplicationContext(await AppModule.register({ IS_GET_CONTEXT: true }));
const edition: TOOLJET_EDITIONS = getTooljetEdition() as TOOLJET_EDITIONS;
2025-02-25 06:52:50 +00:00
const { DataSourcesUtilService } = await import(`${await getImportPath(true, edition)}/data-sources/util.service`);
const dataSourcesService = nestApp.get(DataSourcesUtilService);
for (const org of organizations) {
console.log(`Backfilling for organization ${org.name} : ${org.id}`);
const apps = await entityManager.find(App, {
where: { organizationId: org.id },
});
for (const app of apps) {
await this.associateDataSourcesAndQueriesToAppVersions(app, entityManager, dataSourcesService);
}
}
console.log(`Backfill done for ${organizations.length} organization/s`);
2025-02-25 06:52:50 +00:00
await nestApp.close();
}
async associateDataSourcesAndQueriesToAppVersions(
app: App,
entityManager: EntityManager,
2025-02-25 06:52:50 +00:00
dataSourcesService: IDataSourcesUtilService
) {
const appVersions = await entityManager.find(AppVersion, {
where: { appId: app.id },
order: { createdAt: 'ASC' },
});
Feature - Multi Environment support (#4833) * initial commit * multi env changes * multi-env changes * entity fixes * data query changes * fix * trying to avoid conflict with EE * moved version creation to app creation function * fixing some issues * execution of data query * revert options changes * changed migration * fixed some migration issues: testing migration * multi env support * app import export fix * fixes * migration fixes * removed plugins from data query * fixing some migration issues * fixes * remove console log * fix * front end api changes * backward compatibility for app import * Fixed a bug * correcting some mistakes * Added constraints and fixed some issues * changes * fix for data source listing * fixing version operation issues * remove kind from data query * removed kind from data query * fixes * fixes * fix for version creation * migration fixes * Fixed preview and run query issues * Fix: new version and event query id issue * fixed rest api oauth issue - next test refresh token * import export changes * fixes for app import * import fix * added await for for loops * fix * fix for migration * Fixed backend oauth-envId issue * import export changes * migration fixes * fix * fix * fix for app import from 0.9.0 * test case fixes * test case fixes * making app name mandatory for import * adding type for options * fix: imported apps query linking issues * review changes * lint issue fixes * added on delete cascade Co-authored-by: Muhsin Shah <muhsinshah21@gmail.com>
2022-12-09 14:53:42 +00:00
const dataSources = await entityManager.query(
'select * from data_sources where app_id = $1 and app_version_id IS NULL',
[app.id]
);
const dataQueries = await entityManager.query(
'select * from data_queries where app_id = $1 and app_version_id IS NULL',
[app.id]
);
const [firstAppVersion, ...restAppVersions] = appVersions;
await this.associateExistingDataSourceAndQueriesToVersion(firstAppVersion, dataSources, dataQueries, entityManager);
await this.createNewDataSourcesAndQueriesForVersions(
restAppVersions,
dataSources,
dataQueries,
entityManager,
dataSourcesService
);
}
async associateExistingDataSourceAndQueriesToVersion(
firstAppVersion: AppVersion,
Feature - Multi Environment support (#4833) * initial commit * multi env changes * multi-env changes * entity fixes * data query changes * fix * trying to avoid conflict with EE * moved version creation to app creation function * fixing some issues * execution of data query * revert options changes * changed migration * fixed some migration issues: testing migration * multi env support * app import export fix * fixes * migration fixes * removed plugins from data query * fixing some migration issues * fixes * remove console log * fix * front end api changes * backward compatibility for app import * Fixed a bug * correcting some mistakes * Added constraints and fixed some issues * changes * fix for data source listing * fixing version operation issues * remove kind from data query * removed kind from data query * fixes * fixes * fix for version creation * migration fixes * Fixed preview and run query issues * Fix: new version and event query id issue * fixed rest api oauth issue - next test refresh token * import export changes * fixes for app import * import fix * added await for for loops * fix * fix for migration * Fixed backend oauth-envId issue * import export changes * migration fixes * fix * fix * fix for app import from 0.9.0 * test case fixes * test case fixes * making app name mandatory for import * adding type for options * fix: imported apps query linking issues * review changes * lint issue fixes * added on delete cascade Co-authored-by: Muhsin Shah <muhsinshah21@gmail.com>
2022-12-09 14:53:42 +00:00
dataSources: any[],
dataQueries: any[],
entityManager: EntityManager
) {
if (!firstAppVersion) return;
for (const dataSource of dataSources) {
await entityManager.update(DataSource, dataSource.id, {
appVersionId: firstAppVersion.id,
});
}
Feature - Multi Environment support (#4833) * initial commit * multi env changes * multi-env changes * entity fixes * data query changes * fix * trying to avoid conflict with EE * moved version creation to app creation function * fixing some issues * execution of data query * revert options changes * changed migration * fixed some migration issues: testing migration * multi env support * app import export fix * fixes * migration fixes * removed plugins from data query * fixing some migration issues * fixes * remove console log * fix * front end api changes * backward compatibility for app import * Fixed a bug * correcting some mistakes * Added constraints and fixed some issues * changes * fix for data source listing * fixing version operation issues * remove kind from data query * removed kind from data query * fixes * fixes * fix for version creation * migration fixes * Fixed preview and run query issues * Fix: new version and event query id issue * fixed rest api oauth issue - next test refresh token * import export changes * fixes for app import * import fix * added await for for loops * fix * fix for migration * Fixed backend oauth-envId issue * import export changes * migration fixes * fix * fix * fix for app import from 0.9.0 * test case fixes * test case fixes * making app name mandatory for import * adding type for options * fix: imported apps query linking issues * review changes * lint issue fixes * added on delete cascade Co-authored-by: Muhsin Shah <muhsinshah21@gmail.com>
2022-12-09 14:53:42 +00:00
if (dataQueries?.length) {
await entityManager.query(
`update data_queries set app_version_id = $1 where id IN(${dataQueries.map((dq) => `'${dq.id}'`)?.join()})`,
[firstAppVersion.id]
);
}
}
async createNewDataSourcesAndQueriesForVersions(
restAppVersions: AppVersion[],
Feature - Multi Environment support (#4833) * initial commit * multi env changes * multi-env changes * entity fixes * data query changes * fix * trying to avoid conflict with EE * moved version creation to app creation function * fixing some issues * execution of data query * revert options changes * changed migration * fixed some migration issues: testing migration * multi env support * app import export fix * fixes * migration fixes * removed plugins from data query * fixing some migration issues * fixes * remove console log * fix * front end api changes * backward compatibility for app import * Fixed a bug * correcting some mistakes * Added constraints and fixed some issues * changes * fix for data source listing * fixing version operation issues * remove kind from data query * removed kind from data query * fixes * fixes * fix for version creation * migration fixes * Fixed preview and run query issues * Fix: new version and event query id issue * fixed rest api oauth issue - next test refresh token * import export changes * fixes for app import * import fix * added await for for loops * fix * fix for migration * Fixed backend oauth-envId issue * import export changes * migration fixes * fix * fix * fix for app import from 0.9.0 * test case fixes * test case fixes * making app name mandatory for import * adding type for options * fix: imported apps query linking issues * review changes * lint issue fixes * added on delete cascade Co-authored-by: Muhsin Shah <muhsinshah21@gmail.com>
2022-12-09 14:53:42 +00:00
dataSources,
dataQueries,
entityManager: EntityManager,
2025-02-25 06:52:50 +00:00
dataSourcesService: IDataSourcesUtilService
) {
if (restAppVersions.length == 0) return;
for (const appVersion of restAppVersions) {
const oldDataSourceToNewMapping = {};
for (const dataSource of dataSources) {
const convertedOptions = this.convertToArrayOfKeyValuePairs(dataSource.options);
Feature - Multi Environment support (#4833) * initial commit * multi env changes * multi-env changes * entity fixes * data query changes * fix * trying to avoid conflict with EE * moved version creation to app creation function * fixing some issues * execution of data query * revert options changes * changed migration * fixed some migration issues: testing migration * multi env support * app import export fix * fixes * migration fixes * removed plugins from data query * fixing some migration issues * fixes * remove console log * fix * front end api changes * backward compatibility for app import * Fixed a bug * correcting some mistakes * Added constraints and fixed some issues * changes * fix for data source listing * fixing version operation issues * remove kind from data query * removed kind from data query * fixes * fixes * fix for version creation * migration fixes * Fixed preview and run query issues * Fix: new version and event query id issue * fixed rest api oauth issue - next test refresh token * import export changes * fixes for app import * import fix * added await for for loops * fix * fix for migration * Fixed backend oauth-envId issue * import export changes * migration fixes * fix * fix * fix for app import from 0.9.0 * test case fixes * test case fixes * making app name mandatory for import * adding type for options * fix: imported apps query linking issues * review changes * lint issue fixes * added on delete cascade Co-authored-by: Muhsin Shah <muhsinshah21@gmail.com>
2022-12-09 14:53:42 +00:00
const newOptions = await dataSourcesService.parseOptionsForCreate(convertedOptions, false, entityManager);
await this.setNewCredentialValueFromOldValue(newOptions, convertedOptions, entityManager);
Feature - Multi Environment support (#4833) * initial commit * multi env changes * multi-env changes * entity fixes * data query changes * fix * trying to avoid conflict with EE * moved version creation to app creation function * fixing some issues * execution of data query * revert options changes * changed migration * fixed some migration issues: testing migration * multi env support * app import export fix * fixes * migration fixes * removed plugins from data query * fixing some migration issues * fixes * remove console log * fix * front end api changes * backward compatibility for app import * Fixed a bug * correcting some mistakes * Added constraints and fixed some issues * changes * fix for data source listing * fixing version operation issues * remove kind from data query * removed kind from data query * fixes * fixes * fix for version creation * migration fixes * Fixed preview and run query issues * Fix: new version and event query id issue * fixed rest api oauth issue - next test refresh token * import export changes * fixes for app import * import fix * added await for for loops * fix * fix for migration * Fixed backend oauth-envId issue * import export changes * migration fixes * fix * fix * fix for app import from 0.9.0 * test case fixes * test case fixes * making app name mandatory for import * adding type for options * fix: imported apps query linking issues * review changes * lint issue fixes * added on delete cascade Co-authored-by: Muhsin Shah <muhsinshah21@gmail.com>
2022-12-09 14:53:42 +00:00
const newDataSource = await entityManager.query(
'insert into data_sources (name, kind, options, app_id, app_version_id) values ($1, $2, $3, $4, $5) returning "id"',
[dataSource.name, dataSource.kind, newOptions, dataSource.app_id, appVersion.id]
);
Feature - Multi Environment support (#4833) * initial commit * multi env changes * multi-env changes * entity fixes * data query changes * fix * trying to avoid conflict with EE * moved version creation to app creation function * fixing some issues * execution of data query * revert options changes * changed migration * fixed some migration issues: testing migration * multi env support * app import export fix * fixes * migration fixes * removed plugins from data query * fixing some migration issues * fixes * remove console log * fix * front end api changes * backward compatibility for app import * Fixed a bug * correcting some mistakes * Added constraints and fixed some issues * changes * fix for data source listing * fixing version operation issues * remove kind from data query * removed kind from data query * fixes * fixes * fix for version creation * migration fixes * Fixed preview and run query issues * Fix: new version and event query id issue * fixed rest api oauth issue - next test refresh token * import export changes * fixes for app import * import fix * added await for for loops * fix * fix for migration * Fixed backend oauth-envId issue * import export changes * migration fixes * fix * fix * fix for app import from 0.9.0 * test case fixes * test case fixes * making app name mandatory for import * adding type for options * fix: imported apps query linking issues * review changes * lint issue fixes * added on delete cascade Co-authored-by: Muhsin Shah <muhsinshah21@gmail.com>
2022-12-09 14:53:42 +00:00
oldDataSourceToNewMapping[dataSource.id] = newDataSource[0].id;
}
const newDataQueries = [];
const dataQueryMapping = {};
for (const dataQuery of dataQueries) {
Feature - Multi Environment support (#4833) * initial commit * multi env changes * multi-env changes * entity fixes * data query changes * fix * trying to avoid conflict with EE * moved version creation to app creation function * fixing some issues * execution of data query * revert options changes * changed migration * fixed some migration issues: testing migration * multi env support * app import export fix * fixes * migration fixes * removed plugins from data query * fixing some migration issues * fixes * remove console log * fix * front end api changes * backward compatibility for app import * Fixed a bug * correcting some mistakes * Added constraints and fixed some issues * changes * fix for data source listing * fixing version operation issues * remove kind from data query * removed kind from data query * fixes * fixes * fix for version creation * migration fixes * Fixed preview and run query issues * Fix: new version and event query id issue * fixed rest api oauth issue - next test refresh token * import export changes * fixes for app import * import fix * added await for for loops * fix * fix for migration * Fixed backend oauth-envId issue * import export changes * migration fixes * fix * fix * fix for app import from 0.9.0 * test case fixes * test case fixes * making app name mandatory for import * adding type for options * fix: imported apps query linking issues * review changes * lint issue fixes * added on delete cascade Co-authored-by: Muhsin Shah <muhsinshah21@gmail.com>
2022-12-09 14:53:42 +00:00
const newDataQueryResult = await entityManager.query(
'insert into data_queries (name, kind, options, data_source_id, app_id, app_version_id) values ($1, $2, $3, $4, $5, $6) returning *',
[
dataQuery.name,
dataQuery.kind,
cloneDeep(dataQuery.options),
oldDataSourceToNewMapping[dataQuery.data_source_id],
dataQuery.app_id,
appVersion.id,
]
);
const newDataQuery = newDataQueryResult[0];
newDataQueries.push(newDataQuery);
dataQueryMapping[dataQuery.id] = newDataQuery.id;
}
for (const newQuery of newDataQueries) {
const newOptions = this.replaceDataQueryOptionsWithNewDataQueryIds(newQuery.options, dataQueryMapping);
newQuery.options = newOptions;
Feature - Multi Environment support (#4833) * initial commit * multi env changes * multi-env changes * entity fixes * data query changes * fix * trying to avoid conflict with EE * moved version creation to app creation function * fixing some issues * execution of data query * revert options changes * changed migration * fixed some migration issues: testing migration * multi env support * app import export fix * fixes * migration fixes * removed plugins from data query * fixing some migration issues * fixes * remove console log * fix * front end api changes * backward compatibility for app import * Fixed a bug * correcting some mistakes * Added constraints and fixed some issues * changes * fix for data source listing * fixing version operation issues * remove kind from data query * removed kind from data query * fixes * fixes * fix for version creation * migration fixes * Fixed preview and run query issues * Fix: new version and event query id issue * fixed rest api oauth issue - next test refresh token * import export changes * fixes for app import * import fix * added await for for loops * fix * fix for migration * Fixed backend oauth-envId issue * import export changes * migration fixes * fix * fix * fix for app import from 0.9.0 * test case fixes * test case fixes * making app name mandatory for import * adding type for options * fix: imported apps query linking issues * review changes * lint issue fixes * added on delete cascade Co-authored-by: Muhsin Shah <muhsinshah21@gmail.com>
2022-12-09 14:53:42 +00:00
await entityManager.query('update data_queries set options = $1 where id = $2', [newOptions, newQuery.id]);
}
appVersion.definition = this.replaceDataQueryIdWithinDefinitions(appVersion.definition, dataQueryMapping);
await entityManager.save(appVersion);
}
}
convertToArrayOfKeyValuePairs(options): Array<object> {
return Object.keys(options).map((key) => {
return {
key: key,
value: options[key]['value'],
encrypted: options[key]['encrypted'],
credential_id: options[key]['credential_id'],
};
});
}
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']);
for (const newOption of newOptionsWithCredentials) {
const oldOption = oldOptions.find((oldOption) => oldOption['key'] == newOption['key']);
const oldCredential = await entityManager.findOne(Credential, { where: { id: oldOption.credential_id } });
const newCredential = await entityManager.findOne(Credential, { where: { id: newOption['credential_id'] } });
newCredential.valueCiphertext = oldCredential?.valueCiphertext;
await entityManager.save(newCredential);
}
}
public async down(queryRunner: QueryRunner): Promise<void> {}
}