Skip to content

Commit

Permalink
Enable remotes with existing name (#5433)
Browse files Browse the repository at this point in the history
- Check if a table with the same name already exists
- If yes, add a number suffix, and check again

Co-authored-by: Thomas Trompette <[email protected]>
  • Loading branch information
thomtrp and Thomas Trompette authored May 17, 2024
1 parent 58f8c31 commit 36e5411
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,21 @@ export class RemoteTableService {
workspaceId,
);

const localTableName = getRemoteTableLocalName(input.name);
const workspaceDataSource =
await this.workspaceDataSourceService.connectToWorkspaceDataSource(
workspaceId,
);

await this.validateTableNameDoesNotExists(
localTableName,
workspaceId,
dataSourceMetatada.schema,
);
const { baseName: localTableBaseName, suffix: localTableSuffix } =
await getRemoteTableLocalName(
input.name,
dataSourceMetatada.schema,
workspaceDataSource,
);

const localTableName = localTableSuffix
? `${localTableBaseName}${localTableSuffix}`
: localTableBaseName;

const remoteTableEntity = this.remoteTableRepository.create({
distantTableName: input.name,
Expand Down Expand Up @@ -297,7 +305,8 @@ export class RemoteTableService {

await this.createRemoteTableMetadata(
workspaceId,
localTableName,
localTableBaseName,
localTableSuffix,
distantTableColumns,
distantTableIdColumn,
dataSourceMetatada.id,
Expand Down Expand Up @@ -411,27 +420,6 @@ export class RemoteTableService {
await this.workspaceCacheVersionService.incrementVersion(workspaceId);
}

private async validateTableNameDoesNotExists(
tableName: string,
workspaceId: string,
workspaceSchemaName: string,
) {
const workspaceDataSource =
await this.workspaceDataSourceService.connectToWorkspaceDataSource(
workspaceId,
);

const numberOfTablesWithSameName = +(
await workspaceDataSource.query(
`SELECT count(table_name) FROM information_schema.tables WHERE table_name LIKE '${tableName}' AND table_schema IN ('core', 'metadata', '${workspaceSchemaName}')`,
)
)[0].count;

if (numberOfTablesWithSameName > 0) {
throw new BadRequestException('Table name is not available.');
}
}

private async fetchForeignTableNamesWithinWorkspace(
workspaceId: string,
foreignDataWrapperId: string,
Expand Down Expand Up @@ -525,16 +513,25 @@ export class RemoteTableService {

private async createRemoteTableMetadata(
workspaceId: string,
localTableName: string,
localTableBaseName: string,
localTableSuffix: number | undefined,
distantTableColumns: PostgresTableSchemaColumn[],
distantTableIdColumn: PostgresTableSchemaColumn,
dataSourceMetadataId: string,
) {
const localTableNameSingular = localTableSuffix
? `${localTableBaseName}${localTableSuffix}`
: localTableBaseName;

const localTableNamePlural = localTableSuffix
? `${plural(localTableBaseName)}${localTableSuffix}`
: plural(localTableBaseName);

const objectMetadata = await this.objectMetadataService.createOne({
nameSingular: localTableName,
namePlural: plural(localTableName),
labelSingular: camelToTitleCase(camelCase(localTableName)),
labelPlural: camelToTitleCase(plural(camelCase(localTableName))),
nameSingular: localTableNameSingular,
namePlural: localTableNamePlural,
labelSingular: camelToTitleCase(camelCase(localTableBaseName)),
labelPlural: camelToTitleCase(plural(camelCase(localTableBaseName))),
description: 'Remote table',
dataSourceId: dataSourceMetadataId,
workspaceId: workspaceId,
Expand Down Expand Up @@ -571,7 +568,7 @@ export class RemoteTableService {
}
} catch (error) {
this.logger.error(
`Could not create field ${columnName} for remote table ${localTableName}: ${error}`,
`Could not create field ${columnName} for remote table ${localTableNameSingular}: ${error}`,
);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,59 @@
import { BadRequestException } from '@nestjs/common/exceptions';

import { singular } from 'pluralize';
import { DataSource } from 'typeorm';

import { camelCase } from 'src/utils/camel-case';

export const getRemoteTableLocalName = (distantTableName: string) =>
singular(camelCase(distantTableName));
const MAX_SUFFIX = 10;

type RemoteTableLocalName = {
baseName: string;
suffix?: number;
};

const isNameAvailable = async (
tableName: string,
workspaceSchemaName: string,
workspaceDataSource: DataSource,
) => {
const numberOfTablesWithSameName = +(
await workspaceDataSource.query(
`SELECT count(table_name) FROM information_schema.tables WHERE table_name LIKE '${tableName}' AND table_schema IN ('core', 'metadata', '${workspaceSchemaName}')`,
)
)[0].count;

return numberOfTablesWithSameName === 0;
};

export const getRemoteTableLocalName = async (
distantTableName: string,
workspaceSchemaName: string,
workspaceDataSource: DataSource,
): Promise<RemoteTableLocalName> => {
const baseName = singular(camelCase(distantTableName));
const isBaseNameValid = await isNameAvailable(
baseName,
workspaceSchemaName,
workspaceDataSource,
);

if (isBaseNameValid) {
return { baseName };
}

for (let suffix = 2; suffix < MAX_SUFFIX; suffix++) {
const name = `${baseName}${suffix}`;
const isNameWithSuffixValid = await isNameAvailable(
name,
workspaceSchemaName,
workspaceDataSource,
);

if (isNameWithSuffixValid) {
return { baseName, suffix };
}
}

throw new BadRequestException('Table name is already taken.');
};

0 comments on commit 36e5411

Please sign in to comment.