-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
Showing
2 changed files
with
86 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 55 additions & 2 deletions
57
...ine/metadata-modules/remote-server/remote-table/utils/get-remote-table-local-name.util.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
}; |