mirror of
https://github.com/ToolJet/ToolJet
synced 2026-04-21 21:47:17 +00:00
* 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>
107 lines
4.3 KiB
TypeScript
107 lines
4.3 KiB
TypeScript
import { App } from 'src/entities/app.entity';
|
|
import { AppEnvironment } from 'src/entities/app_environments.entity';
|
|
import { AppVersion } from 'src/entities/app_version.entity';
|
|
import { DataSource } from 'src/entities/data_source.entity';
|
|
import { DataSourceOptions } from 'src/entities/data_source_options.entity';
|
|
import { EntityManager, MigrationInterface, QueryRunner } from 'typeorm';
|
|
import { AppsService } from '@services/apps.service';
|
|
import { DataSourcesService } from '@services/data_sources.service';
|
|
import { defaultAppEnvironments } from 'src/helpers/utils.helper';
|
|
import { NestFactory } from '@nestjs/core';
|
|
import { AppModule } from 'src/app.module';
|
|
|
|
export class moveDataSourceOptionsToEnvironment1669054493160 implements MigrationInterface {
|
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
|
// Create default environment for all apps
|
|
const entityManager = queryRunner.manager;
|
|
const apps = await entityManager.find(App);
|
|
if (apps?.length) {
|
|
await Promise.all(
|
|
apps.map(async (app: App) => {
|
|
const appVersions = await entityManager.find(AppVersion, { where: { appId: app.id } });
|
|
if (appVersions?.length) {
|
|
await Promise.all(
|
|
appVersions.map(async (appVersion: AppVersion) => {
|
|
await this.associateDataQueriesAndSources(entityManager, app, appVersion);
|
|
})
|
|
);
|
|
} else {
|
|
// apps who doesnt have versions; create default version and env, migrate ds and queries
|
|
const { definition } = await entityManager
|
|
.createQueryBuilder()
|
|
.select()
|
|
.from('apps', 'app')
|
|
.where('app.id = :id', { id: app.id })
|
|
.getRawOne();
|
|
|
|
const defaultAppVersion = await entityManager.save(
|
|
AppVersion,
|
|
entityManager.create(AppVersion, {
|
|
name: 'v1',
|
|
app,
|
|
definition,
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
})
|
|
);
|
|
await this.associateExistingDataSourceAndQueriesToVersion(entityManager, defaultAppVersion);
|
|
await this.associateDataQueriesAndSources(entityManager, app, defaultAppVersion);
|
|
}
|
|
})
|
|
);
|
|
}
|
|
}
|
|
|
|
private async associateDataQueriesAndSources(entityManager: EntityManager, app: App, appVersion: AppVersion) {
|
|
const nestApp = await NestFactory.createApplicationContext(AppModule);
|
|
const dataSourcesService = nestApp.get(DataSourcesService);
|
|
const appsService = nestApp.get(AppsService);
|
|
return await Promise.all(
|
|
defaultAppEnvironments.map(async ({ name, isDefault }) => {
|
|
const environment: AppEnvironment = await entityManager.save(
|
|
entityManager.create(AppEnvironment, {
|
|
name,
|
|
isDefault,
|
|
appVersionId: appVersion.id,
|
|
})
|
|
);
|
|
// Get all data sources under app
|
|
const dataSources = await entityManager.query('select * from data_sources where app_id = $1', [
|
|
appVersion.appId,
|
|
]);
|
|
|
|
if (dataSources?.length) {
|
|
await Promise.all(
|
|
dataSources.map(async (dataSource: any) => {
|
|
const convertedOptions = appsService.convertToArrayOfKeyValuePairs(dataSource.options);
|
|
const options = !environment.isDefault
|
|
? await dataSourcesService.parseOptionsForCreate(convertedOptions, true, entityManager)
|
|
: dataSource.options;
|
|
await entityManager.save(
|
|
entityManager.create(DataSourceOptions, {
|
|
dataSourceId: dataSource.id,
|
|
environmentId: environment.id,
|
|
options,
|
|
})
|
|
);
|
|
})
|
|
);
|
|
}
|
|
})
|
|
);
|
|
}
|
|
|
|
async associateExistingDataSourceAndQueriesToVersion(manager: EntityManager, appVersion: AppVersion) {
|
|
const dataSources = await manager.query(
|
|
'select id from data_sources where app_id = $1 and app_version_id IS NULL',
|
|
[appVersion.appId]
|
|
);
|
|
for await (const { id } of dataSources) {
|
|
await manager.update(DataSource, id, {
|
|
appVersionId: appVersion.id,
|
|
});
|
|
}
|
|
}
|
|
|
|
public async down(queryRunner: QueryRunner): Promise<void> {}
|
|
}
|