Add Tenant initialisation service (#2100)

* Add Tenant initialisation service

* few fixes

* fix constraint

* fix tests

* update metadata json with employees and address

* add V2

* remove metadata.gql
This commit is contained in:
Weiko
2023-10-18 18:01:52 +02:00
committed by GitHub
parent 1cd91e60fa
commit 7fbef6d60d
37 changed files with 513 additions and 177 deletions

View File

@ -4,8 +4,8 @@ import { QueryRunner, Table, TableColumn } from 'typeorm';
import { DataSourceService } from 'src/metadata/data-source/data-source.service';
import {
TenantMigrationTableChange,
TenantMigrationColumnChange,
TenantMigrationTableAction,
TenantMigrationColumnAction,
} from 'src/metadata/tenant-migration/tenant-migration.entity';
import { TenantMigrationService } from 'src/metadata/tenant-migration/tenant-migration.service';
@ -20,11 +20,11 @@ export class MigrationRunnerService {
* Executes pending migrations for a given workspace
*
* @param workspaceId string
* @returns Promise<TenantMigrationTableChange[]>
* @returns Promise<TenantMigrationTableAction[]>
*/
public async executeMigrationFromPendingMigrations(
workspaceId: string,
): Promise<TenantMigrationTableChange[]> {
): Promise<TenantMigrationTableAction[]> {
const workspaceDataSource =
await this.dataSourceService.connectToWorkspaceDataSource(workspaceId);
@ -35,7 +35,7 @@ export class MigrationRunnerService {
const pendingMigrations =
await this.tenantMigrationService.getPendingMigrations(workspaceId);
const flattenedPendingMigrations: TenantMigrationTableChange[] =
const flattenedPendingMigrations: TenantMigrationTableAction[] =
pendingMigrations.reduce((acc, pendingMigration) => {
return [...acc, ...pendingMigration.migrations];
}, []);
@ -71,9 +71,9 @@ export class MigrationRunnerService {
private async handleTableChanges(
queryRunner: QueryRunner,
schemaName: string,
tableMigration: TenantMigrationTableChange,
tableMigration: TenantMigrationTableAction,
) {
switch (tableMigration.change) {
switch (tableMigration.action) {
case 'create':
await this.createTable(queryRunner, schemaName, tableMigration.name);
break;
@ -87,7 +87,7 @@ export class MigrationRunnerService {
break;
default:
throw new Error(
`Migration table change ${tableMigration.change} not supported`,
`Migration table action ${tableMigration.action} not supported`,
);
}
}
@ -142,21 +142,21 @@ export class MigrationRunnerService {
* @param queryRunner QueryRunner
* @param schemaName string
* @param tableName string
* @param columnMigrations TenantMigrationColumnChange[]
* @param columnMigrations TenantMigrationColumnAction[]
* @returns
*/
private async handleColumnChanges(
queryRunner: QueryRunner,
schemaName: string,
tableName: string,
columnMigrations?: TenantMigrationColumnChange[],
columnMigrations?: TenantMigrationColumnAction[],
) {
if (!columnMigrations || columnMigrations.length === 0) {
return;
}
for (const columnMigration of columnMigrations) {
switch (columnMigration.change) {
switch (columnMigration.action) {
case 'create':
await this.createColumn(
queryRunner,
@ -165,13 +165,9 @@ export class MigrationRunnerService {
columnMigration,
);
break;
case 'alter':
throw new Error(
`Migration column change ${columnMigration.change} not supported`,
);
default:
throw new Error(
`Migration column change ${columnMigration.change} not supported`,
`Migration column action ${columnMigration.action} not supported`,
);
}
}
@ -183,13 +179,13 @@ export class MigrationRunnerService {
* @param queryRunner QueryRunner
* @param schemaName string
* @param tableName string
* @param migrationColumn TenantMigrationColumnChange
* @param migrationColumn TenantMigrationColumnAction
*/
private async createColumn(
queryRunner: QueryRunner,
schemaName: string,
tableName: string,
migrationColumn: TenantMigrationColumnChange,
migrationColumn: TenantMigrationColumnAction,
) {
await queryRunner.addColumn(
`${schemaName}.${tableName}`,