ToolJet/server/data-migrations/1679604241777-ReplaceTooljetDbTableNamesWithId.ts

70 lines
3 KiB
TypeScript
Raw Normal View History

Feature: Import export tjdb schema (#5752) * add ability to import export app and tjdb schema * init * feat ::global settings popover new ui * feat :: ui for version export modal * fix :: import export modal * cleanup * ui updates * header footer style fixes * closing settings modal while showing export modal * style fix header * feat :: added button to download table schema * fix :: styling for fx * add ability to import and export apps with tjdb schema * handle duplicate table in workspace * fix table rename * fix selected table on edit and delete * fix invalid toast on table delete * fix column default value * handle exports to strip '::' and quotes * make import/export backward compatible * handle page redirects based on resource import * handle import without tjdb schema * fix column delete and addition * make data migrations to be run per organizations * wip * update migration * fix credentials to be included * fix specific version export * make use of apps ability for import export resource * fix import navigation * fix lint * fix failing tests * fix lint * enable tjdb for public apps * update export error message on tjdb table blank * fix table not selected after creation * fix :: styling for imp exp modal , and functionality bug fixes after dev merge * fixes blank slate and columns selection * fix table delete * fix invalid toast on table edit * fix column information missing tjdb query manager * make ds imports to either reuse global or create * export only unique table ids * create default datasources if not present in export data * reuse existing table on imports * add timestamp to table name if name already exists * add ability to clone with tjdb * make imports work with marketplace plugin * skip dataqueries for which plugins are not installed * fix filter input width * fix failing spec * fix marketplace plugin installation in diff workspaces * fix check for plugin installed in workspace * fix export when table name is empty --------- Co-authored-by: stepinfwd <stepinfwd@gmail.com>
2023-08-28 15:53:15 +00:00
import { DataSource } from 'src/entities/data_source.entity';
import { MigrationInterface, QueryRunner } from 'typeorm';
import { isEmpty } from 'lodash';
import { InternalTable } from 'src/entities/internal_table.entity';
import { Organization } from 'src/entities/organization.entity';
import { DataQuery } from 'src/entities/data_query.entity';
export class ReplaceTooljetDbTableNamesWithId1679604241777 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
let progress = 0;
const entityManager = queryRunner.manager;
const organizations = await entityManager.find(Organization, { select: ['id'] });
const orgCount = organizations.length;
console.log(`Total Organizations: ${orgCount}`);
for (const organization of organizations) {
console.log(
`ReplaceTooljetDbTableNamesWithId1679604241777 Progress ${Math.round((progress / orgCount) * 100)} %`
);
console.log(`Replacing for organization ${organization.name}: ${organization.id}`);
const tjDbDataSources = await entityManager
.createQueryBuilder(DataSource, 'data_sources')
.select(['data_sources.id', 'data_sources.appVersionId', 'apps.id', 'apps.name'])
.innerJoin('data_sources.appVersion', 'app_versions')
.innerJoin('app_versions.app', 'apps', 'apps.organizationId = :organizationId', {
organizationId: organization.id,
})
.where('data_sources.kind = :kind', { kind: 'tooljetdb' })
.getRawMany();
const tjDbDataSourcesCount = tjDbDataSources.length;
console.log(`TjDb datasources: ${tjDbDataSourcesCount}`);
for (const tjDbSource of tjDbDataSources) {
console.log(`App ${tjDbSource.apps_name}: ${tjDbSource.apps_id}`);
const dataQueriesToReplaceWithIds = await entityManager.find(DataQuery, {
where: { dataSourceId: tjDbSource.data_sources_id },
select: ['id', 'options'],
});
console.log(`TjDb dataqueries: ${dataQueriesToReplaceWithIds.length}`);
for (const dataQuery of dataQueriesToReplaceWithIds) {
const options = dataQuery.options;
const { table_name: tableName } = options;
const internalTable = await entityManager.findOne(InternalTable, {
where: { organizationId: organization.id, tableName },
select: ['id', 'tableName'],
});
// There was a bug wherein if the table name had changed, the name in app definition
// will not be changed. So there could be occurences where table with the name on
// app definition won't be found. In such cases we don't make any change for that
// query. User will have to explicitly link the table again for that query in the
// editor after this migration is run.
if (isEmpty(internalTable)) continue;
dataQuery.options = { ...options, table_id: internalTable.id };
await dataQuery.save();
}
}
progress++;
}
}
public async down(queryRunner: QueryRunner): Promise<void> {}
}