Enable remotes with existing name (#5433)
- Check if a table with the same name already exists - If yes, add a number suffix, and check again Co-authored-by: Thomas Trompette <thomast@twenty.com>
This commit is contained in:
@ -258,13 +258,21 @@ export class RemoteTableService {
|
|||||||
workspaceId,
|
workspaceId,
|
||||||
);
|
);
|
||||||
|
|
||||||
const localTableName = getRemoteTableLocalName(input.name);
|
const workspaceDataSource =
|
||||||
|
await this.workspaceDataSourceService.connectToWorkspaceDataSource(
|
||||||
|
workspaceId,
|
||||||
|
);
|
||||||
|
|
||||||
await this.validateTableNameDoesNotExists(
|
const { baseName: localTableBaseName, suffix: localTableSuffix } =
|
||||||
localTableName,
|
await getRemoteTableLocalName(
|
||||||
workspaceId,
|
input.name,
|
||||||
dataSourceMetatada.schema,
|
dataSourceMetatada.schema,
|
||||||
);
|
workspaceDataSource,
|
||||||
|
);
|
||||||
|
|
||||||
|
const localTableName = localTableSuffix
|
||||||
|
? `${localTableBaseName}${localTableSuffix}`
|
||||||
|
: localTableBaseName;
|
||||||
|
|
||||||
const remoteTableEntity = this.remoteTableRepository.create({
|
const remoteTableEntity = this.remoteTableRepository.create({
|
||||||
distantTableName: input.name,
|
distantTableName: input.name,
|
||||||
@ -297,7 +305,8 @@ export class RemoteTableService {
|
|||||||
|
|
||||||
await this.createRemoteTableMetadata(
|
await this.createRemoteTableMetadata(
|
||||||
workspaceId,
|
workspaceId,
|
||||||
localTableName,
|
localTableBaseName,
|
||||||
|
localTableSuffix,
|
||||||
distantTableColumns,
|
distantTableColumns,
|
||||||
distantTableIdColumn,
|
distantTableIdColumn,
|
||||||
dataSourceMetatada.id,
|
dataSourceMetatada.id,
|
||||||
@ -411,27 +420,6 @@ export class RemoteTableService {
|
|||||||
await this.workspaceCacheVersionService.incrementVersion(workspaceId);
|
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(
|
private async fetchForeignTableNamesWithinWorkspace(
|
||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
foreignDataWrapperId: string,
|
foreignDataWrapperId: string,
|
||||||
@ -525,16 +513,25 @@ export class RemoteTableService {
|
|||||||
|
|
||||||
private async createRemoteTableMetadata(
|
private async createRemoteTableMetadata(
|
||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
localTableName: string,
|
localTableBaseName: string,
|
||||||
|
localTableSuffix: number | undefined,
|
||||||
distantTableColumns: PostgresTableSchemaColumn[],
|
distantTableColumns: PostgresTableSchemaColumn[],
|
||||||
distantTableIdColumn: PostgresTableSchemaColumn,
|
distantTableIdColumn: PostgresTableSchemaColumn,
|
||||||
dataSourceMetadataId: string,
|
dataSourceMetadataId: string,
|
||||||
) {
|
) {
|
||||||
|
const localTableNameSingular = localTableSuffix
|
||||||
|
? `${localTableBaseName}${localTableSuffix}`
|
||||||
|
: localTableBaseName;
|
||||||
|
|
||||||
|
const localTableNamePlural = localTableSuffix
|
||||||
|
? `${plural(localTableBaseName)}${localTableSuffix}`
|
||||||
|
: plural(localTableBaseName);
|
||||||
|
|
||||||
const objectMetadata = await this.objectMetadataService.createOne({
|
const objectMetadata = await this.objectMetadataService.createOne({
|
||||||
nameSingular: localTableName,
|
nameSingular: localTableNameSingular,
|
||||||
namePlural: plural(localTableName),
|
namePlural: localTableNamePlural,
|
||||||
labelSingular: camelToTitleCase(camelCase(localTableName)),
|
labelSingular: camelToTitleCase(camelCase(localTableBaseName)),
|
||||||
labelPlural: camelToTitleCase(plural(camelCase(localTableName))),
|
labelPlural: camelToTitleCase(plural(camelCase(localTableBaseName))),
|
||||||
description: 'Remote table',
|
description: 'Remote table',
|
||||||
dataSourceId: dataSourceMetadataId,
|
dataSourceId: dataSourceMetadataId,
|
||||||
workspaceId: workspaceId,
|
workspaceId: workspaceId,
|
||||||
@ -571,7 +568,7 @@ export class RemoteTableService {
|
|||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error(
|
this.logger.error(
|
||||||
`Could not create field ${columnName} for remote table ${localTableName}: ${error}`,
|
`Could not create field ${columnName} for remote table ${localTableNameSingular}: ${error}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,59 @@
|
|||||||
|
import { BadRequestException } from '@nestjs/common/exceptions';
|
||||||
|
|
||||||
import { singular } from 'pluralize';
|
import { singular } from 'pluralize';
|
||||||
|
import { DataSource } from 'typeorm';
|
||||||
|
|
||||||
import { camelCase } from 'src/utils/camel-case';
|
import { camelCase } from 'src/utils/camel-case';
|
||||||
|
|
||||||
export const getRemoteTableLocalName = (distantTableName: string) =>
|
const MAX_SUFFIX = 10;
|
||||||
singular(camelCase(distantTableName));
|
|
||||||
|
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.');
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user