ToolJet/server/data-migrations/1676545162064-BackfillRunpyDatasources.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

56 lines
2.2 KiB
TypeScript

import { EntityManager, MigrationInterface, QueryRunner } from 'typeorm';
/**
* finds all the queries which are attached to restapi datasource with type static but have a non-null code option, which indicates that they are actually associated with a runpy data source.
* creates a new runpy data source and attaches all such queries to it.
*/
export class BackfillRunpyDatasources1676545162064 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const entityManager = queryRunner.manager;
let progress = 0;
const allVersions = await entityManager
.createQueryBuilder()
.select()
.from('app_versions', 'app_versions')
.getRawMany();
for (const version of allVersions) {
progress++;
console.log(
`BackfillRunpyDatasources1676545162064 Progress ${Math.round((progress / allVersions.length) * 100)} %`
);
await this.createDefaultVersionAndAttachQueries(entityManager, version);
}
}
async createDefaultVersionAndAttachQueries(entityManager: EntityManager, version: any) {
const wronglyAttachedRunpyQueries = await entityManager.query(
"select data_queries.id from data_queries inner join data_sources on data_queries.data_source_id = data_sources.id where data_queries.options->> 'code' is not null and data_sources.kind = 'restapi' and data_sources.type = 'static' and data_sources.app_version_id = $1",
[version.id]
);
if (wronglyAttachedRunpyQueries.length > 0) {
let runpyDS = await entityManager.query(
"select data_sources.id from data_sources where data_sources.kind = 'runpy' and data_sources.type = 'static'"
);
if (runpyDS.length === 0) {
runpyDS = await entityManager.query(
'insert into data_sources (name, kind, app_version_id, type) values ($1, $2, $3, $4) returning "id"',
['runpydefault', 'runpy', version.id, 'static']
);
}
await entityManager.query(
`update data_queries set data_source_id = $1 where id in (${wronglyAttachedRunpyQueries
.map(({ id }) => `'${id}'`)
.join()})`,
[runpyDS[0].id]
);
}
}
public async down(queryRunner: QueryRunner): Promise<void> {}
}