ToolJet/server/data-migrations/1675844361118-MigrateEnvironmentsUnderWorkspace.ts
vjaris42 bb9a211e55
[Feature] :: Global datasources (#5504)
* add: columns and migrations for data queries and sources

* add: migrations for app environments

* fix: datasources and queries api

* fix: import apis

* add: radixui colors

* create: global datasource page

* fix: version creation not including global datasources queries

* fix: version deletion failure

* fix: ui and other bugs

* add: check for abilities on global ds

* fix: bugs

* fix: existing test cases

* fix: migration and bugs

* fix: rest api oauthorize bugs

* hide: add button for local ds

* fix: query bugs

* fix: new organization environment creation

* fix: local ds label showing for new apps

* fix: on page load queries for preview app and published app

* fix: import bugs from v1

* fix: merge conflicts

* fix: import apis

* fix: apss with mulit envs

* fix: ui bugs

* fix: environments not being created on db:seed

* fix: ui bugs

* fix: route settings for global datasources

* fix: customer dashboard template

* fix: local ds queries not being saved

* fix: runpy issues

* changes: ui

* fix: migration issues

* fix: ui

* hide datasources when no local datasources

* fix: test cases

* fix: unit test cases and global queries on app import/export

* cleanup

* add: package-lock file

* undo: migration rename

* cleanup

* fix: ui bugs

* migration fixes

* fix: dark mode issues

* fix: change datasource failing on query create mode

* fix: workspace selector issues

* fix: clickoutside for change scope option

* migration changes

* fix: open api issue

* reverting configs changes

* [Fix] Global datasources & Environment Id issue (#5830)

* fix: oauth env id issue

* code changes

---------

Co-authored-by: gsmithun4 <gsmithun4@gmail.com>
Co-authored-by: Muhsin Shah <muhsinshah21@gmail.com>
2023-03-24 21:41:21 +05:30

74 lines
2.8 KiB
TypeScript

import { AppEnvironment } from 'src/entities/app_environments.entity';
import { Organization } from 'src/entities/organization.entity';
import { defaultAppEnvironments } from 'src/helpers/utils.helper';
import { MigrationInterface, QueryRunner } from 'typeorm';
export class MigrateEnvironmentsUnderWorkspace1675844361118 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const entityManager = queryRunner.manager;
let progress = 0;
const organizations = await entityManager.find(Organization, {
select: ['id', 'name'],
});
//Insert new environments under workspace
for (const org of organizations) {
progress++;
console.log(
`MigrateEnvironmentsUnderWorkspace1675844361118 Progress ${Math.round(
(progress / organizations.length) * 100
)} %`
);
const newMappingForEnvironments = {};
console.log(`Performing environment migration for ${org.name}: ${org.id}`);
for (const { name, isDefault } of defaultAppEnvironments) {
console.log(`Current Environment name: ${name}`);
const environment: AppEnvironment = await entityManager.save(
entityManager.create(AppEnvironment, {
name,
isDefault,
organizationId: org.id,
})
);
newMappingForEnvironments[name] = {
...newMappingForEnvironments[name],
[org.id]: environment.id,
};
//Retrieve old environments under app_versions
const oldMappingForEnvironments = {};
const envs = await queryRunner.query(
`select app_environments.id from app_versions inner join apps on apps.organization_id = $1
inner join app_environments on app_environments.app_version_id = app_versions.id
where app_versions.app_id = apps.id and app_environments.name=$2`,
[org.id, name]
);
oldMappingForEnvironments[name] = {
...oldMappingForEnvironments[name],
[org.id]: envs,
};
//Update datasources options from old and new mapping
if (oldMappingForEnvironments[name][org.id] && oldMappingForEnvironments[name][org.id].length > 0) {
await queryRunner.query(
`update data_source_options set environment_id = $1
where environment_id IN (${oldMappingForEnvironments[name][org.id]
.map((env) => `'${env.id}'`)
?.join()})`,
[newMappingForEnvironments[name][org.id]]
);
}
}
console.log(`Env migration completed for organization: ${org.name}: ${org.id}`);
}
//Drop app_version_id column as it is no longer needed
await queryRunner.dropColumn('app_environments', 'app_version_id');
}
public async down(queryRunner: QueryRunner): Promise<void> {}
}